Skip to content

Commit ff33fc7

Browse files
authored
Add typing_extensions.NamedTuple
Fixes python/typing_extensions#56
1 parent 9645dae commit ff33fc7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

stdlib/typing_extensions.pyi

Lines changed: 25 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",
@@ -196,6 +199,7 @@ else:
196199
if sys.version_info >= (3, 11):
197200
from typing import (
198201
LiteralString as LiteralString,
202+
NamedTuple as NamedTuple, # Ability to create generic NamedTuples is new in 3.11
199203
Never as Never,
200204
NotRequired as NotRequired,
201205
Required as Required,
@@ -237,3 +241,24 @@ else:
237241
field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = ...,
238242
**kwargs: object,
239243
) -> IdentityFunction: ...
244+
245+
class NamedTuple(tuple[Any, ...]):
246+
if sys.version_info < (3, 8):
247+
_field_types: collections.OrderedDict[str, type]
248+
elif sys.version_info < (3, 9):
249+
_field_types: dict[str, type]
250+
_field_defaults: dict[str, Any]
251+
_fields: tuple[str, ...]
252+
_source: str
253+
@overload
254+
def __init__(self, typename: str, fields: Iterable[tuple[str, Any]] = ...) -> None: ...
255+
@overload
256+
def __init__(self, typename: str, fields: None = ..., **kwargs: Any) -> None: ...
257+
@classmethod
258+
def _make(cls: type[_T], iterable: Iterable[Any]) -> _T: ...
259+
if sys.version_info >= (3, 8):
260+
def _asdict(self) -> dict[str, Any]: ...
261+
else:
262+
def _asdict(self) -> collections.OrderedDict[str, Any]: ...
263+
264+
def _replace(self: TypeshedSelf, **kwargs: Any) -> TypeshedSelf: ...

0 commit comments

Comments
 (0)