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:
Samodya Abey 2022-05-03 18:51:42 +05:30 committed by GitHub
parent 6c7249f265
commit f6f36cc269
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 172 additions and 7 deletions

View file

@ -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
----------------------------

View file

@ -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`.)