From aa4410ff7806425f143c3c4a21324d8f10b3f76d Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 3 Jun 2024 08:49:12 -0700 Subject: [PATCH] [mypyc] Fix ParamSpec (#17309) Fixes https://github.com/mypyc/mypyc/issues/1051 --- mypyc/irbuild/mapper.py | 4 +-- mypyc/test-data/irbuild-generics.test | 41 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/mypyc/irbuild/mapper.py b/mypyc/irbuild/mapper.py index a3abbb1f84fb..90ce0e16c741 100644 --- a/mypyc/irbuild/mapper.py +++ b/mypyc/irbuild/mapper.py @@ -15,7 +15,7 @@ Type, TypedDictType, TypeType, - TypeVarType, + TypeVarLikeType, UnboundType, UninhabitedType, UnionType, @@ -131,7 +131,7 @@ def type_to_rtype(self, typ: Type | None) -> RType: return object_rprimitive elif isinstance(typ, TypeType): return object_rprimitive - elif isinstance(typ, TypeVarType): + elif isinstance(typ, TypeVarLikeType): # Erase type variable to upper bound. # TODO: Erase to union if object has value restriction? return self.type_to_rtype(typ.upper_bound) diff --git a/mypyc/test-data/irbuild-generics.test b/mypyc/test-data/irbuild-generics.test index 4f9d0ab83a16..910148f80dda 100644 --- a/mypyc/test-data/irbuild-generics.test +++ b/mypyc/test-data/irbuild-generics.test @@ -148,3 +148,44 @@ L2: r4 = x L3: return r4 + + +[case testParamSpec] +from typing import Callable, ParamSpec, TypeVar + +P = ParamSpec("P") + +def execute(func: Callable[P, int], *args: P.args, **kwargs: P.kwargs) -> int: + return func(*args, **kwargs) + +def f(x: int) -> int: + return x + +execute(f, 1) +[out] +def execute(func, args, kwargs): + func :: object + args :: tuple + kwargs :: dict + r0 :: list + r1 :: object + r2 :: dict + r3 :: i32 + r4 :: bit + r5 :: tuple + r6 :: object + r7 :: int +L0: + r0 = PyList_New(0) + r1 = CPyList_Extend(r0, args) + r2 = PyDict_New() + r3 = CPyDict_UpdateInDisplay(r2, kwargs) + r4 = r3 >= 0 :: signed + r5 = PyList_AsTuple(r0) + r6 = PyObject_Call(func, r5, r2) + r7 = unbox(int, r6) + return r7 +def f(x): + x :: int +L0: + return x