From 7c1d28c3f33f89c9c377bd71fc82d79572f8eb80 Mon Sep 17 00:00:00 2001 From: lladhibhutall Date: Sun, 22 Mar 2020 22:42:07 +0530 Subject: [PATCH 1/5] Add Protocol for Mapping --- stdlib/2and3/builtins.pyi | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index 3f6e671f2cd2..449a3d510c0b 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.pyi @@ -7,7 +7,7 @@ from typing import ( Set, AbstractSet, FrozenSet, MutableSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsAbs, SupportsComplex, IO, BinaryIO, Union, ItemsView, KeysView, ValuesView, ByteString, Optional, AnyStr, Type, Text, - Protocol, + Protocol, Collection, ) from abc import abstractmethod, ABCMeta from ast import mod, AST @@ -25,7 +25,9 @@ else: _T = TypeVar('_T') _T_co = TypeVar('_T_co', covariant=True) _KT = TypeVar('_KT') +_KT_contra = TypeVar('_KT_contra', contravariant=True) _VT = TypeVar('_VT') +_VT_co = TypeVar('_VT_co', covariant=True) _S = TypeVar('_S') _T1 = TypeVar('_T1') _T2 = TypeVar('_T2') @@ -33,10 +35,13 @@ _T3 = TypeVar('_T3') _T4 = TypeVar('_T4') _T5 = TypeVar('_T5') _TT = TypeVar('_TT', bound='type') +_TC = TypeVar('_TC', bound=Type[object]) class _SupportsIndex(Protocol): def __index__(self) -> int: ... +def runtime_checkable(cls: _TC) -> _TC: ... + class object: __doc__: Optional[str] __dict__: Dict[str, Any] @@ -433,7 +438,26 @@ class str(Sequence[str], _str_base): def find(self, sub: Text, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ... def format(self, *args: object, **kwargs: object) -> str: ... if sys.version_info >= (3,): - def format_map(self, map: Mapping[str, Any]) -> str: ... + if sys.version_info >= (3, 6): + @runtime_checkable + class Collectionss(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): + # Implement Sized (but don't have it as a base class). + @abstractmethod + def __len__(self) -> int: ... + + _Collection = Collectionss + else: + @runtime_checkable + class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): + # Implement Sized (but don't have it as a base class). + @abstractmethod + def __len__(self) -> int: ... + class _Mapping(Protocol, Collection[_KT], Generic[_KT, _VT_co]): + # TODO: Cant make key type as covariant + # see discussion in https://github.com/python/typing/pull/273 + @abstractmethod + def __getitem__(self, k: _KT) -> _VT_co: ... + def format_map(self, map: _Mapping[str, Any]) -> str: ... def index(self, sub: Text, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ... def isalnum(self) -> bool: ... def isalpha(self) -> bool: ... From 449ca99576721745cd9ae1769922adb719cf3804 Mon Sep 17 00:00:00 2001 From: lladhibhutall Date: Mon, 23 Mar 2020 01:03:22 +0530 Subject: [PATCH 2/5] restructure --- stdlib/2and3/builtins.pyi | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index 449a3d510c0b..277e231223da 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.pyi @@ -7,7 +7,7 @@ from typing import ( Set, AbstractSet, FrozenSet, MutableSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsAbs, SupportsComplex, IO, BinaryIO, Union, ItemsView, KeysView, ValuesView, ByteString, Optional, AnyStr, Type, Text, - Protocol, Collection, + Protocol, ) from abc import abstractmethod, ABCMeta from ast import mod, AST @@ -40,7 +40,22 @@ _TC = TypeVar('_TC', bound=Type[object]) class _SupportsIndex(Protocol): def __index__(self) -> int: ... -def runtime_checkable(cls: _TC) -> _TC: ... +def runtime_checkable(cls: _TC) -> _TC: ... + +if sys.version_info >= (3, 6): + @runtime_checkable + class Collections(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): + # Implement Sized (but don't have it as a base class). + @abstractmethod + def __len__(self) -> int: ... + + _Collection = Collections +else: + @runtime_checkable + class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): + # Implement Sized (but don't have it as a base class). + @abstractmethod + def __len__(self) -> int: ... class object: __doc__: Optional[str] @@ -438,21 +453,7 @@ class str(Sequence[str], _str_base): def find(self, sub: Text, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ... def format(self, *args: object, **kwargs: object) -> str: ... if sys.version_info >= (3,): - if sys.version_info >= (3, 6): - @runtime_checkable - class Collectionss(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): - # Implement Sized (but don't have it as a base class). - @abstractmethod - def __len__(self) -> int: ... - - _Collection = Collectionss - else: - @runtime_checkable - class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): - # Implement Sized (but don't have it as a base class). - @abstractmethod - def __len__(self) -> int: ... - class _Mapping(Protocol, Collection[_KT], Generic[_KT, _VT_co]): + class _Mapping(Protocol, _Collection[_KT], Generic[_KT, _VT_co]): # TODO: Cant make key type as covariant # see discussion in https://github.com/python/typing/pull/273 @abstractmethod From 486d47047dc9ab0b9468099c91bd378a03763925 Mon Sep 17 00:00:00 2001 From: lladhibhutall Date: Mon, 23 Mar 2020 01:07:57 +0530 Subject: [PATCH 3/5] Remove _KT_contra --- stdlib/2and3/builtins.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index 277e231223da..6ebcc7ca8219 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.pyi @@ -25,7 +25,6 @@ else: _T = TypeVar('_T') _T_co = TypeVar('_T_co', covariant=True) _KT = TypeVar('_KT') -_KT_contra = TypeVar('_KT_contra', contravariant=True) _VT = TypeVar('_VT') _VT_co = TypeVar('_VT_co', covariant=True) _S = TypeVar('_S') From 59c61462d65a2cf5001469e5f8ba805d2cc50a6d Mon Sep 17 00:00:00 2001 From: lladhibhutall Date: Mon, 23 Mar 2020 02:20:13 +0530 Subject: [PATCH 4/5] Add stubs for file consistency and whitelist --- stdlib/2/__builtin__.pyi | 26 +++++++++++++++++++++++- tests/stubtest_whitelists/py3_common.txt | 3 +++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index 3f6e671f2cd2..6ebcc7ca8219 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -26,6 +26,7 @@ _T = TypeVar('_T') _T_co = TypeVar('_T_co', covariant=True) _KT = TypeVar('_KT') _VT = TypeVar('_VT') +_VT_co = TypeVar('_VT_co', covariant=True) _S = TypeVar('_S') _T1 = TypeVar('_T1') _T2 = TypeVar('_T2') @@ -33,10 +34,28 @@ _T3 = TypeVar('_T3') _T4 = TypeVar('_T4') _T5 = TypeVar('_T5') _TT = TypeVar('_TT', bound='type') +_TC = TypeVar('_TC', bound=Type[object]) class _SupportsIndex(Protocol): def __index__(self) -> int: ... +def runtime_checkable(cls: _TC) -> _TC: ... + +if sys.version_info >= (3, 6): + @runtime_checkable + class Collections(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): + # Implement Sized (but don't have it as a base class). + @abstractmethod + def __len__(self) -> int: ... + + _Collection = Collections +else: + @runtime_checkable + class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): + # Implement Sized (but don't have it as a base class). + @abstractmethod + def __len__(self) -> int: ... + class object: __doc__: Optional[str] __dict__: Dict[str, Any] @@ -433,7 +452,12 @@ class str(Sequence[str], _str_base): def find(self, sub: Text, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ... def format(self, *args: object, **kwargs: object) -> str: ... if sys.version_info >= (3,): - def format_map(self, map: Mapping[str, Any]) -> str: ... + class _Mapping(Protocol, _Collection[_KT], Generic[_KT, _VT_co]): + # TODO: Cant make key type as covariant + # see discussion in https://github.com/python/typing/pull/273 + @abstractmethod + def __getitem__(self, k: _KT) -> _VT_co: ... + def format_map(self, map: _Mapping[str, Any]) -> str: ... def index(self, sub: Text, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ... def isalnum(self) -> bool: ... def isalpha(self) -> bool: ... diff --git a/tests/stubtest_whitelists/py3_common.txt b/tests/stubtest_whitelists/py3_common.txt index d6fafc92830b..6c43a9b2e6ca 100644 --- a/tests/stubtest_whitelists/py3_common.txt +++ b/tests/stubtest_whitelists/py3_common.txt @@ -202,6 +202,9 @@ builtins.quit builtins.reveal_locals builtins.reveal_type builtins.staticmethod.__get__ +builtins.str._Mapping +builtins.runtime_checkable +builtins.Collections bz2.BZ2File.readinto bz2.BZ2File.readlines bz2.BZ2File.write From d728d0fbc3d9fa328e08e195fdcccc2cea6497de Mon Sep 17 00:00:00 2001 From: lladhibhutall Date: Mon, 23 Mar 2020 02:30:07 +0530 Subject: [PATCH 5/5] Move builtins.Collections to respective whitelist file --- tests/stubtest_whitelists/py36.txt | 1 + tests/stubtest_whitelists/py37.txt | 1 + tests/stubtest_whitelists/py38.txt | 1 + tests/stubtest_whitelists/py3_common.txt | 1 - 4 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/stubtest_whitelists/py36.txt b/tests/stubtest_whitelists/py36.txt index 778563040edb..193fd0bc5e7c 100644 --- a/tests/stubtest_whitelists/py36.txt +++ b/tests/stubtest_whitelists/py36.txt @@ -13,6 +13,7 @@ asyncio.protocols.BufferedProtocol asyncio.runners asyncio.tasks.Task._wakeup builtins.str.maketrans +builtins.Collections cmath.log codecs.StreamRecoder.seek collections.AsyncGenerator.ag_await diff --git a/tests/stubtest_whitelists/py37.txt b/tests/stubtest_whitelists/py37.txt index 4e5636538683..a3a3ff97df02 100644 --- a/tests/stubtest_whitelists/py37.txt +++ b/tests/stubtest_whitelists/py37.txt @@ -26,6 +26,7 @@ asyncio.selector_events.BaseSelectorEventLoop.create_unix_server builtins.dict.get builtins.reversed builtins.str.maketrans +builtins.Collections cmath.log collections.AsyncGenerator.ag_await collections.AsyncGenerator.ag_code diff --git a/tests/stubtest_whitelists/py38.txt b/tests/stubtest_whitelists/py38.txt index a1bc6b042394..1e272d6c7ad4 100644 --- a/tests/stubtest_whitelists/py38.txt +++ b/tests/stubtest_whitelists/py38.txt @@ -48,6 +48,7 @@ builtins.bytearray.pop builtins.compile builtins.dict.get builtins.reversed +builtins.Collections bz2.BZ2Compressor.compress bz2.BZ2File.read bz2.BZ2File.read1 diff --git a/tests/stubtest_whitelists/py3_common.txt b/tests/stubtest_whitelists/py3_common.txt index 6c43a9b2e6ca..76a52a69d14b 100644 --- a/tests/stubtest_whitelists/py3_common.txt +++ b/tests/stubtest_whitelists/py3_common.txt @@ -204,7 +204,6 @@ builtins.reveal_type builtins.staticmethod.__get__ builtins.str._Mapping builtins.runtime_checkable -builtins.Collections bz2.BZ2File.readinto bz2.BZ2File.readlines bz2.BZ2File.write