bpo-43923: Add support for generic typing.NamedTuple (#92027)

This commit is contained in:
Serhiy Storchaka 2022-05-03 01:41:23 +03:00 committed by GitHub
parent 81fb3548be
commit b04e02c57f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 4 deletions

View file

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