-
-
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
incorrect annotations for builtins.reversed
#11645
Comments
This looks right; feel free to send a PR. We previously dealt with |
Do you have real-world use cases for these changes, or is this just correctness for the sake of correctness? From a practical point of view, it makes sense for type checkers to conmplain about returning an integer in |
@Akuli I was trying to implement |
@Akuli I also stumbled upon this issue whilst writing the callback protocols in optype; an opinionated low-level typing library that I've been working on. To be precise, I encountered it in |
@Akuli I just thought of another "cool" example: using I can imagine that using |
Alright, this makes more sense to me now :) |
The
builtins.reversed.__new__
signature is currently annotated like(Reversible[T] | SupportsLenAndGetItem[T]) -> Iterator[T]
:typeshed/stdlib/builtins.pyi
Lines 1639 to 1646 in 3d26992
It has two problems:
argument of type
SupportsLenAndGetItem[T]
This should return a
reversed[T]
instance (i.e.Self
), instead of "just" anIterable[T]
, which is unnecessarily loose.argument with type that implement
__reversed__
This is only handled if the argument's
__reversed__
returns anIterator[T]
, i.e. arguments that areReversible[T]
. But in reality,reversed(x)
is equivalent tox.__reversed__()
(if__reversed__
exists). To illustrate:However, this case isn't handled because
Revver[X]
cannot be expressed as aReversible[?]
.So instead, it should look something like this:
Let me know if I should make this into PR :)
The text was updated successfully, but these errors were encountered: