Skip to content

Support GenericAlias in typing #84576

Description

@serhiy-storchaka
BPO 40396
Nosy @gvanrossum, @serhiy-storchaka, @ilevkivskyi
PRs
  • bpo-40396: Support GenericAlias in the typing functions. #19718
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-04-26.19:27:57.404>
    created_at = <Date 2020-04-26.16:02:57.099>
    labels = ['type-feature', 'library', '3.9']
    title = 'Support GenericAlias in typing'
    updated_at = <Date 2020-04-26.19:27:57.404>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2020-04-26.19:27:57.404>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-04-26.19:27:57.404>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2020-04-26.16:02:57.099>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40396
    keywords = ['patch']
    message_count = 2.0
    messages = ['367308', '367326']
    nosy_count = 3.0
    nosy_names = ['gvanrossum', 'serhiy.storchaka', 'levkivskyi']
    pr_nums = ['19718']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40396'
    versions = ['Python 3.9']

    Activity

    1. serhiy-storchaka commented on Apr 26, 2020

      @serhiy-storchaka
      MemberAuthor

      Currently typing functions get_origin(), get_args() and get_type_hints() do not support GenericAlias.

      >>> from typing import *
      >>> get_origin(List[int])
      <class 'list'>
      >>> get_origin(list[int])
      >>> get_args(List[int])
      (<class 'int'>,)
      >>> get_args(list[int])
      ()
      >>> def foo(x: List[ForwardRef('X')], y: list[ForwardRef('X')]) -> None: ...                                                                                                                                                                 
      ...                                                                                                                                                                                                                                          
      >>> class X: ...
      ... 
      >>> get_type_hints(foo)
      {'x': typing.List[__main__.X], 'y': list[ForwardRef('X')], 'return': <class 'NoneType'>}

      The proposed PR fixes this.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Apr 26, 2020
    3. serhiy-storchaka commented on Apr 26, 2020

      @serhiy-storchaka
      MemberAuthor

      New changeset 68b352a by Serhiy Storchaka in branch 'master':
      bpo-40396: Support GenericAlias in the typing functions. (GH-19718)
      68b352a

    4. transferred this issue fromon Apr 10, 2022
    5. danielkatzan commented on Aug 1, 2022

      @danielkatzan

      @serhiy-storchaka
      why does get_origin(list) returns None while get_origin(List) returns <class 'list'>

      shouldn't list behaves exactly the same as List? (the same question applies to all generics)

    6. gvanrossum commented on Aug 1, 2022

      @gvanrossum
      Member

      @danielkatzan Looks like an omission in the code for get_origin(). Would you like to submit a PR to fix it? I can't promise that it will make it into 3.11 but it will be in 3.11.1.

    7. danielkatzan commented on Aug 1, 2022

      @danielkatzan

      @gvanrossum Sure, I would be happy to, but could use a little pointer here
      Not sure how this can be accomplished

      note while type(list[str]) is <class 'types.GenericAlias'> and has the __origin__ attribute, type(list) is actually <class 'type'> and doesn't have an __origin__ attribute

      I can add an explicit if, checking if the tp is actually dict\list etc, and return that instead of None

      will that be a valid approach? or just masking a deeper issue that might pop up later in a different location?

    8. serhiy-storchaka commented on Aug 1, 2022

      @serhiy-storchaka
      MemberAuthor

      This issue was closed 2 years ago, please open a new issue.

      The fact that get_origin(List) returns list is not documented. It looks to me like an implementation artifact.

    9. AlexWaygood commented on Aug 1, 2022

      @AlexWaygood
      Member

      The fact that get_origin(List) returns list is not documented. It looks to me like an implementation artifact.

      Changing that would be a breaking change at this point (at least... it would break code that I, personally, have written).

    10. danielkatzan commented on Aug 1, 2022

      @danielkatzan

      opened this #95539

    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.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions