Add typing_extensions.NamedTuple (#8295)

Fixes https://github.com/python/typing_extensions/issues/56
This commit is contained in:
Alex Waygood
2022-07-14 15:02:51 +01:00
committed by GitHub
parent 9645dae925
commit 10f3238998

View File

@@ -1,6 +1,8 @@
import abc
import collections
import sys
from _typeshed import IdentityFunction, Self as TypeshedSelf # see #6932 for why the Self alias cannot have a leading underscore
from collections.abc import Iterable
from typing import ( # noqa: Y022,Y027,Y039
TYPE_CHECKING as TYPE_CHECKING,
Any,
@@ -54,6 +56,7 @@ __all__ = [
"Counter",
"Deque",
"DefaultDict",
"NamedTuple",
"OrderedDict",
"TypedDict",
"SupportsIndex",
@@ -193,9 +196,11 @@ else:
def is_typeddict(tp: object) -> bool: ...
# New things in 3.11
# NamedTuples are not new, but the ability to create generic NamedTuples is new in 3.11
if sys.version_info >= (3, 11):
from typing import (
LiteralString as LiteralString,
NamedTuple as NamedTuple,
Never as Never,
NotRequired as NotRequired,
Required as Required,
@@ -237,3 +242,24 @@ else:
field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = ...,
**kwargs: object,
) -> IdentityFunction: ...
class NamedTuple(tuple[Any, ...]):
if sys.version_info < (3, 8):
_field_types: collections.OrderedDict[str, type]
elif sys.version_info < (3, 9):
_field_types: dict[str, type]
_field_defaults: dict[str, Any]
_fields: tuple[str, ...]
_source: str
@overload
def __init__(self, typename: str, fields: Iterable[tuple[str, Any]] = ...) -> None: ...
@overload
def __init__(self, typename: str, fields: None = ..., **kwargs: Any) -> None: ...
@classmethod
def _make(cls: type[TypeshedSelf], iterable: Iterable[Any]) -> TypeshedSelf: ...
if sys.version_info >= (3, 8):
def _asdict(self) -> dict[str, Any]: ...
else:
def _asdict(self) -> collections.OrderedDict[str, Any]: ...
def _replace(self: TypeshedSelf, **kwargs: Any) -> TypeshedSelf: ...