From aae92ca6a53cc8cacf7cc0898da6e84cf5e29f01 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 19 Nov 2023 10:19:42 +0300 Subject: [PATCH 1/5] Add `NamedTupleMeta` metaclass --- stdlib/typing.pyi | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 7694157d70fe..fb4f4becf194 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -2,7 +2,7 @@ import collections # Needed by aliases like DefaultDict, see mypy issue 2986 import sys import typing_extensions from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import IdentityFunction, Incomplete, ReadableBuffer, SupportsKeysAndGetItem +from _typeshed import IdentityFunction, Incomplete, ReadableBuffer, SupportsKeysAndGetItem, Self as _typeshed_Self from abc import ABCMeta, abstractmethod from contextlib import AbstractAsyncContextManager, AbstractContextManager from re import Match as Match, Pattern as Pattern @@ -859,7 +859,10 @@ if sys.version_info >= (3, 11): # Type constructors -class NamedTuple(tuple[Any, ...]): +class NamedTupleMeta(type): + def __new__(cls: type[_typeshed_Self], typename: str, bases: tuple[type, ...], ns: dict[str, Any]) -> _typeshed_Self: ... + +class NamedTuple(tuple[Any, ...], metaclass=NamedTupleMeta): if sys.version_info < (3, 8): _field_types: ClassVar[collections.OrderedDict[str, type]] elif sys.version_info < (3, 9): From 338441a2322291b22a1517170dfd1d3e31829046 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:22:56 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/typing.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index fb4f4becf194..8ba7c719ce91 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -2,7 +2,7 @@ import collections # Needed by aliases like DefaultDict, see mypy issue 2986 import sys import typing_extensions from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import IdentityFunction, Incomplete, ReadableBuffer, SupportsKeysAndGetItem, Self as _typeshed_Self +from _typeshed import IdentityFunction, Incomplete, ReadableBuffer, Self as _typeshed_Self, SupportsKeysAndGetItem from abc import ABCMeta, abstractmethod from contextlib import AbstractAsyncContextManager, AbstractContextManager from re import Match as Match, Pattern as Pattern From 7793f5e29ea60ac77e9a7e2633c9fc0d968167a6 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 19 Nov 2023 11:35:01 +0300 Subject: [PATCH 3/5] Use ABCMeta --- stdlib/typing.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 8ba7c719ce91..62fcb08e4b71 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -859,7 +859,7 @@ if sys.version_info >= (3, 11): # Type constructors -class NamedTupleMeta(type): +class NamedTupleMeta(ABCMeta): # We lie about `ABCMeta`, but it solves the conflict def __new__(cls: type[_typeshed_Self], typename: str, bases: tuple[type, ...], ns: dict[str, Any]) -> _typeshed_Self: ... class NamedTuple(tuple[Any, ...], metaclass=NamedTupleMeta): From e42b71802f780836d4b0a88c49d644278b9553e9 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 19 Nov 2023 11:48:13 +0300 Subject: [PATCH 4/5] Try `type: ingore` --- stdlib/typing.pyi | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 62fcb08e4b71..90063b8cfa04 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -2,7 +2,7 @@ import collections # Needed by aliases like DefaultDict, see mypy issue 2986 import sys import typing_extensions from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import IdentityFunction, Incomplete, ReadableBuffer, Self as _typeshed_Self, SupportsKeysAndGetItem +from _typeshed import IdentityFunction, Incomplete, ReadableBuffer, SupportsKeysAndGetItem from abc import ABCMeta, abstractmethod from contextlib import AbstractAsyncContextManager, AbstractContextManager from re import Match as Match, Pattern as Pattern @@ -859,10 +859,12 @@ if sys.version_info >= (3, 11): # Type constructors -class NamedTupleMeta(ABCMeta): # We lie about `ABCMeta`, but it solves the conflict - def __new__(cls: type[_typeshed_Self], typename: str, bases: tuple[type, ...], ns: dict[str, Any]) -> _typeshed_Self: ... +class NamedTupleMeta(type): + def __new__(cls, typename: str, bases: tuple[type, ...], ns: dict[str, Any]) -> typing_extensions.Self: ... -class NamedTuple(tuple[Any, ...], metaclass=NamedTupleMeta): +# We `type: ignore` here, because `tuple` base-class has `ABCMeta` in stubs, +# while in runtime it does not. So, ignoring this error. +class NamedTuple(tuple[Any, ...], metaclass=NamedTupleMeta): # type: ignore[misc] if sys.version_info < (3, 8): _field_types: ClassVar[collections.OrderedDict[str, type]] elif sys.version_info < (3, 9): From c4abab74403a2d2c1abda7873a150e268e6cb67e Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 19 Nov 2023 11:50:45 +0300 Subject: [PATCH 5/5] Fix error --- stdlib/typing.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 90063b8cfa04..d2238e07ccd7 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -2,7 +2,7 @@ import collections # Needed by aliases like DefaultDict, see mypy issue 2986 import sys import typing_extensions from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import IdentityFunction, Incomplete, ReadableBuffer, SupportsKeysAndGetItem +from _typeshed import IdentityFunction, Incomplete, ReadableBuffer, Self as _typeshed_Self, SupportsKeysAndGetItem from abc import ABCMeta, abstractmethod from contextlib import AbstractAsyncContextManager, AbstractContextManager from re import Match as Match, Pattern as Pattern @@ -860,7 +860,7 @@ if sys.version_info >= (3, 11): # Type constructors class NamedTupleMeta(type): - def __new__(cls, typename: str, bases: tuple[type, ...], ns: dict[str, Any]) -> typing_extensions.Self: ... + def __new__(cls: type[_typeshed_Self], typename: str, bases: tuple[type, ...], ns: dict[str, Any]) -> _typeshed_Self: ... # We `type: ignore` here, because `tuple` base-class has `ABCMeta` in stubs, # while in runtime it does not. So, ignoring this error.