mypy
mypy copied to clipboard
Standalone subscripted alias expression fails mypy check
Bug Report
Standalone subscripted alias expression raises the error "Type application is only supported for generic classes"
To Reproduce
Here's a reproducible playground link: https://mypy-play.net/?mypy=0.971&python=3.10&gist=e262d3dd99f2a708e96eb28338792632
Code:
T = TypeVar("T")
OptionalT: TypeAlias = Optional[T]
OptionalT[int] # error: Type application is only supported for generic classes
Expected Behavior
I was surprised that you couldn't have a free expression of the subscripted generic type alias. Standalone expressions like Optional[int]
obviously work, as does X
where X: TypeAlias = int
.
Actual Behavior
The free expression gives the error: Type application is only supported for generic classes
in the playground
Your Environment
The environment is python 3.10, mypy 0.971.
Note this only reproduces if the type alias is used at the top level, not in an annotation.
I will take a look 🙂
The problem is that right now mypy only knows what to do with Instance
types. Any Union
types cause this problem:
from typing import Optional, TypeVar, Generic, Union
from typing_extensions import TypeAlias
T = TypeVar("T")
class A(Generic[T]): ...
class B(Generic[T]): ...
AorB: TypeAlias = Union[A[T], B[T]]
AorB[int] # Type application is only supported for generic classes
I think we should allow Union
type here.