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
$ 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
$ 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(
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 typedpkg_namespace.alpha.alpha_module import alpha_func
alpha_func(False)
alpha_func(2)
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"
$ 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"
Importing type hints from namespace packages only works when using the
from ... import ...form.Using the
--namespace-packagesflag works properly when thetypedpkg_namespace-alphais not installed butmypyis executed from the directory containing the package contents (as expected) but only for imports using thefrom ... import ...style.import ...does not work as expected. Likewise the same is true without the--namespace-packagesflag iftypedpkg_namespace-alphais 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
setup.pytypedpkg_namespace/alpha/alpha_module.pyfrom_imp.pynot_from_imp.pytypedpkg_namespace/alpha/__init__.pyandtypedpkg_namespace/alpha/py.typedare emptyRepro Example
--namespace-packages