GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — All instances of `types.GenericAlias` are iterable on Python 3.11+ · Issue #103450 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
All instances of types.GenericAlias are iterable on Python 3.11+#103450
Copy link
Labels
3.11only security fixesonly security fixes3.12only security fixesonly security fixestopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@AlexWaygood
AlexWaygood
opened on Apr 11, 2023
Issue body actions

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?

Activity

  1. AlexWaygood
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    3.11only security fixesonly security fixes
    topic-typing
    3.12only security fixesonly security fixes
    on Apr 11, 2023
  2. hauntsaninja commented on Apr 11, 2023

    @hauntsaninja
    hauntsaninja
    on Apr 11, 2023
    Contributor
    More actions

    the runtime usually prefers to be lenient when it comes to typing constructs, leaving it to type checkers to point out invalid usage

    I vote for this option!

  3. JelleZijlstra commented on Apr 11, 2023

    @JelleZijlstra
    JelleZijlstra
    on Apr 11, 2023
    Member
    More actions

    My thinking at the time was that this could be useful for allowing something like tuple[*Iterable[int]], or something related to the TypeVarDict idea that was recently brought up on typing-sig. I don't see a need to forbid this again, especially since we've already had a series of releases allowing it.

  4. AlexWaygood commented on Apr 11, 2023

    @AlexWaygood
    AlexWaygood
    on Apr 11, 2023
    MemberAuthor
    More actions

    It's probably not really a big deal, but one impact of this is that there's yet another way in which collections.abc.Callable and typing.Callable are slightly different from each other:

    >>> 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
  5. AlexWaygood
    changed the title [-]All generic aliases are iterable on Python 3.11+[/-] [+]All instances of `types.GenericAlias` are iterable on Python 3.11+[/+] on Apr 11, 2023
  6. sobolevn commented on Apr 12, 2023

    @sobolevn
    sobolevn
    on Apr 12, 2023
    Member
    More actions

    @AlexWaygood yes, this happens because most of the typing constructs directly inherit from

    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
  7. AlexWaygood commented on Apr 29, 2023

    @AlexWaygood
    AlexWaygood
    on Apr 29, 2023
    MemberAuthor
    More actions

    Seems like there's little appetite for changing this

  8. AlexWaygood
    closed this as not plannedon Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.