pip install -e .
) 时,mypy 会抛出错误。它适用于不可编辑的安装(
pip install .
)。
我能够用一个玩具示例重现它,所以这里是文件:
.
├── src
│ └── my_ns
│ └── mylib
│ ├── __init__.py
│ ├── main.py
│ ├── py.typed
│ └── second.py
├── mypy.ini
└── pyproject.toml
main.py
def something() -> None:
print("I am something")
第二.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"
version = "0.1.0"
[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
)
my_ns
是一个命名空间包,因此它有意不包含 __init__.py
(并且必须保留命名空间)。
这是运行 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)
如何使 mypy 使用可编辑的安装和支持命名空间包?