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/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index 3f6e671f2cd2..6ebcc7ca8219 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.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/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 d6fafc92830b..76a52a69d14b 100644 --- a/tests/stubtest_whitelists/py3_common.txt +++ b/tests/stubtest_whitelists/py3_common.txt @@ -202,6 +202,8 @@ builtins.quit builtins.reveal_locals builtins.reveal_type builtins.staticmethod.__get__ +builtins.str._Mapping +builtins.runtime_checkable bz2.BZ2File.readinto bz2.BZ2File.readlines bz2.BZ2File.write