python / python/mypy

Namespace packages and __init__ imports

Open
#9,716 2 comments 1 reaction 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

--namespace-packages runs into an issue if we import from __init__.py explicitly.

For instance, given:

.
└── proj
    ├── __init__.py
    └── a.py

$ cat proj/__init__.py 
x: int

$ cat proj/a.py       
from .__init__ import x

$ cat proj/b.py
from proj.__init__ import x

running mypy will result in:

$ mypy proj --namespace-packages
proj/__init__.py: error: Source file found twice under different module names: 'proj' and 'proj.__init__'

This issue comes up at least twice in mypy_primer's corpus.

This is correct in that you do get two entries in sys.modules, but undesirable for this to block type checking

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 error with the shown proj/init.py, proj/a.py, and proj/b.py files using mypy proj --namespace-packages. Start by tracing namespace-package module discovery and how explicit __init__ imports are recorded. Done means this example no longer reports the duplicate source-file error while still type-checking the imports.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.