Skip to content

Commit 85ffd4a

Browse files
authored
Fix type eval if params are not in alphabetical order. (#23)
Fixes `type X[B, A] = A`
1 parent 1eaa857 commit 85ffd4a

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

tests/test_type_eval.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ class TB:
134134
y: list[object]
135135

136136

137+
type GetA1[A, B] = A
138+
type GetA2[B, A] = A
139+
140+
141+
def test_eval_arg_order():
142+
d = eval_typing(GetA1[int, str])
143+
assert d is int
144+
d = eval_typing(GetA2[str, int])
145+
assert d is int
146+
147+
137148
def test_type_getattr_union_1():
138149
d = eval_typing(GetAttr[TA | TB, Literal["x"]])
139150
assert d == int | str
@@ -640,7 +651,7 @@ def test_never_is():
640651
assert d is True
641652

642653

643-
def test_eval_iter():
654+
def test_eval_iter_01():
644655
d = eval_typing(Iter[tuple[int, str]])
645656
assert tuple(d) == (int, str)
646657

typemap/type_eval/_eval_typing.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,20 @@ def _eval_applied_type_alias(obj: types.GenericAlias, ctx: EvalContext):
298298

299299
func = obj.evaluate_value
300300

301-
args = tuple(types.CellType(_eval_types(arg, ctx)) for arg in obj.__args__)
301+
# obj.__args__ matches the declared parameter order, but args are expected
302+
# to be in the same order as func.__code__.co_freevars.
303+
args_by_name = dict(
304+
zip(
305+
(p.__name__ for p in obj.__origin__.__type_params__),
306+
obj.__args__,
307+
strict=False,
308+
)
309+
)
310+
311+
args = tuple(
312+
types.CellType(_eval_types(args_by_name[name], ctx))
313+
for name in func.__code__.co_freevars
314+
)
302315
mod = sys.modules[obj.__module__]
303316

304317
with _child_context() as child_ctx:

0 commit comments

Comments
 (0)