From b013cc016a3e3c8c7caa6a27bdf7b5e22998dd41 Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Sat, 16 Mar 2024 14:38:05 +0100 Subject: [PATCH] Support `TypeAliasType` in a class scope (#17038) Fixes https://github.com/python/mypy/issues/16614#issuecomment-2000428700 --- mypy/semanal.py | 2 +- test-data/unit/check-type-aliases.test | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 93e84ced4639..5aaf2bc6f433 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -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: # diff --git a/test-data/unit/check-type-aliases.test b/test-data/unit/check-type-aliases.test index 79a443dbeedc..7330a04c3647 100644 --- a/test-data/unit/check-type-aliases.test +++ b/test-data/unit/check-type-aliases.test @@ -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]