mypy icon indicating copy to clipboard operation
mypy copied to clipboard

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

Open DetachHead opened this issue 2 years ago • 2 comments

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

DetachHead avatar Mar 03 '23 02:03 DetachHead

related to: #13376

KotlinIsland avatar Mar 03 '23 02:03 KotlinIsland

workaround: use new python 3.12 type alias syntax

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

DetachHead avatar Aug 19 '24 03:08 DetachHead