You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Relative import that climbs past the top-level package reports a bogus module named ".foo" instead of "No parent module" #22539
When a relative import climbs past the top-level package and names a module after the dots, mypy reports a module named ".foo" (with a leading dot) as missing, instead of reporting that the relative import climbs too far.
To Reproduce
Project layout:
foo.py (contains: bar = 1)
pkg/__init__.py (empty)
pkg/mod.py (contains: from ..foo import bar)
Run mypy pkg/mod.py from the project root. Note that foo.py exists at the top level, so the module is not genuinely missing.
The same happens for other forms that climb too far:
a top-level a.py containing from .foo import bar
pkg/sub/mod.py containing from ...foo import bar
Expected Behavior
Mypy should report that the relative import cannot be performed, the same way it already does for the form without a module name after the dots. For example, pkg/mod.py containing from .. import foo reports:
pkg/mod.py:1: error: No parent module -- cannot perform relative import
That matches Python itself, which raises ImportError: attempted relative import beyond top-level package for from ..foo import bar in pkg/mod.py, and ImportError: attempted relative import with no known parent package for from .foo import bar in a top-level file.
Actual Behavior
pkg/mod.py:1: error: Cannot find implementation or library stub for module named ".foo" [import-not-found]
pkg/mod.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
The reported module name ".foo" can never exist, and the note sends the user to the missing-imports docs, which is the wrong fix for this code.
Your Environment
Mypy version used: 2.5.0+dev.1572a18848d181f5cd91877aa3ab4e1644b14f20 (compiled: no)
Mypy command-line flags: --no-incremental
Mypy configuration options from mypy.ini (and other config files): none
Python version used: 3.12.3
Extra notes
The cause looks like the name-joining step in mypy/build.py, in State.correct_rel_imp:
new_id=file_id+"."+imp.idifimp.idelsefile_id
When the import climbs past the top level, file_id has already been stripped to "", so new_id becomes ".foo". That value is truthy, so the if not new_id guard below it, which reports "No parent module -- cannot perform relative import", never fires.
The same pattern is in mypy/util.py, in correct_relative_import, where the ok check (len(parts) >= rel) stays True even when stripping leaves an empty base, and the return value joins the empty base with "." + target, giving ".foo".
A valid relative import is unaffected: pkg/sub/mod.py containing from ..foo import bar still resolves to pkg.foo correctly.
Hi, I'd like to work on this one.
Reproduced it on master at 1572a18, Windows, Python 3.12.10. All three forms report ".foo", while from .. import foo gives the right error.
$ mypy --no-incremental pkg/mod.py # from ..foo import bar
pkg\mod.py:1: error: Cannot find implementation or library stub for module named ".foo" [import-not-found]
$ mypy --no-incremental pkg/mod2.py # from .. import foo
pkg\mod2.py:1: error: No parent module -- cannot perform relative import
I think the cause is State.correct_rel_imp in mypy/build.py. When the import climbs past the top, file_id is "", so line 1188 builds ".foo", and the if not new_id check below it never fires:
new_id=file_id+"."+imp.idifimp.idelsefile_id
Should correct_relative_import in mypy/util.py get the same fix in this PR, or should I keep it to build.py?
Used Claude Code (opus 5.5) to help understand repo structure and reproduce this and find the code path. I checked the results myself.
I'll add test cases in semanal-errors.test, and report back here within a few days.
Activity
JadeJaguar commentedon Nov 23, 2026
Hi, I'd like to work on this one.
Reproduced it on master at 1572a18, Windows, Python 3.12.10. All three forms report ".foo", while from .. import foo gives the right error.
I think the cause is State.correct_rel_imp in mypy/build.py. When the import climbs past the top, file_id is "", so line 1188 builds ".foo", and the if not new_id check below it never fires:
Should correct_relative_import in mypy/util.py get the same fix in this PR, or should I keep it to build.py?
Used Claude Code (opus 5.5) to help understand repo structure and reproduce this and find the code path. I checked the results myself.
I'll add test cases in semanal-errors.test, and report back here within a few days.