Repository navigation
Fix #8217: Register public submodules in skimage2 namespace - #8291
ravikumar266 wants to merge 4 commits into
Conversation
|
Reviews (1): Last reviewed commit: "Fix #8217: Register public submodules in..." | Re-trigger Greptile |
74fe653 to
fbc5918
Compare
|
pre-commit.ci autofix |
589adfa to
43aeeb0
Compare
|
Reviews (2): Last reviewed commit: "Use modern find_spec/exec_module MetaPat..." | Re-trigger Greptile |
There was a problem hiding this comment.
Pull request overview
This PR fixes skimage2.<submodule> import failures by adding a lazy import mechanism that registers _skimage2.<submodule> modules under the skimage2.<submodule> names in sys.modules, so standard import statements work (not just attribute access via __getattr__).
Changes:
- Add a
sys.meta_pathfinder/loader to resolveskimage2.<submodule>imports to_skimage2.<submodule>lazily. - Update
skimage2.__getattr__to import and register public submodules insys.modules. - Add a regression test verifying
from skimage2.<submodule> import ...works and that private top-level submodules remain inaccessible.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/skimage2/init.py | Implements lazy import aliasing via __getattr__ + a sys.meta_path finder so skimage2.<submodule> imports resolve to _skimage2.<submodule>. |
| tests/skimage2/test_skimage2.py | Adds a test that exercises public skimage2.<submodule> imports and checks private top-level submodules are still not importable. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| "metrics", | ||
| "morphology", | ||
| "registration", | ||
| "segmentation", | ||
| "transform", |
| warnings.warn( | ||
| "Importing from the `skimage2` namespace is experimental. " | ||
| "Its API is under development and considered unstable!", | ||
| category=_skimage2.ExperimentalAPIWarning, | ||
| stacklevel=2, | ||
| ExperimentalAPIWarning, | ||
| ) |
|
|
||
| sys.meta_path.append(_Skimage2SubmoduleFinder()) | ||
|
|
Fixes scikit-imagegh-8217 -- skimage2.<submodule> import statements (e.g. 'from skimage2.filters import gaussian' or 'import skimage2.filters') previously raised ModuleNotFoundError, even though attribute access (skimage2.filters via __getattr__) worked fine. Python's import statement machinery bypasses module-level __getattr__ and looks up skimage2.filters directly in sys.modules, where it was never registered. This replaces the earlier eager for-loop approach (flagged in review for eagerly importing every public submodule, including skimage2.io and its plugin/imageio initialization) with a sys.meta_path finder that lazily registers _skimage2.<submodule> under the skimage2.<submodule> name in sys.modules only when actually imported. Uses the modern find_spec/exec_module loader protocol (PEP 451); the older find_module/load_module API used in an earlier iteration is no longer invoked by Python's import machinery and silently did nothing. Also includes a whitespace-only pre-commit autofix to tests/skimage2/test_skimage2.py. Verified against the full existing tests/skimage2/ suite (9514 passed) and the existing test_skimage2_submodule_imports regression test, which already covered this exact scenario including that private submodules remain inaccessible. Confirmed the doctest collection error on this file's module-level ExperimentalAPIWarning is pre-existing (reproduces identically on the unmodified file) and unrelated to this change.
43aeeb0 to
21b6735
Compare
|
Reviews (3): Last reviewed commit: "Use modern find_spec/exec_module MetaPat..." | Re-trigger Greptile |
| def create_module(self, spec): | ||
| submod_name = spec.name[len(self._prefix) :] | ||
| mod = importlib.import_module(f"_skimage2.{submod_name}") | ||
| globals()[submod_name] = mod | ||
| return mod |
There was a problem hiding this comment.
Alias corrupts canonical module metadata
When skimage2.filters is imported, create_module returns the existing _skimage2.filters object for the alias spec. Importlib then associates that shared object with the skimage2.filters spec and no-op loader, so canonical introspection reports alias metadata and reloading _skimage2.filters no longer re-executes its real loader.
|
See also #8310 |
Summary
Fixes #8217 —
skimage2.<submodule>import statements (e.g.from skimage2.filters import gaussianorimport skimage2.filters)previously raised
ModuleNotFoundError, even though attribute access(
skimage2.filtersvia__getattr__) worked fine.Root cause
Python's import statement machinery bypasses module-level
__getattr__and looks up
skimage2.filtersdirectly insys.modules, where it wasnever registered.
Approach
@greptile-apps flagged that an earlier version of this PR eagerly
imported every public submodule on a plain
import skimage2,including
skimage2.ioand its plugin/imageio initialization. Thisversion implements their suggested Option 2 instead: a
sys.meta_pathfinder (_Skimage2SubmoduleFinder) that lazilyregisters the real
_skimage2.<submodule>under theskimage2.<submodule>name insys.modules, only when actuallyimported.
It uses the modern
find_spec/exec_moduleloader protocol (PEP 451)rather than the older
find_module/load_moduleAPI, since thelatter is no longer invoked by current Python's import machinery.
Review feedback addressed
@copilotai's review caught three issues, all now fixed:
restorationwas missing from__all__, soskimage2.restorationwould still have raised
ModuleNotFoundErrordespite this PR's goal.Verified against
_skimage2's actual public submodules viapkgutil.iter_modulesand confirmed__all__now matches exactly.stacklevel=2was accidentally dropped from the experimental-APIwarning during refactoring, which would have pointed the warning at
skimage2/__init__.pyinstead of the user's import site. Restored.sys.meta_path.append(...)only runs if an instance of_Skimage2SubmoduleFinderisn't already registered.Testing
tests/skimage2/suite: 9514 passed, 35 skipped(optional deps), 97 xfailed (optional deps) — no regressions
test_skimage2_submodule_importstest (whichalready covered this exact scenario, including that private
submodules like
skimage2._sharedremain inaccessible) now passesimport skimage2(
sys.modulesshows noskimage2.*or_skimage2.ioentries)skimage2.restorationnow imports correctly(
from skimage2.restoration import denoise_tv_chambolle)the user's import site, not inside
skimage2/__init__.pyExperimentalAPIWarningis pre-existing and reproduces identicallyon the unmodified file — unrelated to this change
this branch (suppressing a redundant warning re-check in
test_skimage2_submodule_imports, since warning-firing behavior isalready covered by
test_import_skimage2_warning's subprocess-basedtest)
Note
Used an LLM for research assistance during investigation and
implementation (diagnosing why the original
find_module/load_moduleapproach silently didn't work under the current Python version, and
drafting the
find_spec/exec_modulereplacement). All findings andbehavior were independently verified locally before being acted on.