stubtest: don't report re-exported submodules as missing from the stub#21574
Open
ShirGanon wants to merge 1 commit into
Open
stubtest: don't report re-exported submodules as missing from the stub#21574ShirGanon wants to merge 1 commit into
ShirGanon wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #21328
When a package's
__init__binds a submodule as an attribute at runtime — e.g. viafrom .submod import *(or simplyfrom . 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: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
_get_stub_submodules()to find the direct submodules of a package that have their own stub (present in_all_stubs).verify_mypyfile, skip reporting a runtime submodule as "not present in stub" when it has its own stub file (it's verified separately)._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
test_reexported_submodule_in_allinmypy/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.stubtest.pychange (it reportssubmod is not present in stub) and passes with it.mypy/test/teststubtest.pysuite passes (67 passed), no regressions.ruff check,black, and mypy self-check onmypy/stubtest.pyare clean.Disclosure: drafted with Claude Code; I reviewed, understand, and tested the change locally.