Background
I work in a large Python3 repo that is type checked with Mypy. In accordance with PEP420 we do not use __init__.py
files.
Mypy recently added support for implicit namespace packages, but the implementation now follows the chain of import
statements from a given entrypoint. In the case of this repo, (a Django app) there are many modules that are imported dynamically (like middleware) or not imported at all (tests and one-off scripts).
Consider this example:
my-py3-repo/
├── hello/
│ ├── services/
│ │ └── hello_service.py
│ └── hello.py
├── scripts/
│ ├── db/
│ │ └── migrate.py
│ └── manage.py
└── tests/
└── hello/
├── services/
│ └── hello_service_test.py
└── hello_test.py
Assuming that hello.py
imports hello_service.py
, everything under the hello
namespace will be type checked as expected with mypy ./hello
.
Problem
However test discovery with pytest
, nose
, django
et al works differently and hello_test.py
would not usually import hello_service_test.py
. There is currently no way for Mypy to discover hello_service_test.py
with mypy ./tests
(if not using __init__.py
).
Similarly, everything under the scripts
directory would suffer the same problem.
Question
How can I configure Mypy such that the scripts
and tests
directories are always type checked?
Configuration
# Pipfile
[dev-packages]
mypy = "==0.670"
[requires]
python_version = "3.7"
# setup.cfg
[mypy]
python_version = 3.7
ignore_missing_imports = True
namespace_packages = True
See also this issue I created in the Mypy repo.