|
1 | 1 | import abc
|
| 2 | +import collections |
2 | 3 | import sys
|
3 | 4 | from _typeshed import IdentityFunction, Self as TypeshedSelf # see #6932 for why the Self alias cannot have a leading underscore
|
| 5 | +from collections.abc import Iterable |
4 | 6 | from typing import ( # noqa: Y022,Y027,Y039
|
5 | 7 | TYPE_CHECKING as TYPE_CHECKING,
|
6 | 8 | Any,
|
@@ -52,6 +54,7 @@ __all__ = [
|
52 | 54 | "Counter",
|
53 | 55 | "Deque",
|
54 | 56 | "DefaultDict",
|
| 57 | + "NamedTuple", |
55 | 58 | "OrderedDict",
|
56 | 59 | "TypedDict",
|
57 | 60 | "SupportsIndex",
|
@@ -189,9 +192,11 @@ else:
|
189 | 192 | def is_typeddict(tp: object) -> bool: ...
|
190 | 193 |
|
191 | 194 | # New things in 3.11
|
| 195 | +# NamedTuples are not new, but the ability to create generic NamedTuples is new in 3.11 |
192 | 196 | if sys.version_info >= (3, 11):
|
193 | 197 | from typing import (
|
194 | 198 | LiteralString as LiteralString,
|
| 199 | + NamedTuple as NamedTuple, |
195 | 200 | Never as Never,
|
196 | 201 | NotRequired as NotRequired,
|
197 | 202 | Required as Required,
|
@@ -233,3 +238,24 @@ else:
|
233 | 238 | field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = ...,
|
234 | 239 | **kwargs: object,
|
235 | 240 | ) -> IdentityFunction: ...
|
| 241 | + |
| 242 | + class NamedTuple(tuple[Any, ...]): |
| 243 | + if sys.version_info < (3, 8): |
| 244 | + _field_types: collections.OrderedDict[str, type] |
| 245 | + elif sys.version_info < (3, 9): |
| 246 | + _field_types: dict[str, type] |
| 247 | + _field_defaults: dict[str, Any] |
| 248 | + _fields: tuple[str, ...] |
| 249 | + _source: str |
| 250 | + @overload |
| 251 | + def __init__(self, typename: str, fields: Iterable[tuple[str, Any]] = ...) -> None: ... |
| 252 | + @overload |
| 253 | + def __init__(self, typename: str, fields: None = ..., **kwargs: Any) -> None: ... |
| 254 | + @classmethod |
| 255 | + def _make(cls: type[TypeshedSelf], iterable: Iterable[Any]) -> TypeshedSelf: ... |
| 256 | + if sys.version_info >= (3, 8): |
| 257 | + def _asdict(self) -> dict[str, Any]: ... |
| 258 | + else: |
| 259 | + def _asdict(self) -> collections.OrderedDict[str, Any]: ... |
| 260 | + |
| 261 | + def _replace(self: TypeshedSelf, **kwargs: Any) -> TypeshedSelf: ... |
0 commit comments