1

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?

boberto
  • 151
  • 8
  • `[[tool.mypy.overrides]]` `module` defines the *module* of *your* code where some overrides should be applied. It does not point to "for `numpy` args, allow generic type omission". What you want is not possible with `mypy` (and any other type checker), because the use cases are pretty rare. You may open a feature request for that on `mypy` issue tracker. Check out [this issue](https://github.com/python/mypy/issues/7468) and [this PR](https://github.com/python/mypy/pull/8108) (follow the links). It was considered, but not implemented yet. – STerliakov Mar 22 '23 at 13:40
  • Does this answer your question? [Type hinting / annotation (PEP 484) for numpy.ndarray](https://stackoverflow.com/questions/35673895/type-hinting-annotation-pep-484-for-numpy-ndarray) – Yogev Neumann May 09 '23 at 03:06
  • Yes, but in the spirit of allowing to add types gradually, I'd like to ignore the error for `np.ndarray` at first and then go in and give more accurate type hints in incremental steps. – boberto May 10 '23 at 06:38

1 Answers1

0

have you already taken a look at it here:

Type hinting / annotation (PEP 484) for numpy.ndarray

Yogev Neumann
  • 2,099
  • 2
  • 13
  • 24
Magaren
  • 192
  • 8
  • Yes, but in the spirit of allowing to add types _gradually_, I'd like to ignore the error for `np.ndarray` at first and then go in and give more accurate type hints in incremental steps. – boberto Mar 24 '23 at 06:20