-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Incorrect type inference with __radd__
with subclass of tuple[int, ...]
#19006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Ohh, this is interesting! Lines 3487 to 3516 in c724a6a
|
@sterliakov I stepped through this example with the debugger: from typing import assert_type
class Size(tuple[int, ...]):
def __add__(self, other: tuple[int, ...], /) -> "Size": return Size() # type: ignore[override]
def __radd__(self, other: tuple[int, ...], /) -> "Size": return Size()
size: Size = Size([3,4])
tup: tuple[int, ...] = (1, 2)
assert_type(tup + size, Size) # ✅ tuple[int, ...] + Size
assert_type(() + size, Size) # ❌ tuple[()] + Size
assert_type((1, 2) + size, Size) # ❌ tuple[Literal[1], Literal[2]] + Size The last two examples fail inside this branch: Lines 472 to 494 in c724a6a
In both cases, |
That's correct: only Sorry, I linked to the wrong place - that one is for cases without |
Right, so it doesn't really have anything to do with tuple literals after all, I will update the OP. |
__radd__
of tuple subtype against literal tuples.__radd__
with subclass of tuple[int, ...]
Bug Report
mypy
generally correctly prioritizes__radd__
if the right operand is a subtype of the left operand. However, I discovered that it can fail to do so when creating a subclass oftuple[int, ...]
.To Reproduce
The bug does seem to be tuple-specific, for instance it does not appear with integer literals: https://mypy-play.net/?mypy=latest&python=3.12&gist=da0763e25cd0654d1a8b8b0b67291bc5
Expected Behavior
All
assert_type
in the example above should succeed.The text was updated successfully, but these errors were encountered: