From 4f1f74c6bf879977185cd80ed5eb0089d495aef9 Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Sat, 10 Jan 2026 15:00:40 +0100 Subject: [PATCH] Accept value as keyword argument in TypeAliasType --- mypy/semanal.py | 10 ++++++++-- test-data/unit/check-type-aliases.test | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 11f0156372bf..d5260115ec64 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -4061,7 +4061,11 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool: type_params: TypeVarLikeList | None all_type_params_names: list[str] | None if self.check_type_alias_type_call(s.rvalue, name=lvalue.name): - rvalue = s.rvalue.args[1] + rvalue = ( + s.rvalue.args[1] + if s.rvalue.arg_kinds[1] == ARG_POS + else s.rvalue.args[s.rvalue.arg_names.index("value")] + ) pep_695 = True type_params, all_type_params_names = self.analyze_type_alias_type_params(s.rvalue) else: @@ -4249,7 +4253,9 @@ def check_type_alias_type_call(self, rvalue: Expression, *, name: str) -> TypeGu return False if not self.check_typevarlike_name(rvalue, name, rvalue): return False - if rvalue.arg_kinds.count(ARG_POS) != 2: + if rvalue.arg_kinds.count(ARG_POS) != ( + 2 - ("value" in rvalue.arg_names) - ("name" in rvalue.arg_names) + ): return False return True diff --git a/test-data/unit/check-type-aliases.test b/test-data/unit/check-type-aliases.test index cec44d582f0a..61eda2bd7774 100644 --- a/test-data/unit/check-type-aliases.test +++ b/test-data/unit/check-type-aliases.test @@ -1071,6 +1071,14 @@ x: TestType = 42 y: TestType = 'a' z: TestType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "int | str") +TA = TypeAliasType("TA", value=int) +a: TA = 1 +b: TA = 's' # E: Incompatible types in assignment (expression has type "str", variable has type "int") + +TA2 = TypeAliasType("TA2", type_params=(), value=int) # shuffled arguments +a2: TA2 = 1 +b2: TA2 = 's' # E: Incompatible types in assignment (expression has type "str", variable has type "int") + reveal_type(TestType) # N: Revealed type is "typing_extensions.TypeAliasType" TestType() # E: "TypeAliasType" not callable