Skip to content

All instances of types.GenericAlias are iterable on Python 3.11+ #103450

Closed as not planned
@AlexWaygood

Description

@AlexWaygood

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:

>>> list(dict[int, int])
[*dict[int, int]]
>>> import collections.abc
>>> list(collections.abc.Generator[int, str, bytes])
[*collections.abc.Generator[int, str, bytes]]

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixestopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions