Skip to content

Commit 807d6b5

Browse files
authored
gh-101015: Fix typing.get_type_hints with unpacked *tuple (PEP 646) (#101031)
1 parent d717be0 commit 807d6b5

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

Lib/test/test_typing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,36 @@ class D(Generic[Unpack[Ts]]): pass
12241224
self.assertIs(D[T].__origin__, D)
12251225
self.assertIs(D[Unpack[Ts]].__origin__, D)
12261226

1227+
def test_get_type_hints_on_unpack_args(self):
1228+
Ts = TypeVarTuple('Ts')
1229+
1230+
def func1(*args: *Ts): pass
1231+
self.assertEqual(gth(func1), {'args': Unpack[Ts]})
1232+
1233+
def func2(*args: *tuple[int, str]): pass
1234+
self.assertEqual(gth(func2), {'args': Unpack[tuple[int, str]]})
1235+
1236+
class CustomVariadic(Generic[*Ts]): pass
1237+
1238+
def func3(*args: *CustomVariadic[int, str]): pass
1239+
self.assertEqual(gth(func3), {'args': Unpack[CustomVariadic[int, str]]})
1240+
1241+
def test_get_type_hints_on_unpack_args_string(self):
1242+
Ts = TypeVarTuple('Ts')
1243+
1244+
def func1(*args: '*Ts'): pass
1245+
self.assertEqual(gth(func1, localns={'Ts': Ts}),
1246+
{'args': Unpack[Ts]})
1247+
1248+
def func2(*args: '*tuple[int, str]'): pass
1249+
self.assertEqual(gth(func2), {'args': Unpack[tuple[int, str]]})
1250+
1251+
class CustomVariadic(Generic[*Ts]): pass
1252+
1253+
def func3(*args: '*CustomVariadic[int, str]'): pass
1254+
self.assertEqual(gth(func3, localns={'CustomVariadic': CustomVariadic}),
1255+
{'args': Unpack[CustomVariadic[int, str]]})
1256+
12271257
def test_tuple_args_are_correct(self):
12281258
Ts = TypeVarTuple('Ts')
12291259

Lib/typing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,13 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
371371
ForwardRef(arg) if isinstance(arg, str) else arg
372372
for arg in t.__args__
373373
)
374+
is_unpacked = t.__unpacked__
374375
if _should_unflatten_callable_args(t, args):
375376
t = t.__origin__[(args[:-1], args[-1])]
376377
else:
377378
t = t.__origin__[args]
379+
if is_unpacked:
380+
t = Unpack[t]
378381
ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
379382
if ev_args == t.__args__:
380383
return t
@@ -828,7 +831,7 @@ def __init__(self, arg, is_argument=True, module=None, *, is_class=False):
828831
# Unfortunately, this isn't a valid expression on its own, so we
829832
# do the unpacking manually.
830833
if arg[0] == '*':
831-
arg_to_compile = f'({arg},)[0]' # E.g. (*Ts,)[0]
834+
arg_to_compile = f'({arg},)[0]' # E.g. (*Ts,)[0] or (*tuple[int, int],)[0]
832835
else:
833836
arg_to_compile = arg
834837
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`typing.get_type_hints` on ``'*tuple[...]'`` and ``*tuple[...]``.
2+
It must not drop the ``Unpack`` part.

0 commit comments

Comments
 (0)