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

typing.TypeAlias is not in the list of allowed plain _SpecialForm typeforms #90813

Open
GBeauregard mannequin opened this issue Feb 6, 2022 · 4 comments
Open

typing.TypeAlias is not in the list of allowed plain _SpecialForm typeforms #90813

GBeauregard mannequin opened this issue Feb 6, 2022 · 4 comments
Labels
stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@GBeauregard
Copy link
Mannequin

GBeauregard mannequin commented Feb 6, 2022

BPO 46655
Nosy @gvanrossum, @JelleZijlstra, @Fidget-Spinner, @GBeauregard
PRs
  • bpo-46655: allow stringized TypeAlias with get_type_hints #31156
  • [3.10] bpo-46655: allow stringized TypeAlias with get_type_hints (GH-… #31175
  • 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 = None
    created_at = <Date 2022-02-06.07:30:06.388>
    labels = ['type-bug', 'library']
    title = 'typing.TypeAlias is not in the list of allowed plain _SpecialForm typeforms'
    updated_at = <Date 2022-02-07.16:22:04.935>
    user = 'https://github.com/GBeauregard'

    bugs.python.org fields:

    activity = <Date 2022-02-07.16:22:04.935>
    actor = 'gvanrossum'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2022-02-06.07:30:06.388>
    creator = 'GBeauregard'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46655
    keywords = ['patch']
    message_count = 4.0
    messages = ['412618', '412685', '412686', '412766']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'JelleZijlstra', 'kj', 'GBeauregard']
    pr_nums = ['31156', '31175']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46655'
    versions = []

    @GBeauregard
    Copy link
    Mannequin Author

    GBeauregard mannequin commented Feb 6, 2022

    typing.TypeAlias is allowed to be bare, but it's not listed in the list of types in typing._type_check that are allowed to be bare. This means it's possible to reach the wrong error TypeError: Plain typing.TypeAlias is not valid as type argument at runtime.

    Examples offhand:
    from typing import TypeAlias, get_type_hints
    class A:
    a: "TypeAlias" = int
    get_type_hints(A)

    from typing import Annotated, TypeAlias
    b: Annotated[TypeAlias, ""] = int

    There's likely more and/or more realistic ways to trigger the problem. Anything that triggers typing._type_check on typing.TypeAlias will give this error (TypeError: Plain typing.TypeAlias is not valid as type argument).

    I will fix this by adding TypeAlias to the list of typing special forms allowed to be bare/plain. I intend to move these to their own named var to reduce the chance of types not getting added in the future.

    @GBeauregard GBeauregard mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Feb 6, 2022
    @gvanrossum
    Copy link
    Member

    Looks like the more likely use case would be PEP-563:

    from __future__ import annotations
    from typing import TypeAlias, get_type_hints
    
    import typing
    
    class C:
        a: TypeAlias = int
    
    print(get_type_hints(C))

    This prints

    Traceback (most recent call last):
      File "C:\Users\gvanrossum\cpython\t.py", line 9, in <module>
        print(get_type_hints(C))
      File "C:\Users\gvanrossum\AppData\Local\Programs\Python\Python310\lib\typing.py", line 1808, in get_type_hints
        value = _eval_type(value, base_globals, base_locals)
      File "C:\Users\gvanrossum\AppData\Local\Programs\Python\Python310\lib\typing.py", line 326, in _eval_type
        return t._evaluate(globalns, localns, recursive_guard)
      File "C:\Users\gvanrossum\AppData\Local\Programs\Python\Python310\lib\typing.py", line 690, in _evaluate
        type_ = _type_check(
      File "C:\Users\gvanrossum\AppData\Local\Programs\Python\Python310\lib\typing.py", line 171, in _type_check
        raise TypeError(f"Plain {arg} is not valid as type argument")

    @gvanrossum
    Copy link
    Member

    New changeset 77b025b by Gregory Beauregard in branch 'main':
    bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156)
    77b025b

    @gvanrossum
    Copy link
    Member

    New changeset e2eeffe by Gregory Beauregard in branch '3.10':
    [3.10] bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156). (bpo-31175)
    e2eeffe

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants