From 21ad21fbea049cec0b23b197e0a98f3078b89e08 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 20 Aug 2020 14:42:06 +0300 Subject: [PATCH 1/5] support anything with .keys() and __getitem__ in dict.__init__ --- stdlib/2and3/_typeshed/__init__.pyi | 6 +++++- stdlib/2and3/builtins.pyi | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/stdlib/2and3/_typeshed/__init__.pyi b/stdlib/2and3/_typeshed/__init__.pyi index 1236bbdd9c44..332cbc1fb117 100644 --- a/stdlib/2and3/_typeshed/__init__.pyi +++ b/stdlib/2and3/_typeshed/__init__.pyi @@ -15,7 +15,7 @@ import array import mmap import sys -from typing import AbstractSet, Container, Protocol, Text, Tuple, TypeVar, Union +from typing import AbstractSet, Container, Iterable, Protocol, Text, Tuple, TypeVar, Union from typing_extensions import Literal _KT_co = TypeVar("_KT_co", covariant=True) @@ -30,6 +30,10 @@ _T_contra = TypeVar("_T_contra", contravariant=True) class SupportsItems(Protocol[_KT_co, _VT_co]): def items(self) -> AbstractSet[Tuple[_KT_co, _VT_co]]: ... +class SupportsKeysAndGetItem(Protocol[_KT_contra, _VT_co]): + def keys(self) -> Iterable[_KT_contra]: ... + def __getitem__(self, __k: _KT_contra) -> _VT_co: ... + class SupportsGetItem(Container[_KT_contra], Protocol[_KT_contra, _VT_co]): def __getitem__(self, __k: _KT_contra) -> _VT_co: ... diff --git a/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index ae98b2b0edad..c68a05d7ea7e 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.pyi @@ -10,6 +10,7 @@ from _typeshed import ( OpenBinaryModeWriting, OpenTextMode, ReadableBuffer, + SupportsKeysAndGetItem, SupportsWrite, ) from abc import ABCMeta @@ -1001,7 +1002,7 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): @overload def __init__(self, **kwargs: _VT) -> None: ... @overload - def __init__(self, map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... + def __init__(self, map: SupportsKeysAndGetItem[_KT, _VT], **kwargs: _VT) -> None: ... @overload def __init__(self, iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... def __new__(cls: Type[_T1], *args: Any, **kwargs: Any) -> _T1: ... From 156b02fb7a89d86a7e150511527eb2b6d313a256 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 20 Aug 2020 14:49:48 +0300 Subject: [PATCH 2/5] _KT_contra --> _KT_co --- stdlib/2and3/_typeshed/__init__.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/2and3/_typeshed/__init__.pyi b/stdlib/2and3/_typeshed/__init__.pyi index 332cbc1fb117..6ea3e818bb7c 100644 --- a/stdlib/2and3/_typeshed/__init__.pyi +++ b/stdlib/2and3/_typeshed/__init__.pyi @@ -30,9 +30,9 @@ _T_contra = TypeVar("_T_contra", contravariant=True) class SupportsItems(Protocol[_KT_co, _VT_co]): def items(self) -> AbstractSet[Tuple[_KT_co, _VT_co]]: ... -class SupportsKeysAndGetItem(Protocol[_KT_contra, _VT_co]): - def keys(self) -> Iterable[_KT_contra]: ... - def __getitem__(self, __k: _KT_contra) -> _VT_co: ... +class SupportsKeysAndGetItem(Protocol[_KT_co, _VT_co]): + def keys(self) -> Iterable[_KT_co]: ... + def __getitem__(self, __k: _KT_co) -> _VT_co: ... class SupportsGetItem(Container[_KT_contra], Protocol[_KT_contra, _VT_co]): def __getitem__(self, __k: _KT_contra) -> _VT_co: ... From 6780f050a2db83e6a6ff6fe4fe72e12c34ec3a96 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 20 Aug 2020 14:52:12 +0300 Subject: [PATCH 3/5] _KT_co --> _KT --- stdlib/2and3/_typeshed/__init__.pyi | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stdlib/2and3/_typeshed/__init__.pyi b/stdlib/2and3/_typeshed/__init__.pyi index 6ea3e818bb7c..10aa178da120 100644 --- a/stdlib/2and3/_typeshed/__init__.pyi +++ b/stdlib/2and3/_typeshed/__init__.pyi @@ -18,6 +18,7 @@ import sys from typing import AbstractSet, Container, Iterable, Protocol, Text, Tuple, TypeVar, Union from typing_extensions import Literal +_KT = TypeVar("_KT", covariant=True) _KT_co = TypeVar("_KT_co", covariant=True) _KT_contra = TypeVar("_KT_contra", contravariant=True) _VT = TypeVar("_VT") @@ -30,9 +31,9 @@ _T_contra = TypeVar("_T_contra", contravariant=True) class SupportsItems(Protocol[_KT_co, _VT_co]): def items(self) -> AbstractSet[Tuple[_KT_co, _VT_co]]: ... -class SupportsKeysAndGetItem(Protocol[_KT_co, _VT_co]): - def keys(self) -> Iterable[_KT_co]: ... - def __getitem__(self, __k: _KT_co) -> _VT_co: ... +class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]): + def keys(self) -> Iterable[_KT]: ... + def __getitem__(self, __k: _KT) -> _VT_co: ... class SupportsGetItem(Container[_KT_contra], Protocol[_KT_contra, _VT_co]): def __getitem__(self, __k: _KT_contra) -> _VT_co: ... From fccf26cb284958c01682d84c83ff7930bc51767e Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 20 Aug 2020 14:53:27 +0300 Subject: [PATCH 4/5] fix copy/pasta --- stdlib/2and3/_typeshed/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/2and3/_typeshed/__init__.pyi b/stdlib/2and3/_typeshed/__init__.pyi index 10aa178da120..c66e380efe80 100644 --- a/stdlib/2and3/_typeshed/__init__.pyi +++ b/stdlib/2and3/_typeshed/__init__.pyi @@ -18,7 +18,7 @@ import sys from typing import AbstractSet, Container, Iterable, Protocol, Text, Tuple, TypeVar, Union from typing_extensions import Literal -_KT = TypeVar("_KT", covariant=True) +_KT = TypeVar("_KT") _KT_co = TypeVar("_KT_co", covariant=True) _KT_contra = TypeVar("_KT_contra", contravariant=True) _VT = TypeVar("_VT") From e22af3069610de2c64640bd39115c96153ab8fde Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 20 Aug 2020 17:07:24 +0300 Subject: [PATCH 5/5] copy file --- stdlib/2/__builtin__.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index ae98b2b0edad..c68a05d7ea7e 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -10,6 +10,7 @@ from _typeshed import ( OpenBinaryModeWriting, OpenTextMode, ReadableBuffer, + SupportsKeysAndGetItem, SupportsWrite, ) from abc import ABCMeta @@ -1001,7 +1002,7 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): @overload def __init__(self, **kwargs: _VT) -> None: ... @overload - def __init__(self, map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... + def __init__(self, map: SupportsKeysAndGetItem[_KT, _VT], **kwargs: _VT) -> None: ... @overload def __init__(self, iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... def __new__(cls: Type[_T1], *args: Any, **kwargs: Any) -> _T1: ...