Skip to content

Commit d06dcf0

Browse files
authored
[0.970 backport] add typing_extensions.NamedTuple (#13149)
python/typeshed#8295
1 parent 9f99400 commit d06dcf0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

mypy/typeshed/stdlib/typing_extensions.pyi

+26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import abc
2+
import collections
23
import sys
34
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
46
from typing import ( # noqa: Y022,Y027,Y039
57
TYPE_CHECKING as TYPE_CHECKING,
68
Any,
@@ -52,6 +54,7 @@ __all__ = [
5254
"Counter",
5355
"Deque",
5456
"DefaultDict",
57+
"NamedTuple",
5558
"OrderedDict",
5659
"TypedDict",
5760
"SupportsIndex",
@@ -189,9 +192,11 @@ else:
189192
def is_typeddict(tp: object) -> bool: ...
190193

191194
# New things in 3.11
195+
# NamedTuples are not new, but the ability to create generic NamedTuples is new in 3.11
192196
if sys.version_info >= (3, 11):
193197
from typing import (
194198
LiteralString as LiteralString,
199+
NamedTuple as NamedTuple,
195200
Never as Never,
196201
NotRequired as NotRequired,
197202
Required as Required,
@@ -233,3 +238,24 @@ else:
233238
field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = ...,
234239
**kwargs: object,
235240
) -> 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

Comments
 (0)