Skip to content

Commit b013cc0

Browse files
authored
Support TypeAliasType in a class scope (#17038)
Fixes #16614 (comment)
1 parent 61a4900 commit b013cc0

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3657,7 +3657,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
36573657
return False
36583658

36593659
non_global_scope = self.type or self.is_func_scope()
3660-
if not pep_613 and isinstance(rvalue, RefExpr) and non_global_scope:
3660+
if not pep_613 and not pep_695 and isinstance(rvalue, RefExpr) and non_global_scope:
36613661
# Fourth rule (special case): Non-subscripted right hand side creates a variable
36623662
# at class and function scopes. For example:
36633663
#

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,11 @@ TestType = TypeAliasType("TestType", Union[int, str])
10741074
x: TestType = 42
10751075
y: TestType = 'a'
10761076
z: TestType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[int, str]")
1077+
1078+
class A:
1079+
ClassAlias = TypeAliasType("ClassAlias", int)
1080+
xc: A.ClassAlias = 1
1081+
yc: A.ClassAlias = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
10771082
[builtins fixtures/tuple.pyi]
10781083

10791084
[case testTypeAliasTypeInvalid]

0 commit comments

Comments
 (0)