mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-43923: Add support for generic typing.NamedTuple (#92027)
This commit is contained in:
parent
81fb3548be
commit
b04e02c57f
5 changed files with 66 additions and 4 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue