python / python/mypy

Mypy does not recognize pkg_resources-style namespace packages

Open
#14,052 0 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

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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.