-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Recent change to tuple
constructor breaks subclasses of tuples
#8275
Comments
I feel like maybe a type checker should disallow this snippet. This is what happens at runtime: >>> class MyTuple(tuple[int, str, str]): ...
...
>>> MyTuple()
() Perhaps type checkers should disallow calls to subclasses of specialised tuples unless |
@AlexWaygood the runtime type of This change is from #7454. I already brought up this possible issue, but for whatever reason mypy doesn't infer the wrong type for subclasses in this case. Given that it causes a regression for pyright, I'm fine with reverting it. |
The runtime type is class MyTuple(tuple[int, str, str]):
...
x = MyTuple() # type checker is happy with this, infers type of x as MyTuple
# type checker is happy with this due to x having inferred type of MyTuple,
# and due to MyTuple being a subtype of tuple[int, str, str].
# However, this fails at runtime.
a, b, c = x That feels pretty unsafe to me. But I'm fine with reverting #7454 for now; I agree that it doesn't bring much benefit. |
A recent change to the
tuple.__new__
method inbuiltins.pyi
introduced an overload:This overload is problematic for classes that derive from specialized tuples.
Because of this overload, the wrong type is evaluated.
I think this overload should return
Self
, nottuple[()]
. Admittedly, that doesn't produce the ideal type evaluation fortuple()
. It producestuple[Any, ...]
rather thantuple[()]
.The text was updated successfully, but these errors were encountered: