diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-05-02 22:41:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-02 22:41:23 (GMT) |
commit | b04e02c57f82d6e1ae52ee6f070eb893d019f4fd (patch) | |
tree | c31c0be6958d56e84ffbe2c69eee51d73ab1f6a1 /Doc | |
parent | 81fb3548be5a18bf40a6f4505a02cc7fb72c9c34 (diff) | |
download | cpython-b04e02c57f82d6e1ae52ee6f070eb893d019f4fd.zip cpython-b04e02c57f82d6e1ae52ee6f070eb893d019f4fd.tar.gz cpython-b04e02c57f82d6e1ae52ee6f070eb893d019f4fd.tar.bz2 |
bpo-43923: Add support for generic typing.NamedTuple (#92027)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/typing.rst | 9 | ||||
-rw-r--r-- | Doc/whatsnew/3.11.rst | 4 |
2 files changed, 13 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 868ea1b..05ac057 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1615,6 +1615,12 @@ These are not used in annotations. They are building blocks for declaring types. def __repr__(self) -> str: return f'<Employee {self.name}, id={self.id}>' + ``NamedTuple`` subclasses can be generic:: + + class Group(NamedTuple, Generic[T]): + key: T + group: list[T] + Backward-compatible usage:: Employee = NamedTuple('Employee', [('name', str), ('id', int)]) @@ -1633,6 +1639,9 @@ These are not used in annotations. They are building blocks for declaring types. Removed the ``_field_types`` attribute in favor of the more standard ``__annotations__`` attribute which has the same information. + .. versionchanged:: 3.11 + Added support for generic namedtuples. + .. class:: NewType(name, tp) A helper class to indicate a distinct type to a typechecker, diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 0a8ba1e..80ce462 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -715,6 +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. + (Contributed by Serhiy Storchaka in :issue:`43923`.) + + unicodedata ----------- |