GH-103629: Update Unpack's repr in compliance with PEP 692 by franekmagiera · Pull Request #104048 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 32 additions & 27 deletions Lib/test/test_typing.py
13 changes: 12 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,17 @@ class Bar(Generic[Unpack[Ts]]): ...
Foo[*tuple[int, str]]
class Bar(Generic[*Ts]): ...

The operator can also be used along with a `TypedDict` to annotate
`**kwargs` in a function signature. For instance:

class Movie(TypedDict):
name: str
year: int

# This function expects two keyword arguments - *name* of type `str` and
# *year* of type `int`.
def foo(**kwargs: Unpack[Movie]): ...

Note that there is only some runtime checking of this operator. Not
everything the runtime allows may be accepted by static type checkers.

Expand All @@ -1767,7 +1778,7 @@ class _UnpackGenericAlias(_GenericAlias, _root=True):
def __repr__(self):
# `Unpack` only takes one argument, so __args__ should contain only
# a single item.
return '*' + repr(self.__args__[0])
return f'typing.Unpack[{_type_repr(self.__args__[0])}]'

def __getitem__(self, args):
if self.__typing_is_unpacked_typevartuple__:
Expand Down