Skip to content
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

Support GenericAlias in typing #84576

Closed
serhiy-storchaka opened this issue Apr 26, 2020 · 8 comments
Closed

Support GenericAlias in typing #84576

serhiy-storchaka opened this issue Apr 26, 2020 · 8 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir topic-typing type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

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']

    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka serhiy-storchaka added 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Apr 26, 2020
    @serhiy-storchaka
    Copy link
    Member Author

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

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @danielkatzan
    Copy link

    @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)

    @gvanrossum
    Copy link
    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.

    @danielkatzan
    Copy link

    @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?

    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @AlexWaygood
    Copy link
    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).

    @danielkatzan
    Copy link

    danielkatzan commented Aug 1, 2022

    opened this #95539

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir topic-typing type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants