8000 Importing types from namespace packages emits false positives without `from ... import ...` · Issue #5758 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
Importing types from namespace packages emits false positives without from ... import ... #5758
Closed
@chrisphilip322

Description

@chrisphilip322

Importing type hints from namespace packages only works when using the from ... import ... form.

Using the --namespace-packages flag works properly when the typedpkg_namespace-alpha is not installed but mypy is executed from the directory containing the package contents (as expected) but only for imports using the from ... import ... style. import ... does not work as expected. Likewise the same is true without the --namespace-packages flag if typedpkg_namespace-alpha is installed.

Specifically, the type hints are correctly imported but a false positive error message is still emitted of Cannot find module named 'typedpkg_namespace'.

Repro steps

  • Virtual env state
$ pip list
Package         Version
--------------- --------------------------------------------------
mypy            0.640+dev.e12be3ba9e6be3e9242e3a81ffaa9b3a136876fe
mypy-extensions 0.4.1
pip             18.1
setuptools      40.4.3
typed-ast       1.1.0
  • Repro file structure
$ find . \( -path ./.mypy_cache -o -path ./venv \) -prune -o -print
.
./typedpkg_namespace
./typedpkg_namespace/alpha
./typedpkg_namespace/alpha/__init__.py
./typedpkg_namespace/alpha/py.typed
./typedpkg_namespace/alpha/alpha_module.py
./not_from_imp.py
./from_imp.py
./setup.py
  • Repro files
    • setup.py
    setup(
        name='typedpkg_namespace.alpha',
        version='1.0.0',
        packages=find_namespace_packages(),
        namespace_packages=['typedpkg_namespace'],
        zip_safe=False,
        package_data={'typedpkg_namespace.alpha': ['py.typed']}
    )
    
    • typedpkg_namespace/alpha/alpha_module.py
    def alpha_func(a: bool) -> bool:
        return not a
    
    • from_imp.py
    from typedpkg_namespace.alpha.alpha_module import alpha_func
    alpha_func(False)
    alpha_func(2)
    
    • not_from_imp.py
    import typedpkg_namespace.alpha.alpha_module as am; alpha_func = am.alpha_func
    alpha_func(False)
    alpha_func(2)
    
    • typedpkg_namespace/alpha/__init__.py and typedpkg_namespace/alpha/py.typed are empty

Repro Example

  • using --namespace-packages
$ mypy from_imp.py  --namespace-packages
from_imp.py:3: error: Argument 1 to "alpha_func" has incompatible type "int"; expected "bool"

$ mypy not_from_imp.py --namespace-packages
not_from_imp.py:1: error: Cannot find module named 'typedpkg_namespace'
not_from_imp.py:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
not_from_imp.py:3: error: Argument 1 has incompatible type "int"; expected "bool"
  • using installed package
$ pip install .
Looking in indexes: http://artifactory.factset.com/artifactory/api/pypi/python/simple/
Processing /home/user/cphilip/side_projects/test_namespace/typedpkg_namespace-alpha
Installing collected packages: typedpkg-namespace.alpha
  Running setup.py install for typedpkg-namespace.alpha ... done
Successfully installed typedpkg-namespace.alpha-1.0.0

$ pip list
Package                  Version
------------------------ --------------------------------------------------
mypy                     0.640+dev.e12be3ba9e6be3e9242e3a81ffaa9b3a136876fe
mypy-extensions          0.4.1
pip                      18.1
setuptools               40.4.3
typed-ast                1.1.0
typedpkg-namespace.alpha 1.0.0

$ mypy from_imp.py
from_imp.py:3: error: Argument 1 to "alpha_func" has incompatible type "int"; expected "bool"

$ mypy not_from_imp.py
not_from_imp.py:1: error: Cannot find module named 'typedpkg_namespace'
not_from_imp.py:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
not_from_imp.py:3: error: Argument 1 has incompatible type "int"; expected "bool"

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0