Skip to content

Report no parent module for relative imports that climb past the top level - #22540

Open
JadeJaguar wants to merge 2 commits into
python:masterfrom
JadeJaguar:fix/22539-relative-import-past-top
Open

JadeJaguar wants to merge 2 commits into
python:masterfrom
JadeJaguar:fix/22539-relative-import-past-top

Conversation

@JadeJaguar

Copy link
Copy Markdown

Fixes #22539

A relative import that climbs past the top-level package and names a module after the dots (for example from ..foo import bar in pkg/mod.py) made mypy report a missing module named ".foo". With this change it reports No parent module -- cannot perform relative import, the same error mypy already gives for from .. import foo.

What changed

Two files:

  • mypy/build.py, State.correct_rel_imp (one line). When the import climbs past the top, file_id is stripped to "", and the old join file_id + "." + imp.id built ".foo". That string is not empty, so the if not new_id check right below it never reported the error. The name is now joined only when file_id is not empty, so new_id stays "" and the existing check reports it. Valid relative imports are unchanged, because their file_id is never empty.
  • test-data/unit/semanal-errors.test: three new cases next to testRelativeImportAtTopLevelModule2: top-level from .m import x, top-level from ..m import x, and from ..m import x inside pkg/mod.py. Each has a real m.py, so the module is not actually missing.

I left correct_relative_import in mypy/util.py as 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:

$ 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]
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)

After:

$ mypy --no-incremental pkg/mod.py    # from ..foo import bar
pkg\mod.py:1: error: No parent module -- cannot perform relative import
Found 1 error in 1 file (errors prevented further checking)

The other two forms from the issue (from .foo import bar in a top-level file and from ...foo import bar in pkg/sub/mod.py) change the same way. A valid from ..foo import bar in pkg/sub/ok.py still resolves to pkg.foo.

Tests

The three new cases fail on master (module named ".m") and pass with the change:

$ python -m pytest -q -n0 mypy/test/testsemanal.py -k RelativeImportFrom
4 passed, 576 deselected

$ python -m pytest -q -n 4 mypy/test/testsemanal.py mypy/test/testcheck.py::TypeCheckSuite::check-modules.test
829 passed

$ python -m pytest -q -n 4 mypy/test/testsemanal.py mypy/test/testcheck.py::TypeCheckSuite::check-modules.test mypy/test/testcheck.py::TypeCheckSuite::check-incremental.test mypy/test/testfinegrained.py mypy/test/testgraph.py
1954 passed, 32 skipped

$ python -m mypy --config-file mypy_self_check.ini -p mypy
Success: no issues found in 196 source files

$ pre-commit run --files mypy/build.py test-data/unit/semanal-errors.test
black, ruff check, codespell, whitespace: Passed

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.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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.

Relative import that climbs past the top-level package reports a bogus module named ".foo" instead of "No parent module"

1 participant