python / python/mypy

Incorrect handling of `import from` namespace package that spans site-packages and the source tree

Open
#14,822 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

The following command:

mypy --explicit-package-bases ns_package/sub_ns_package1/test.py

Resulted in

ns_package/sub_ns_package1/test.py:1: error: Module "ns_package" has no attribute "sub_ns_package2"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

With the following setup:

Source code tree:

ns_package
    sub_ns_package1
        test.py

(note that there is no __init__.py)

site-packages tree (partial):

ns_package
    sub_ns_package2
        module.py    

(same, no __init__.py)

test.py contents:

from ns_package import sub_ns_package2

While scratching my head I also found the following:

  • import sub_ns_package.ns_package2 works.
  • it also works if the above import appears together with from ns_package import sub_ns_package2

To Reproduce
See the above.

Expected Behavior

mypy should finish happily in the above example.

Actual Behavior

ns_package/sub_ns_package1/test.py:1: error: Module "ns_package" has no attribute "sub_ns_package2"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.0.1
  • Mypy command-line flags: --explicit-package-bases (tried with or without it) ; --namespace-packages (tried with or without it)
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.8.16

Potential Fix
I have very little knowledge about the codebase, but the following patch would make the problem go away. I'm not sure it's the right fix though.

https://github.com/python/mypy/commit/e69bbd2c82e60640171dd7e2653c010976ad4239

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report with the shown namespace-package tree and mypy --explicit-package-bases ns_package/sub_ns_package1/test.py; then inspect the referenced commit e69bbd2c82e60640171dd7e2653c010976ad4239. Add regression coverage for the split source-tree/site-packages case, and verify the command finishes without the attr-defined error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.