-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
All instances of types.GenericAlias
are iterable on Python 3.11+
#103450
Comments
I vote for this option! |
My thinking at the time was that this could be useful for allowing something like |
It's probably not really a big deal, but one impact of this is that there's yet another way in which >>> import collections.abc, typing
>>> list(collections.abc.Callable[..., int])
[*collections.abc.Callable[..., int]]
>>> list(typing.Callable[..., int])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '_CallableGenericAlias' object is not iterable |
types.GenericAlias
are iterable on Python 3.11+
@AlexWaygood yes, this happens because most of the class _NotIterable:
"""Mixin to prevent iteration, without being compatible with Iterable.
That is, we could do:
def __iter__(self): raise TypeError()
But this would make users of this mixin duck type-compatible with
collections.abc.Iterable - isinstance(foo, Iterable) would be True.
Luckily, we can instead prevent iteration by setting __iter__ to None, which
is treated specially.
"""
__slots__ = ()
__iter__ = None |
Seems like there's little appetite for changing this |
All generic aliases appear to be iterable on Python 3.11+, meaning you can do some fairly bizarre things that don't really make any sense:
This is due to the implementation of PEP 646 that was added in #31019 by @mrahtz. It was a deliberate decision at the time: 4aa94df is the commit in that PR branch that removes the restriction that only
tuple
can be unpacked (the commit was done in response to a review comment here: #31019 (comment)). However, there wasn't much discussion at the time.Do we want to stick with this behaviour, on the basis that the runtime usually prefers to be lenient when it comes to typing constructs, leaving it to type checkers to point out invalid usage? Or should this be considered a bug?
The text was updated successfully, but these errors were encountered: