From ff33fc77b47343f878ad16ad53fb58615b7cd254 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 14 Jul 2022 13:46:53 +0100 Subject: [PATCH 1/7] Add `typing_extensions.NamedTuple` Fixes https://github.com/python/typing_extensions/issues/56 --- stdlib/typing_extensions.pyi | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 50eb5bb8368a..269f58f7687d 100644 --- a/stdlib/typing_extensions.pyi +++ b/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, @@ -54,6 +56,7 @@ __all__ = [ "Counter", "Deque", "DefaultDict", + "NamedTuple", "OrderedDict", "TypedDict", "SupportsIndex", @@ -196,6 +199,7 @@ else: if sys.version_info >= (3, 11): from typing import ( LiteralString as LiteralString, + NamedTuple as NamedTuple, # Ability to create generic NamedTuples is new in 3.11 Never as Never, NotRequired as NotRequired, Required as Required, @@ -237,3 +241,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[_T], iterable: Iterable[Any]) -> _T: ... + 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: ... From e6a4b5c111ecb17d87cca60cc83b42ed128acad7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 Jul 2022 12:48:05 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/typing_extensions.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 269f58f7687d..07754eb13460 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -2,7 +2,7 @@ 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 collections.abc import Iterable from typing import ( # noqa: Y022,Y027,Y039 TYPE_CHECKING as TYPE_CHECKING, Any, @@ -197,9 +197,9 @@ else: # New things in 3.11 if sys.version_info >= (3, 11): + from typing import NamedTuple as NamedTuple # Ability to create generic NamedTuples is new in 3.11 from typing import ( LiteralString as LiteralString, - NamedTuple as NamedTuple, # Ability to create generic NamedTuples is new in 3.11 Never as Never, NotRequired as NotRequired, Required as Required, From d14b1573c7819088bb35ccd8ae4960121b37f055 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 14 Jul 2022 13:51:47 +0100 Subject: [PATCH 3/7] Update stdlib/typing_extensions.pyi --- stdlib/typing_extensions.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 07754eb13460..c99f0f168228 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -255,7 +255,7 @@ else: @overload def __init__(self, typename: str, fields: None = ..., **kwargs: Any) -> None: ... @classmethod - def _make(cls: type[_T], iterable: Iterable[Any]) -> _T: ... + def _make(cls: type[TypeshedSelf], iterable: Iterable[Any]) -> TypeshedSelf: ... if sys.version_info >= (3, 8): def _asdict(self) -> dict[str, Any]: ... else: From 64c86a442adee3f887fb4e363bd9e96a7f121ee4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 Jul 2022 12:52:46 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/typing_extensions.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index c99f0f168228..32108d17337e 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -197,9 +197,9 @@ else: # New things in 3.11 if sys.version_info >= (3, 11): - from typing import NamedTuple as NamedTuple # Ability to create generic NamedTuples is new in 3.11 - from typing import ( + from typing import ( # Ability to create generic NamedTuples is new in 3.11 LiteralString as LiteralString, + NamedTuple as NamedTuple, Never as Never, NotRequired as NotRequired, Required as Required, @@ -241,7 +241,7 @@ 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] From 6f42874538db3705c3f7ca70d1d4664b4074674d Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 14 Jul 2022 13:55:45 +0100 Subject: [PATCH 5/7] Update stdlib/typing_extensions.pyi --- stdlib/typing_extensions.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 32108d17337e..6bd5badd6cac 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -197,8 +197,9 @@ else: # New things in 3.11 if sys.version_info >= (3, 11): - from typing import ( # Ability to create generic NamedTuples is new in 3.11 + from typing import ( LiteralString as LiteralString, + # Ability to create generic NamedTuples is new in 3.11 NamedTuple as NamedTuple, Never as Never, NotRequired as NotRequired, From 1bb5f699d190d13e814b3077bcca65fb2d401f0f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 Jul 2022 12:56:48 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/typing_extensions.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 6bd5badd6cac..32108d17337e 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -197,9 +197,8 @@ else: # New things in 3.11 if sys.version_info >= (3, 11): - from typing import ( + from typing import ( # Ability to create generic NamedTuples is new in 3.11 LiteralString as LiteralString, - # Ability to create generic NamedTuples is new in 3.11 NamedTuple as NamedTuple, Never as Never, NotRequired as NotRequired, From 3cfcdc99123ec07ec312985e666ff8b7aa49bece Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 14 Jul 2022 13:59:36 +0100 Subject: [PATCH 7/7] One final go at battling black/isort --- stdlib/typing_extensions.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 32108d17337e..0d82ec720c25 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -196,8 +196,9 @@ 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 ( # Ability to create generic NamedTuples is new in 3.11 + from typing import ( LiteralString as LiteralString, NamedTuple as NamedTuple, Never as Never,