我想在我的代码库中逐步引入mypy
的类型检查。我使用numpy
,并一直在将np.ndarray
添加到我的函数中,作为NumPy数组的类型提示:
def foo(a: np.ndarray, b: dict[str, complex]) -> np.ndarray:
在我的pyproject.toml
中使用以下mypy
配置:
[tool.mypy]
python_version = "3.9"
plugins = [
"numpy.typing.mypy_plugin"
]
check_untyped_defs = true
disallow_any_generics = true
disallow_untyped_defs = true
implicit_reexport = false
warn_unused_ignores = true
warn_redundant_casts = true
pretty = true
show_column_numbers = true
show_error_codes = true
show_error_context = true
show_traceback = true
该注解正确地导致了错误:
src/mypy_numpy/foo.py: note: In function "foo":
src/mypy_numpy/foo.py:4: error: Missing type parameters for generic type "ndarray" [type-arg]
def foo(a: np.ndarray, b: dict[str, complex]) -> np.ndarray:
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 3 source files)
不过,我想忽略np.ndarray
注解的错误 just,这样我就可以为这些NumPy对象增量添加更精确的注解。我已经尝试了以下配置:
[tool.mypy]
python_version = "3.9"
plugins = [
"numpy.typing.mypy_plugin"
]
check_untyped_defs = true
disallow_any_generics = true
disallow_untyped_defs = true
implicit_reexport = false
warn_unused_ignores = true
warn_redundant_casts = true
pretty = true
show_column_numbers = true
show_error_codes = true
show_error_context = true
show_traceback = true
[[tool.mypy.overrides]]
module = "numpy"
disallow_any_generics = false
但它并没有像我希望的那样起作用。我想要的甚至可能吗?
1条答案
按热度按时间rqqzpn5f1#
你已经看过了这里: