From 803c4e0bf1bd6715064ab7a129ff5067d82f236a Mon Sep 17 00:00:00 2001 From: Hana Joo Date: Fri, 25 Apr 2025 01:34:52 -0700 Subject: [PATCH] Support the intrinsic SET_FUNCTION_TYPE_PARAMS. Nothing special to do from pytype side, just consume the parameter and it usually would and return the function object. PiperOrigin-RevId: 751317933 --- pytype/tests/test_typevar1.py | 33 +++++++++++++++++++++++++++++++++ pytype/vm.py | 8 +++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/pytype/tests/test_typevar1.py b/pytype/tests/test_typevar1.py index 975edf342..3c0d1e92a 100644 --- a/pytype/tests/test_typevar1.py +++ b/pytype/tests/test_typevar1.py @@ -52,6 +52,39 @@ def test_unused_typevar_pep695_switch_order(self): """, ) + @test_utils.skipBeforePy((3, 12), "PEP 695 - 3.12 feature") + def test_unused_typevar_pep695_function_type_var_single(self): + ty = self.Infer(""" + def foo[T, S](a: T) -> T: + return a + """) + self.assertTypesMatchPytd( + ty, + """ + from typing import TypeVar + T = TypeVar("T") + + def foo(a: T) -> T: ... + """, + ) + + @test_utils.skipBeforePy((3, 12), "PEP 695 - 3.12 feature") + def test_unused_typevar_pep695_function_type_var_double(self): + ty = self.Infer(""" + def foo[T, S](a: T, b: S) -> tuple[S, T]: + return (a, b) + """) + self.assertTypesMatchPytd( + ty, + """ + from typing import TypeVar + S = TypeVar('S') + T = TypeVar('T') + + def foo(a: T, b: S) -> tuple[S, T]: ... + """, + ) + def test_import_typevar(self): with test_utils.Tempdir() as d: d.create_file("a.pyi", """T = TypeVar("T")""") diff --git a/pytype/vm.py b/pytype/vm.py index e26bb7903..f65822f1f 100644 --- a/pytype/vm.py +++ b/pytype/vm.py @@ -3939,7 +3939,13 @@ def byte_INTRINSIC_TYPEVAR_WITH_CONSTRAINTS(self, state): return state def byte_INTRINSIC_SET_FUNCTION_TYPE_PARAMS(self, state): - # TODO: b/350910471 - Implement to support PEP 695 + # Second parameter here is a type parameter, it's stored to the + # __type_params__ function attribute at runtime, but this information is not + # consumed by us. This intrinsic consumes two parameters which is the + # function object and the type parameter list and returns the function + # object. + state, (func, _) = state.popn(2) + state = state.push(func) return state