summaryrefslogtreecommitdiffstats
path: root/Lib/typing.py
diff options
context:
space:
mode:
authorFranek Magiera <framagie@gmail.com>2023-05-01 17:58:50 (GMT)
committerGitHub <noreply@github.com>2023-05-01 17:58:50 (GMT)
commit2d526cd32fe8b286aae38956648e508070729f8f (patch)
tree3a1bdf9e4ff451630cc62aaeb69461803100af4a /Lib/typing.py
parenta679c3d58d10aafd9ac9355fdd16151607e37d65 (diff)
downloadcpython-2d526cd32fe8b286aae38956648e508070729f8f.zip
cpython-2d526cd32fe8b286aae38956648e508070729f8f.tar.gz
cpython-2d526cd32fe8b286aae38956648e508070729f8f.tar.bz2
GH-103629: Update Unpack's repr in compliance with PEP 692 (#104048)
Diffstat (limited to 'Lib/typing.py')
-rw-r--r--Lib/typing.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index 354bc80..1a1c989 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1753,6 +1753,17 @@ def Unpack(self, parameters):
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.
@@ -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__: