Skip to content

stubtest: don't report re-exported submodules as missing from the stub#21574

Open
ShirGanon wants to merge 1 commit into
python:masterfrom
ShirGanon:fix/stubtest-reexported-submodules
Open

stubtest: don't report re-exported submodules as missing from the stub#21574
ShirGanon wants to merge 1 commit into
python:masterfrom
ShirGanon:fix/stubtest-reexported-submodules

Conversation

@ShirGanon

Copy link
Copy Markdown

Fixes #21328

When a package's __init__ binds a submodule as an attribute at runtime — e.g. via from .submod import * (or simply from . import submod) — and lists it in __all__, stubtest reported the submodule as missing from the stub, even though the submodule has its own stub file and is verified separately. This produced two spurious diagnostics per submodule:

error: pkg.submod is not present in stub
error: pkg.__all__ names exported from the stub do not correspond to the names exported at runtime
  Names exported at runtime but not in the stub: ['submod']

Python's import system binds an imported submodule as an attribute of its parent package, so listing such a submodule (which has its own stub) in the package's __all__ is correct, and stubtest shouldn't flag it.

Changes

  • Added _get_stub_submodules() to find the direct submodules of a package that have their own stub (present in _all_stubs).
  • In verify_mypyfile, skip reporting a runtime submodule as "not present in stub" when it has its own stub file (it's verified separately).
  • In _verify_exported_names, exclude such submodules from the runtime-vs-stub __all__ comparison.

Submodules without a corresponding stub are still reported as before, so this only suppresses genuine false positives.

Testing

  • Added test_reexported_submodule_in_all in mypy/test/teststubtest.py, which builds a package whose __init__ imports a submodule and lists it in __all__, and asserts stubtest no longer reports it as missing.
  • Confirmed the new test fails without the stubtest.py change (it reports submod is not present in stub) and passes with it.
  • Full mypy/test/teststubtest.py suite passes (67 passed), no regressions.
  • ruff check, black, and mypy self-check on mypy/stubtest.py are clean.

Disclosure: drafted with Claude Code; I reviewed, understand, and tested the change locally.

When a package binds a submodule as an attribute at runtime (e.g. via
`from .submod import *` in `__init__`, or by importing it) and lists it in
`__all__`, stubtest reported the submodule as "not present in stub" and as
an `__all__` mismatch, even though the submodule has its own stub file and
is verified separately. This produced one spurious diagnostic per submodule.

Skip reporting such submodules (those present in `_all_stubs`) as missing
from the parent package's stub, and exclude them from the `__all__`
runtime-vs-stub comparison.

Fixes python#21328

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant