-
-
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
Accept __getitem__
-iterables in builtins.enumerate
#12294
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Ok I wasn't aware of the earlier discussion about this. And why should it only be allowed for |
I think my comment here explains why we don't need this: #7813 (comment). The mypy-primer output on this PR also does not suggest this change makes type checking better for users. |
@JelleZijlstra I agree that it wouldn't be a good idea to replace But my (revised) suggestion is to only change the constructor signatures of It's a subtle change, and avoids their constructors from being unnecessarily restrictive. |
I don't really see why |
So am I correct to understand (and please do correct me if I'm wrong) that, after considering that
you decide that the issues (1-3) addressed in this PR are considered to be less of a problem than the downsides (4-5)? I understand that there probably aren't many usecases for the "getitem-iterators". But I'm a bit of a typing purist, and always considered my "typing utopia" to be in perfect harmony with the runtime, i.e. with noo false-positives (i.e. broad annotations like |
The failing CI and many new mypy-primer errors say otherwise. I'm not necessarily opposed to making a change here if it demonstrably makes the situation better for users, but this PR as it stands is not that. |
(As the person who added the |
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.
Ok I realize now that this isn't gonna work for the builtin collection types, because that could be incompatible with their user-defined subtypes. But @hauntsaninja 's logic regarding Although the mypy primer makes the earlier argument about confusing error messages more clear. |
…or prevent cluttered error messages
Iterable
and _GetItemIterable
as "iterand" types in builtins
__getitem__
-iterables in builtins.enumerate
This comment has been minimized.
This comment has been minimized.
Oh haha nevermind. |
…rable` for prevent cluttered error messages" This reverts commit 37c95e6.
Diff from mypy_primer, showing the effect of this PR on open source code: discord.py (https://github.com/Rapptz/discord.py)
- discord/abc.py:542: error: Argument 1 to "enumerate" has incompatible type "object"; expected "Iterable[Never]" [arg-type]
+ discord/abc.py:542: error: Argument 1 to "enumerate" has incompatible type "object"; expected "Iterable[Never] | _GetItemIterable[Never]" [arg-type]
cki-lib (https://gitlab.com/cki-project/cki-lib)
- tests/test_yaml.py:169: error: Argument 1 to "enumerate" has incompatible type "object"; expected "Iterable[Never]" [arg-type]
+ tests/test_yaml.py:169: error: Argument 1 to "enumerate" has incompatible type "object"; expected "Iterable[Never] | _GetItemIterable[Never]" [arg-type]
materialize (https://github.com/MaterializeInc/materialize)
- misc/python/materialize/cli/cloudbench.py:175: error: Argument 1 to "enumerate" has incompatible type "BenchSuccessResult | BenchFailureLogs | None"; expected "Iterable[str]" [arg-type]
+ misc/python/materialize/cli/cloudbench.py:175: error: Argument 1 to "enumerate" has incompatible type "BenchSuccessResult | BenchFailureLogs | None"; expected "Iterable[str] | _GetItemIterable[str]" [arg-type]
|
Would it be accepteble like this @JelleZijlstra? And if so, what are you thoughts on doing this as well for I don't think that it'll work for |
Thinking about it more, I don't see much point in changing this for just a few specific iterators. It's going to be confusing if "getitem iterables" are accepted in the type system by some but not all functions. It makes sense for There's also the issue @TeamSpen210 brought up on the previous thread: with the union, some types (e.g., |
As others have pointed out, the old-style iteration protocol isn't currently supported by typeshed, except in the case of |
I still don't understand what exactly makes this a "legacy feature", or "old-style". |
And @JelleZijlstra that union issue you mention doesn't apply here, as the union is used to annotate a function parameter, and not passed as type argument to a covariant type parameter. |
Because a better alternative has been introduced 23 years ago and |
Why do you think that matters? I haven't tried it but I believe your PR would make type checkers incorrectly accept |
@JelleZijlstra Yes you've got a point here. But it's easy to fix this using overloads, so that we can specifiy that Mypy accepts this as well. |
So now that there aren't any technical obstacles left, would you care to reconsider? |
I'm not interested in making this change; I don't think the increase in complexity is worth it. Another maintainer can speak up and merge a version of this change if they have a different view; I won't mind. |
Ok that's fine. Could you re-open it then? Or should I make a new PR? |
it doesn't seem like you've persuaded any maintainers yet that this change is worth it. If any of us is persuaded at any point, rest assured we will not hesitate to reopen this PR. |
I'm rather surprised that this bugfix, which at this point has no technical objections, requires persuasion at all. Additionally, it was closed for reasons that don't apply anymore, yet re-opening it is out of the question because "I haven't persuaded any maintainers yet". Shouldn't it be the other way around? A bug should only remain in the code if there's a good reason for it. The only remaining objection is that of the "confusing error messages". But that's the responsibility of type-checkers. The responsibility of typing stubs is to provide type hints that are correct and complete. And yes, this is in fact a fix for a bug, not just a "change": Currently type-checkers falsely reject valid usage of That should be more than enough reason to, at the very least, re-open this PR. |
I think you'll find that to be counterproductive when it comes to convincing us |
Do you have a practical example of why this change would be useful to you? What is the type that you want to enumerate over and only has I agree with other maintainers that this is a slippery slope: if we change |
The first example that comes to mind is
I don't see this would make Which brings me to another advantage of not having to define an |
I think ctypes is a good example. With How do you use
This doesn't work. A |
I noticed that the following example isn't accounted for in the
builtins
stubs, and currently rejected by typecheckers (although I only checked with pyright):