diff --git a/tests/test_type_eval.py b/tests/test_type_eval.py index 77829f4..f79031a 100644 --- a/tests/test_type_eval.py +++ b/tests/test_type_eval.py @@ -134,6 +134,17 @@ class TB: y: list[object] +type GetA1[A, B] = A +type GetA2[B, A] = A + + +def test_eval_arg_order(): + d = eval_typing(GetA1[int, str]) + assert d is int + d = eval_typing(GetA2[str, int]) + assert d is int + + def test_type_getattr_union_1(): d = eval_typing(GetAttr[TA | TB, Literal["x"]]) assert d == int | str @@ -640,7 +651,7 @@ def test_never_is(): assert d is True -def test_eval_iter(): +def test_eval_iter_01(): d = eval_typing(Iter[tuple[int, str]]) assert tuple(d) == (int, str) diff --git a/typemap/type_eval/_eval_typing.py b/typemap/type_eval/_eval_typing.py index bab4cce..8fe0f5a 100644 --- a/typemap/type_eval/_eval_typing.py +++ b/typemap/type_eval/_eval_typing.py @@ -298,7 +298,20 @@ def _eval_applied_type_alias(obj: types.GenericAlias, ctx: EvalContext): func = obj.evaluate_value - args = tuple(types.CellType(_eval_types(arg, ctx)) for arg in obj.__args__) + # obj.__args__ matches the declared parameter order, but args are expected + # to be in the same order as func.__code__.co_freevars. + args_by_name = dict( + zip( + (p.__name__ for p in obj.__origin__.__type_params__), + obj.__args__, + strict=False, + ) + ) + + args = tuple( + types.CellType(_eval_types(args_by_name[name], ctx)) + for name in func.__code__.co_freevars + ) mod = sys.modules[obj.__module__] with _child_context() as child_ctx: