Repository navigation
Report no parent module for relative imports that climb past the top level - #22540
Open
JadeJaguar wants to merge 2 commits into
Open
JadeJaguar wants to merge 2 commits into
JadeJaguar wants to merge 2 commits into
Conversation
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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 #22539
A relative import that climbs past the top-level package and names a module after the dots (for example
from ..foo import barinpkg/mod.py) made mypy report a missing module named".foo". With this change it reportsNo parent module -- cannot perform relative import, the same error mypy already gives forfrom .. import foo.What changed
Two files:
mypy/build.py,State.correct_rel_imp(one line). When the import climbs past the top,file_idis stripped to"", and the old joinfile_id + "." + imp.idbuilt".foo". That string is not empty, so theif not new_idcheck right below it never reported the error. The name is now joined only whenfile_idis not empty, sonew_idstays""and the existing check reports it. Valid relative imports are unchanged, because theirfile_idis never empty.test-data/unit/semanal-errors.test: three new cases next totestRelativeImportAtTopLevelModule2: top-levelfrom .m import x, top-levelfrom ..m import x, andfrom ..m import xinsidepkg/mod.py. Each has a realm.py, so the module is not actually missing.I left
correct_relative_importinmypy/util.pyas it is. It has the same join, but after this change the blocker error stops checking before semanal reaches it. I asked on the issue whether you want it changed too, and can add that here if so.Before and after
Issue layout (top-level
foo.py,pkg/__init__.py), master at 1572a18, Windows, Python 3.12.10:Before:
After:
The other two forms from the issue (
from .foo import barin a top-level file andfrom ...foo import barinpkg/sub/mod.py) change the same way. A validfrom ..foo import barinpkg/sub/ok.pystill resolves topkg.foo.Tests
The three new cases fail on master (
module named ".m") and pass with the change:I did not run the full suite locally; CI will.
AI use
I used Claude Code (claude opus 5.5) for this PR. It wrote the one-line change and the three test cases. I read the change and the tests, checked the before and after output myself.