Skip to content

Commit fe91422

Browse files
Disallow ClassVar in type aliases (#19263)
1 parent 929377a commit fe91422

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

mypy/typeanal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,10 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
675675
t,
676676
code=codes.VALID_TYPE,
677677
)
678+
if self.defining_alias:
679+
self.fail(
680+
"ClassVar[...] can't be used inside a type alias", t, code=codes.VALID_TYPE
681+
)
678682
if len(t.args) == 0:
679683
return AnyType(TypeOfAny.from_omitted_generics, line=t.line, column=t.column)
680684
if len(t.args) != 1:

test-data/unit/check-type-aliases.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,3 +1311,10 @@ class Bar(Generic[T]):
13111311
x: Bar[int]
13121312
reveal_type(x.var.bar) # N: Revealed type is "__main__.Bar[builtins.int]"
13131313
[builtins fixtures/tuple.pyi]
1314+
1315+
[case testExplicitTypeAliasClassVarProhibited]
1316+
from typing import ClassVar
1317+
from typing_extensions import TypeAlias
1318+
1319+
Foo: TypeAlias = ClassVar[int] # E: ClassVar[...] can't be used inside a type alias
1320+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)