Skip to content

Commit 7285830

Browse files
authored
Accept value as keyword argument in TypeAliasType (#20556)
Fixes #20531 The case where value is missing is handled and tested
1 parent 4ac35ba commit 7285830

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

mypy/semanal.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4061,7 +4061,11 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
40614061
type_params: TypeVarLikeList | None
40624062
all_type_params_names: list[str] | None
40634063
if self.check_type_alias_type_call(s.rvalue, name=lvalue.name):
4064-
rvalue = s.rvalue.args[1]
4064+
rvalue = (
4065+
s.rvalue.args[1]
4066+
if s.rvalue.arg_kinds[1] == ARG_POS
4067+
else s.rvalue.args[s.rvalue.arg_names.index("value")]
4068+
)
40654069
pep_695 = True
40664070
type_params, all_type_params_names = self.analyze_type_alias_type_params(s.rvalue)
40674071
else:
@@ -4249,7 +4253,9 @@ def check_type_alias_type_call(self, rvalue: Expression, *, name: str) -> TypeGu
42494253
return False
42504254
if not self.check_typevarlike_name(rvalue, name, rvalue):
42514255
return False
4252-
if rvalue.arg_kinds.count(ARG_POS) != 2:
4256+
if rvalue.arg_kinds.count(ARG_POS) != (
4257+
2 - ("value" in rvalue.arg_names) - ("name" in rvalue.arg_names)
4258+
):
42534259
return False
42544260

42554261
return True

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,14 @@ x: TestType = 42
10711071
y: TestType = 'a'
10721072
z: TestType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "int | str")
10731073

1074+
TA = TypeAliasType("TA", value=int)
1075+
a: TA = 1
1076+
b: TA = 's' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1077+
1078+
TA2 = TypeAliasType("TA2", type_params=(), value=int) # shuffled arguments
1079+
a2: TA2 = 1
1080+
b2: TA2 = 's' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
1081+
10741082
reveal_type(TestType) # N: Revealed type is "typing_extensions.TypeAliasType"
10751083
TestType() # E: "TypeAliasType" not callable
10761084

0 commit comments

Comments
 (0)