Skip to content

Commit 803c4e0

Browse files
h-joocopybara-github
authored andcommitted
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
1 parent 8d0e1d6 commit 803c4e0

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

pytype/tests/test_typevar1.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,39 @@ def test_unused_typevar_pep695_switch_order(self):
5252
""",
5353
)
5454

55+
@test_utils.skipBeforePy((3, 12), "PEP 695 - 3.12 feature")
56+
def test_unused_typevar_pep695_function_type_var_single(self):
57+
ty = self.Infer("""
58+
def foo[T, S](a: T) -> T:
59+
return a
60+
""")
61+
self.assertTypesMatchPytd(
62+
ty,
63+
"""
64+
from typing import TypeVar
65+
T = TypeVar("T")
66+
67+
def foo(a: T) -> T: ...
68+
""",
69+
)
70+
71+
@test_utils.skipBeforePy((3, 12), "PEP 695 - 3.12 feature")
72+
def test_unused_typevar_pep695_function_type_var_double(self):
73+
ty = self.Infer("""
74+
def foo[T, S](a: T, b: S) -> tuple[S, T]:
75+
return (a, b)
76+
""")
77+
self.assertTypesMatchPytd(
78+
ty,
79+
"""
80+
from typing import TypeVar
81+
S = TypeVar('S')
82+
T = TypeVar('T')
83+
84+
def foo(a: T, b: S) -> tuple[S, T]: ...
85+
""",
86+
)
87+
5588
def test_import_typevar(self):
5689
with test_utils.Tempdir() as d:
5790
d.create_file("a.pyi", """T = TypeVar("T")""")

pytype/vm.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3939,7 +3939,13 @@ def byte_INTRINSIC_TYPEVAR_WITH_CONSTRAINTS(self, state):
39393939
return state
39403940

39413941
def byte_INTRINSIC_SET_FUNCTION_TYPE_PARAMS(self, state):
3942-
# TODO: b/350910471 - Implement to support PEP 695
3942+
# Second parameter here is a type parameter, it's stored to the
3943+
# __type_params__ function attribute at runtime, but this information is not
3944+
# consumed by us. This intrinsic consumes two parameters which is the
3945+
# function object and the type parameter list and returns the function
3946+
# object.
3947+
state, (func, _) = state.popn(2)
3948+
state = state.push(func)
39433949
return state
39443950

39453951

0 commit comments

Comments
 (0)