I would like to introduce type checking with mypy
gradually into my codebase. I use numpy
and have been adding np.ndarray
to my functions as type hint for NumPy arrays:
def foo(a: np.ndarray, b: dict[str, complex]) -> np.ndarray:
With the following mypy
configuration in my pyproject.toml
:
[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
that annotation leads, correctly, to the error:
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)
I would like to ignore that error just for the np.ndarray
annotation though, so I can incrementally add more exact annotations for theses NumPy objects. I've tried the following configuration:
[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
but it has not worked as I hoped. Is what I want even possible?