Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone subscripted alias expression fails mypy check #13337

Open
OneRaynyDay opened this issue Aug 5, 2022 · 3 comments · May be fixed by #13383
Open

Standalone subscripted alias expression fails mypy check #13337

OneRaynyDay opened this issue Aug 5, 2022 · 3 comments · May be fixed by #13383
Assignees
Labels
bug mypy got something wrong

Comments

@OneRaynyDay
Copy link

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 OneRaynyDay added the bug mypy got something wrong label Aug 5, 2022
@JelleZijlstra
Copy link
Member

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

@sobolevn sobolevn self-assigned this Aug 11, 2022
@sobolevn
Copy link
Member

I will take a look 🙂

@sobolevn
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants