mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-44863: Allow generic typing.TypedDict (#27663)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
6c7249f265
commit
f6f36cc269
6 changed files with 172 additions and 7 deletions
|
@ -1738,7 +1738,7 @@ These are not used in annotations. They are building blocks for declaring types.
|
|||
z: int
|
||||
|
||||
A ``TypedDict`` cannot inherit from a non-TypedDict class,
|
||||
notably including :class:`Generic`. For example::
|
||||
except for :class:`Generic`. For example::
|
||||
|
||||
class X(TypedDict):
|
||||
x: int
|
||||
|
@ -1755,6 +1755,12 @@ These are not used in annotations. They are building blocks for declaring types.
|
|||
T = TypeVar('T')
|
||||
class XT(X, Generic[T]): pass # raises TypeError
|
||||
|
||||
A ``TypedDict`` can be generic::
|
||||
|
||||
class Group(TypedDict, Generic[T]):
|
||||
key: T
|
||||
group: list[T]
|
||||
|
||||
A ``TypedDict`` can be introspected via annotations dicts
|
||||
(see :ref:`annotations-howto` for more information on annotations best practices),
|
||||
:attr:`__total__`, :attr:`__required_keys__`, and :attr:`__optional_keys__`.
|
||||
|
@ -1802,6 +1808,9 @@ These are not used in annotations. They are building blocks for declaring types.
|
|||
|
||||
.. versionadded:: 3.8
|
||||
|
||||
.. versionchanged:: 3.11
|
||||
Added support for generic ``TypedDict``\ s.
|
||||
|
||||
Generic concrete collections
|
||||
----------------------------
|
||||
|
||||
|
|
|
@ -715,7 +715,10 @@ For major changes, see :ref:`new-feat-related-type-hints-311`.
|
|||
to clear all registered overloads of a function.
|
||||
(Contributed by Jelle Zijlstra in :gh:`89263`.)
|
||||
|
||||
* :class:`~typing.NamedTuple` subclasses can be generic.
|
||||
* :data:`typing.TypedDict` subclasses can now be generic. (Contributed by
|
||||
Samodya Abey in :gh:`89026`.)
|
||||
|
||||
* :class:`~typing.NamedTuple` subclasses can now be generic.
|
||||
(Contributed by Serhiy Storchaka in :issue:`43923`.)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue