You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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? :)
So since there is no self: integer, self: number, or self: 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).
@jorenham thanks for taking care of this. Just one bit:
Mypy 1.15.0 and 1.16.0 behave in the same way
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.
Is this fixed now? I run into a similar problem with numpy version 2.3.3. The only difference is that I use np.floating and np.number, which mypy still seems to complain about for __iadd__ and __isub__. The linked PR does not seem to address these cases.
Is this fixed now? I run into a similar problem with numpy version 2.3.3. The only difference is that I use np.floating and np.number, which mypy still seems to complain about for __iadd__ and __isub__. The linked PR does not seem to address these cases.
Ah I didn't realize that this was also affecting number and floating, since that wasn't mentioned in the OP. I'll try to get those ones fixed as well then.
I guess the whole numpy type hierarchy is in principle affected. It would be great if there was a more systematic way to include these type hints (instead of spelling out each alternative individually).
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.integerexists? :)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
bersbersbers 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.
david-zwicker commentedon Sep 30, 2025
Is this fixed now? I run into a similar problem with
numpyversion 2.3.3. The only difference is that I usenp.floatingandnp.number, whichmypystill seems to complain about for__iadd__and__isub__. The linked PR does not seem to address these cases.jorenham commentedon Sep 30, 2025
Ah I didn't realize that this was also affecting
numberandfloating, since that wasn't mentioned in the OP. I'll try to get those ones fixed as well then.david-zwicker commentedon Sep 30, 2025
I guess the whole
numpytype hierarchy is in principle affected. It would be great if there was a more systematic way to include these type hints (instead of spelling out each alternative individually).jorenham commentedon Sep 30, 2025
There is :) I've implemented it in https://github.com/numpy/numtype (not in numpy itself because that'd be backwards incompatible)