Skip to content

Commit

Permalink
Support TypeAliasType in a class scope (python#17038)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdanal authored Mar 16, 2024
1 parent 61a4900 commit b013cc0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3657,7 +3657,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
return False

non_global_scope = self.type or self.is_func_scope()
if not pep_613 and isinstance(rvalue, RefExpr) and non_global_scope:
if not pep_613 and not pep_695 and isinstance(rvalue, RefExpr) and non_global_scope:
# Fourth rule (special case): Non-subscripted right hand side creates a variable
# at class and function scopes. For example:
#
Expand Down
5 changes: 5 additions & 0 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,11 @@ TestType = TypeAliasType("TestType", Union[int, str])
x: TestType = 42
y: TestType = 'a'
z: TestType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[int, str]")

class A:
ClassAlias = TypeAliasType("ClassAlias", int)
xc: A.ClassAlias = 1
yc: A.ClassAlias = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[builtins fixtures/tuple.pyi]

[case testTypeAliasTypeInvalid]
Expand Down

0 comments on commit b013cc0

Please sign in to comment.