Skip to content

283 fix(discovery): don't crash when an optional dependency (e.g. matplotlib) is missing - #293

Open
Gnpd wants to merge 5 commits into
mainfrom
283-partial-fix-discovery-crash
Open

283 fix(discovery): don't crash when an optional dependency (e.g. matplotlib) is missing#293
Gnpd wants to merge 5 commits into
mainfrom
283-partial-fix-discovery-crash

Conversation

@Gnpd

@Gnpd Gnpd commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

all_estimators(), all_displays(), and all_functions() crashed with an uncaught ImportError whenever matplotlib (the viz extra) wasn't installed, since chemotools.plotting/chemotools.inspector fail to import without it and discovery had no handling for that.

This is a fast fix for the immediate crash, but it's also worth keeping as a general safeguard: any future subpackage with an optional dependency will now degrade gracefully instead of breaking discovery for everyone.

Fixes #283.

Changes

  • Extract a shared _iter_chemotools_modules() generator (used by all three public functions) that catches ImportError per submodule and emits a UserWarning naming the skipped module instead of propagating, a permanent safeguard, not just a one-off patch, so any future optional-dependency-gated subpackage degrades gracefully instead of crashing discovery.
  • Warning is raised with stacklevel=3 so it's attributed to the caller's own call site (e.g. their all_estimators() call) rather than to internal discovery-loop plumbing.
  • Add a regression test (test_discovery_skips_module_with_missing_optional_dependency) that fakes an ImportError from chemotools.plotting/chemotools.inspector to simulate matplotlib being absent, and asserts each discovery function still returns a list and warns instead of crashing.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens chemotools.utils.discovery so all_estimators(), all_displays(), and all_functions() no longer crash when importing a subpackage fails due to a missing optional dependency (e.g. matplotlib for viz), and instead skip the failing module with a warning.

Changes:

  • Add _iter_chemotools_modules() to centralize module iteration and handle per-module ImportError by warning and skipping.
  • Update all_estimators(), all_displays(), and all_functions() to use the shared iterator.
  • Add a regression test ensuring discovery warns and still returns a list when chemotools.plotting / chemotools.inspector can’t be imported.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
chemotools/utils/discovery.py Introduces a shared module iterator that catches ImportError during discovery and updates all discovery entrypoints to use it.
tests/utils/test_discovery.py Adds a regression test that simulates an ImportError from optional-dependency-gated subpackages and asserts discovery warns instead of crashing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread chemotools/utils/discovery.py Outdated
except ImportError as exc:
warnings.warn(
f"Skipping '{module_name}' during discovery: {exc}",
stacklevel=3,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stacklevel=3 is already correct

Tested directly, by calling all_estimators() from a notebook cell with a submodule forced to fail import:

stacklevel=3 (current):
...ipykernel_15920\875199054.py:21: Skipping 'chemotools.plotting' during discovery: ...
→ line 21 is the exact cell line that called all_estimators(). Correct.

stacklevel=4 (proposed):
...IPython\core\interactiveshell.py:3546: Skipping 'chemotools.plotting' during discovery: ...
→ overshoots past the caller entirely, into IPython's own cell-execution internals.

So stacklevel=4 is strictly worse, not a fix. Keeping stacklevel=3 as-is.

Comment thread tests/utils/test_discovery.py Outdated
@Gnpd
Gnpd requested review from paucablop and removed request for paucablop July 17, 2026 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: all_estimators() / all_displays() / all_functions() crash with ImportError when matplotlib is not installed

2 participants