numpy mypy与`disallow_any_generics = false`仅适用于某些模块

lp0sw83n  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(115)

我想在我的代码库中逐步引入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

但它并没有像我希望的那样起作用。我想要的甚至可能吗?

相关问题