@@ -1615,6 +1615,12 @@ These are not used in annotations. They are building blocks for declaring types.
1615
1615
def __repr__(self) -> str:
1616
1616
return f'<Employee {self.name}, id={self.id}>'
1617
1617
1618
+ ``NamedTuple `` subclasses can be generic::
1619
+
1620
+ class Group(NamedTuple, Generic[T]):
1621
+ key: T
1622
+ group: list[T]
1623
+
1618
1624
Backward-compatible usage::
1619
1625
1620
1626
Employee = NamedTuple('Employee', [('name', str), ('id', int)])
@@ -1633,6 +1639,9 @@ These are not used in annotations. They are building blocks for declaring types.
1633
1639
Removed the ``_field_types `` attribute in favor of the more
1634
1640
standard ``__annotations__ `` attribute which has the same information.
1635
1641
1642
+ .. versionchanged :: 3.11
1643
+ Added support for generic namedtuples.
1644
+
1636
1645
.. class :: NewType(name, tp)
1637
1646
1638
1647
A helper class to indicate a distinct type to a typechecker,
@@ -1729,7 +1738,7 @@ These are not used in annotations. They are building blocks for declaring types.
1729
1738
z: int
1730
1739
1731
1740
A ``TypedDict `` cannot inherit from a non-TypedDict class,
1732
- notably including :class: `Generic `. For example::
1741
+ except for :class: `Generic `. For example::
1733
1742
1734
1743
class X(TypedDict):
1735
1744
x: int
@@ -1746,6 +1755,12 @@ These are not used in annotations. They are building blocks for declaring types.
1746
1755
T = TypeVar('T')
1747
1756
class XT(X, Generic[T]): pass # raises TypeError
1748
1757
1758
+ A ``TypedDict `` can be generic::
1759
+
1760
+ class Group(TypedDict, Generic[T]):
1761
+ key: T
1762
+ group: list[T]
1763
+
1749
1764
A ``TypedDict `` can be introspected via annotations dicts
1750
1765
(see :ref: `annotations-howto ` for more information on annotations best practices),
1751
1766
:attr: `__total__ `, :attr: `__required_keys__ `, and :attr: `__optional_keys__ `.
@@ -1793,6 +1808,9 @@ These are not used in annotations. They are building blocks for declaring types.
1793
1808
1794
1809
.. versionadded :: 3.8
1795
1810
1811
+ .. versionchanged :: 3.11
1812
+ Added support for generic ``TypedDict ``\ s.
1813
+
1796
1814
Generic concrete collections
1797
1815
----------------------------
1798
1816
0 commit comments