Skip to content

Commit 10f3238

Browse files
authored
Add typing_extensions.NamedTuple (#8295)
Fixes python/typing_extensions#56
1 parent 9645dae commit 10f3238

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

stdlib/typing_extensions.pyi

Lines changed: 26 additions & 0 deletions
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,
@@ -54,6 +56,7 @@ __all__ = [
5456
"Counter",
5557
"Deque",
5658
"DefaultDict",
59+
"NamedTuple",
5760
"OrderedDict",
5861
"TypedDict",
5962
"SupportsIndex",
@@ -193,9 +196,11 @@ else:
193196
def is_typeddict(tp: object) -> bool: ...
194197

195198
# New things in 3.11
199+
# NamedTuples are not new, but the ability to create generic NamedTuples is new in 3.11
196200
if sys.version_info >= (3, 11):
197201
from typing import (
198202
LiteralString as LiteralString,
203+
NamedTuple as NamedTuple,
199204
Never as Never,
200205
NotRequired as NotRequired,
201206
Required as Required,
@@ -237,3 +242,24 @@ else:
237242
field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = ...,
238243
**kwargs: object,
239244
) -> IdentityFunction: ...
245+
246+
class NamedTuple(tuple[Any, ...]):
247+
if sys.version_info < (3, 8):
248+
_field_types: collections.OrderedDict[str, type]
249+
elif sys.version_info < (3, 9):
250+
_field_types: dict[str, type]
251+
_field_defaults: dict[str, Any]
252+
_fields: tuple[str, ...]
253+
_source: str
254+
@overload
255+
def __init__(self, typename: str, fields: Iterable[tuple[str, Any]] = ...) -> None: ...
256+
@overload
257+
def __init__(self, typename: str, fields: None = ..., **kwargs: Any) -> None: ...
258+
@classmethod
259+
def _make(cls: type[TypeshedSelf], iterable: Iterable[Any]) -> TypeshedSelf: ...
260+
if sys.version_info >= (3, 8):
261+
def _asdict(self) -> dict[str, Any]: ...
262+
else:
263+
def _asdict(self) -> collections.OrderedDict[str, Any]: ...
264+
265+
def _replace(self: TypeshedSelf, **kwargs: Any) -> TypeshedSelf: ...

0 commit comments

Comments
 (0)