-
-
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, ...]
@sterliakov To recapitulate: Lines 4050 to 4062 in d68ea35
In the specific case here, the LHS is a fixed size tuple So However, wouldn't it make sense here to consider reinterpreting the fixed size tuple as a variable size tuple? One could ask: Does there exist a choice of |
@randolf-scholz Not sure I follow, are you trying to convince me that variadic tuples should be considered subtypes of some fixed-length tuples? By definition, If I'm not misreading, you're saying that (for some class A: ...
class B(A): ...
class C(A): ... Applying your argument, does there exist a choice of Here's a link to the spec chapter laying out tuple assignability rules: https://typing.python.org/en/latest/spec/tuples.html#type-compatibility-rules |
That was definitely not the goal 😅. The issue is that the Looking at the python C-source, the actual check performed is This check ignores generics, hence the suggestion above was to slightly weaken the Footnotes
|
Ough, sorry, I was missing the third footnote in your previous comment - I thought you're trying to change Yes, at runtime we have essentially an |
@sterliakov I created a draft PR, but there are still some things unclear #19046 |
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: