Description
To Reproduce
from typing import Any
import numpy as np
import numpy.typing as npt
def plus1(array: npt.NDArray[np.integer[Any]]) -> None:
array += 1
Expected Behavior
No error
Actual Behavior
error: Invalid self argument "ndarray[tuple[int, ...], dtype[integer[Any]]]" to attribute function "iadd" with type "Callable[[ndarray[tuple[int, ...], dtype[numpy.bool[builtins.bool]]], _SupportsArray[dtype[numpy.bool[builtins.bool]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]]]] | builtins.bool | _NestedSequence[builtins.bool]], ndarray[_ShapeT_co, _DType_co]]" [misc]
Your Environment
- Mypy version used: 1.16.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini
(and other config files): none - Python version used: 3.13.2
- Numpy version used: 2.2.6
Activity
iwakitakuma33 commentedon May 30, 2025
@bersbersbers
Is it wrong to use union as follows?
bersbersbers commentedon May 30, 2025
It wouldn't be wrong necessarily, if I enumerated all possible integer types. Question is, why should I need to do that, considering that
np.integer
exists? :)I also tried this, with varying success:
jorenham commentedon May 30, 2025
It's defined as follows in the numpy stubs:
https://github.com/numpy/numpy/blob/2b686f659642080e2fc708719385de6e8be0955f/numpy/__init__.pyi#L3334-L3357
So since there is no
self: integer
,self: number
, orself: generic
, mypy rejects this call to__iadd__
. Mypy 1.15.0 and 1.16.0 behave in the same way, so this is not a mypy regression or something.It's worth noting that pyright does not reject this. I'm guessing that it falls back to
__add__
if the overloads of__iadd__
are exhausted.But even so, I agree that your example should be accepted by all type-checkers. I'll try to get this fixed before the upcoming numpy 2.3 release, but it might not make it in time (2.3 is about to be released).
edit: see numpy/numpy#29092
NDArray[integer]
inplace operator mypy issue numpy/numpy#29092bersbersbers commentedon May 30, 2025
@jorenham thanks for taking care of this. Just one bit:
I disagree at least partially - at least in my code base, this issue started appearing after 1.16.0 was released, and going back to 1.15.0 fixes it. Maybe my MWE above also triggers something in 1.15, but something definitely changed with 1.16.
jorenham commentedon Jun 2, 2025
This will be fixed in the upcoming numpy 2.3.0 release.