mypy throws an error when I have an editable installation (pip install -e .
) of my library. It works fine with the non-editable installation (pip install .
).
I was able to reproduce it with a toy example, so here are the files:
main.py
def something() -> None:
print("I am something")
second.py
from my_ns.mylib.main import something
def something_else() -> None:
something()
print("I am something else")
pyproject.toml
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"
[project]
name = "mylib"
requires-python = ">=3.10"
dependencies = [
"pyyaml",
]
version = "0.1.0"
[project.optional-dependencies]
test = ["mypy",]
[tool.setuptools.packages.find]
where = ["src"]
[tool.setuptools.package-data]
"*" = ["*py.typed"]
mypy.ini
[mypy]
namespace_packages = True
explicit_package_bases = True
exclude = (?x)(
^tests/ # ignore everything in tests directory
| ^test/ # ignore everything in test directory
| ^setup.py$ # ignore root's setup.py
)
# The following flags are equivalent to "--strict"
# Basic checks
warn_unused_configs = True
warn_redundant_casts = True
warn_unused_ignores = True
strict_equality = True
extra_checks = True
check_untyped_defs = True
disallow_untyped_calls = True
disallow_incomplete_defs = True
disallow_untyped_defs = True
no_implicit_reexport = True
# These shouldn't be too much additional work, but may require more manual "ignores" for untyped imports
disallow_subclassing_any = True
disallow_untyped_decorators = True
disallow_any_generics = True
warn_return_any = True
my_ns
is a namespace package, so it does by intention not include a __init__.py
(and must remain a namespace).
This is the result when running mypy 1.10.0:
$ mypy --config-file mypy.ini .
src/my_ns/mylib/main.py: error: Source file found twice under different module names: "src.my_ns.mylib.main" and "my_ns.mylib.main"
Found 1 error in 1 file (errors prevented further checking)
How can I make mypy work with an editable install and support namespace packages?