Mypy does not recognize pkg_resources-style namespace packages
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy is not able to understand pkg_resources-style namespace packages. Although, this is not the recommended namespace mechanism, it is still supported in Python for legacy compatibility. Especially as the documentation does not recommend to mix this method with PEP420 namespace.
To Reproduce
A project with such structure:
./root/
__init__.py # Namespace
core/
action.py
__init__.py
./extensions/
root/
__init__.py # Namespace
extra/
__init__.py
extra_action.py
where inside __init__.py as namespace, you have:
__import__("pkg_resources").declare_namespace(__name__)
Expected Behavior
$> mypy .
Success: no issues found in 6 source files
Actual Behavior
$> mypy .
root/__init__.py: error: Duplicate module named "root" (also at "./extensions/root/__init__.py")
root/__init__.py: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
root/__init__.py: note: Common resolutions include: a) using `--exclude` to avoid checking one of them, b) adding `__init__.py` somewhere, c) using `--explicit-package-bases` or adjusting MYPYPATH
Found 1 error in 1 file (errors prevented further checking)
Note: If all the __init__.py as namespace are removed such that it falls back to PEP420 namespace then mypy is happy:
$> mypy .
Success: no issues found in 4 source files
Your Environment
- Mypy version used:
990 - Mypy command-line flags:
mypy . - Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.9.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running mypy . against the reproduced root and extensions directory, focusing on the two namespace __init__.py files that call pkg_resources.declare_namespace. Trace mypy's module-path mapping for these files and add coverage for the reported layout; done means the command no longer reports a duplicate root module.
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
- 45/100