GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||
GeezerVison is showing github.com — Accept `__getitem__`-iterables in `builtins.enumerate` by jorenham · Pull Request #12294 · python/typeshed Conversation
|
|
See also #7813 / #8597, nothing specific with builtins, it's just legacy behaviour of iteration in Python 2.1 and earlier. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ok I wasn't aware of the earlier discussion about this. And why should it only be allowed for |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I don't really see why |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
(As the person who added the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…or prevent cluttered error messages
Iterable and _GetItemIterable as "iterand" types in builtins__getitem__-iterables in builtins.enumerate
|
Oh haha nevermind. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…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] |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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., |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
As others have pointed out, the old-style iteration protocol isn't currently supported by typeshed, except in the case of |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I still don't understand what exactly makes this a "legacy feature", or "old-style". |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Because a better alternative has been introduced 23 years ago and |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Why do you think that matters? I haven't tried it but I believe your PR would make type checkers incorrectly accept |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
So now that there aren't any technical obstacles left, would you care to reconsider? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ok that's fine. Could you re-open it then? Or should I make a new PR? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I think you'll find that to be counterproductive when it comes to convincing us |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
I think ctypes is a good example. With How do you use
This doesn't work. A |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.