mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Standalone subscripted alias expression fails mypy check

Open OneRaynyDay opened this issue 1 year ago • 3 comments

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.

OneRaynyDay avatar Aug 05 '22 21:08 OneRaynyDay

Note this only reproduces if the type alias is used at the top level, not in an annotation.

JelleZijlstra avatar Aug 06 '22 15:08 JelleZijlstra

I will take a look 🙂

sobolevn avatar Aug 11 '22 10:08 sobolevn

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.

sobolevn avatar Aug 11 '22 11:08 sobolevn