TypeAlias
to alias a ClassVar
with an Annotated
type raises an error
#18226
Labels
bug
mypy got something wrong
Bug Report
When using a TypeAlias to alias a ClassVar with an Annotated type, mypy raises an error indicating that ClassVar is valid only within a class body. This happens even though the code is valid and works at runtime. The issue seems to stem from mypy not recognizing the use of ClassVar within a type alias.
To Reproduce
Expected Behavior
Mypy should recognize that MyType is a type alias for a ClassVar and allow its usage within the class A without errors. The typ attribute should be treated as a class variable of type Annotated[str, "MyType"].
Actual Behavior
Mypy raises the following error:
This suggests that mypy does not accept the use of ClassVar within a type alias outside of a class body, even though it's valid Python code.
Your Environment
Additional Information
The code works as expected at runtime without any issues. Using ClassVar directly within the class body without a type alias does not produce an error. This issue may affect libraries like Pydantic that rely on such patterns for type annotations.
The text was updated successfully, but these errors were encountered: