From d06dcf0c0383a95492aef8fe46eafcff10be63c0 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sat, 16 Jul 2022 09:23:20 -0700 Subject: [PATCH] [0.970 backport] add typing_extensions.NamedTuple (#13149) https://github.com/python/typeshed/pull/8295 --- mypy/typeshed/stdlib/typing_extensions.pyi | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/mypy/typeshed/stdlib/typing_extensions.pyi b/mypy/typeshed/stdlib/typing_extensions.pyi index 38fb9dec19d9..358853549bed 100644 --- a/mypy/typeshed/stdlib/typing_extensions.pyi +++ b/mypy/typeshed/stdlib/typing_extensions.pyi @@ -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, @@ -52,6 +54,7 @@ __all__ = [ "Counter", "Deque", "DefaultDict", + "NamedTuple", "OrderedDict", "TypedDict", "SupportsIndex", @@ -189,9 +192,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, @@ -233,3 +238,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: ...