python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.5k stars 2.83k forks source link

"Type variable is invalid as target for type alias" error on new union syntax when not using `TypeAlias` type #14824

Open DetachHead opened 1 year ago

DetachHead commented 1 year ago
from typing import TypeVar, TypeAlias

T = TypeVar("T")

MaybeList = T | list[T] # error: Type variable "__main__.T" is invalid as target for type alias
MaybeList2: TypeAlias = T | list[T] # no error

playground

KotlinIsland commented 1 year ago

related to: #13376

DetachHead commented 2 months ago

workaround: use new python 3.12 type alias syntax

type MaybeList[T] = T | list[T]