This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: typing.TypeAlias is not in the list of allowed plain _SpecialForm typeforms
Type: behavior Stage: patch review
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: GBeauregard, JelleZijlstra, gvanrossum, kj
Priority: normal Keywords: patch

Created on 2022-02-06 07:30 by GBeauregard, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 31156 merged GBeauregard, 2022-02-06 08:41
PR 31175 merged GBeauregard, 2022-02-06 23:34
Messages (4)
msg412618 - (view) Author: Gregory Beauregard (GBeauregard) * Date: 2022-02-06 07:30
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.
msg412685 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-02-06 23:16
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")
msg412686 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-02-06 23:16
New changeset 77b025be4a4cd5a3bfc1b1af560cc57e8e956c98 by Gregory Beauregard in branch 'main':
bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156)
https://github.com/python/cpython/commit/77b025be4a4cd5a3bfc1b1af560cc57e8e956c98
msg412766 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-02-07 16:22
New changeset e2eeffefed32bb8c47c09bdd94e27a4e949894ef by Gregory Beauregard in branch '3.10':
[3.10] bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156). (#31175)
https://github.com/python/cpython/commit/e2eeffefed32bb8c47c09bdd94e27a4e949894ef
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90813
2022-02-07 16:22:04gvanrossumsetmessages: + msg412766
2022-02-06 23:34:47GBeauregardsetpull_requests: + pull_request29346
2022-02-06 23:16:28gvanrossumsetmessages: + msg412686
2022-02-06 23:16:00gvanrossumsetnosy: + gvanrossum
messages: + msg412685
2022-02-06 08:41:49GBeauregardsetkeywords: + patch
stage: patch review
pull_requests: + pull_request29331
2022-02-06 07:37:54GBeauregardsetnosy: + kj
2022-02-06 07:37:24GBeauregardsetnosy: + JelleZijlstra
2022-02-06 07:30:06GBeauregardcreate