-
-
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
Fix typing of reversed #10655
Fix typing of reversed #10655
Conversation
Huh, mypy thinks |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
So there's no completely type safe to annotate |
Yes, I see that. I would argue that, if that's the case, this PR is still (marginally) the best way of doing things. In the very rare case where a user calls |
I don't think we should encourage patterns in typeshed that are not considered valid by type checkers, even if that's what the implementation does under the hood. In this case the lie the type checker tells is rather harmless, since So I'm personally not convinced that the change is a positive one, but the maintainers of typeshed will have to decide whether it is or not. BTW minor nit: You used |
This comment has been minimized.
This comment has been minimized.
Note this might be working with python/mypy#16020 |
What concrete problem does this solve, in terms of code examples that will typecheck better with this change? Given that mypy doesn't seem to support this pattern for now, I think we should add test cases to make sure this doesn't regress type checking with mypy (e.g., make sure it doesn't start inferring Any). |
The main reason this is relevant is if you have a subclass of reversed (which is rare but allowed). An example might look something like this:
|
Annotating something with
Maybe we should pretend that |
The thing is, I think it is was the intention of the cpython developers that
This seems fairly clearly to allow for this sort of behaviour. If mypy doesn't like it, then mypy is just wrong. |
True, but mypy is among the most significant ways people use typeshed, and we'd be doing our end users a disservice if we break type checking I'd prefer to have stubs that correctly reflect reality, so we do need a change like the one in this PR, but we should have test cases that demonstrate that this change doesn't cause new problems. Test cases should look something like this: x: list[int]
assert_type(list(reversed(x)), list[int]) To make sure that we don't get a |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, and sorry for the wait!
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Reversed has a
__new__
method, not an__init__
method. Note that it does not normally actually return an instance of the reversed class.