Skip to content

Fix #8217: Register public submodules in skimage2 namespace - #8291

Closed
ravikumar266 wants to merge 4 commits into
scikit-image:mainfrom
ravikumar266:fix-8217-skimage2-submodules
Closed

ravikumar266 wants to merge 4 commits into
scikit-image:mainfrom
ravikumar266:fix-8217-skimage2-submodules

Conversation

@ravikumar266

@ravikumar266 ravikumar266 commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #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.

Root cause

Python's import statement machinery bypasses module-level __getattr__
and looks up skimage2.filters directly in sys.modules, where it was
never registered.

Approach

@greptile-apps flagged that an earlier version of this PR eagerly
imported every public submodule on a plain import skimage2,
including skimage2.io and its plugin/imageio initialization. This
version implements their suggested Option 2 instead: a
sys.meta_path finder (_Skimage2SubmoduleFinder) that lazily
registers the real _skimage2.<submodule> under the
skimage2.<submodule> name in sys.modules, only when actually
imported.

It uses the modern find_spec/exec_module loader protocol (PEP 451)
rather than the older find_module/load_module API, since the
latter is no longer invoked by current Python's import machinery.

Review feedback addressed

@copilotai's review caught three issues, all now fixed:

  • restoration was missing from __all__, so skimage2.restoration
    would still have raised ModuleNotFoundError despite this PR's goal.
    Verified against _skimage2's actual public submodules via
    pkgutil.iter_modules and confirmed __all__ now matches exactly.
  • stacklevel=2 was accidentally dropped from the experimental-API
    warning during refactoring, which would have pointed the warning at
    skimage2/__init__.py instead of the user's import site. Restored.
  • Duplicate finder registration on module reload — added a guard so
    sys.meta_path.append(...) only runs if an instance of
    _Skimage2SubmoduleFinder isn't already registered.

Testing

  • Full existing tests/skimage2/ suite: 9514 passed, 35 skipped
    (optional deps), 97 xfailed (optional deps) — no regressions
  • The pre-existing test_skimage2_submodule_imports test (which
    already covered this exact scenario, including that private
    submodules like skimage2._shared remain inaccessible) now passes
  • Confirmed no eager submodule loading on a plain import skimage2
    (sys.modules shows no skimage2.* or _skimage2.io entries)
  • Confirmed skimage2.restoration now imports correctly
    (from skimage2.restoration import denoise_tv_chambolle)
  • Confirmed the warning's reported location now correctly points at
    the user's import site, not inside skimage2/__init__.py
  • Confirmed the doctest-collection error on this file's module-level
    ExperimentalAPIWarning is pre-existing and reproduces identically
    on the unmodified file — unrelated to this change
  • Re-applied the CI test-isolation fix from an earlier iteration of
    this branch (suppressing a redundant warning re-check in
    test_skimage2_submodule_imports, since warning-firing behavior is
    already covered by test_import_skimage2_warning's subprocess-based
    test)

Note

Used an LLM for research assistance during investigation and
implementation (diagnosing why the original find_module/load_module
approach silently didn't work under the current Python version, and
drafting the find_spec/exec_module replacement). All findings and
behavior were independently verified locally before being acted on.

@greptile-apps

greptile-apps Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Fix #8217: Register public submodules in..." | Re-trigger Greptile

Comment thread src/skimage2/__init__.py Resolved
@ravikumar266
ravikumar266 force-pushed the fix-8217-skimage2-submodules branch 2 times, most recently from 74fe653 to fbc5918 Compare August 23, 2026 09:11
@ravikumar266

Copy link
Copy Markdown
Contributor Author

pre-commit.ci autofix

@ravikumar266
ravikumar266 force-pushed the fix-8217-skimage2-submodules branch from 589adfa to 43aeeb0 Compare August 30, 2026 19:46
@ravikumar266 ravikumar266 reopened this Aug 30, 2026
@ravikumar266
ravikumar266 marked this pull request as ready for review August 30, 2026 19:53
Copilot AI lite review requested due to automatic review settings August 30, 2026 19:53
@greptile-apps

greptile-apps Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "Use modern find_spec/exec_module MetaPat..." | Re-trigger Greptile

Copilot AI left a comment

Copy link
Copy Markdown

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 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_path finder/loader to resolve skimage2.<submodule> imports to _skimage2.<submodule> lazily.
  • Update skimage2.__getattr__ to import and register public submodules in sys.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.

Comment thread src/skimage2/__init__.py
Comment on lines +22 to +26
"metrics",
"morphology",
"registration",
"segmentation",
"transform",
Comment thread src/skimage2/__init__.py
Comment on lines 85 to 89
warnings.warn(
"Importing from the `skimage2` namespace is experimental. "
"Its API is under development and considered unstable!",
category=_skimage2.ExperimentalAPIWarning,
stacklevel=2,
ExperimentalAPIWarning,
)
Comment thread src/skimage2/__init__.py
Comment on lines +82 to +84

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.
@ravikumar266
ravikumar266 force-pushed the fix-8217-skimage2-submodules branch from 43aeeb0 to 21b6735 Compare August 30, 2026 20:17
@greptile-apps

greptile-apps Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Reviews (3): Last reviewed commit: "Use modern find_spec/exec_module MetaPat..." | Re-trigger Greptile

Comment thread src/skimage2/__init__.py
Comment on lines +72 to +76
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

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.

P1 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.

@stefanv

stefanv commented Sep 1, 2026

Copy link
Copy Markdown
Member

See also #8310

@ravikumar266

Copy link
Copy Markdown
Contributor Author

Thanks @stefanv for taking this further with a more comprehensive
approach! Happy to close this PR in favor of #8310 if that's the
preferred direction — let me know.

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.

skimage2 doesn't support submodule imports like _skimage2

3 participants