Skip to content

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

Description

@GBeauregard
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 = []

    Activity

    1. GBeauregard commented on Feb 6, 2022

      GBeauregardmannequin
      MannequinAuthor

      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.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Feb 6, 2022
    3. gvanrossum commented on Feb 6, 2022

      @gvanrossum
      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")
    4. gvanrossum commented on Feb 6, 2022

      @gvanrossum
      Member

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

    5. gvanrossum commented on Feb 7, 2022

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

    6. transferred this issue fromon Apr 10, 2022
    7. picnixz commented on Aug 27, 2024

      @picnixz
      Member

      Closing since the issue has been fixed (I cannot reproduce it on 3.12 or main).

    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

      stdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions