← Back to Pull requests

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

Open
unknown opened this pull request with 416 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 23, 2026 01:36 UTC
Author

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 1572a1884, 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.

    Diff: 416 lines (+312 / -104) across 5 files Reviewer: chrikrah Review duration: 2215m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.45) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 416 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.361
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #22540"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.0% (MEDIUM) · Higher = larger blast radius
    45.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.6% · Safe to merge
    25.6%
    Repository