Repository navigation
typing.TypeAlias is not in the list of allowed plain _SpecialForm typeforms #90813
Description
Activity
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 argumentat 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.
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Feb 6, 2022 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")
Closing since the issue has been fixed (I cannot reproduce it on 3.12 or main).
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:
bugs.python.org fields: