Skip to content

Adding Protocol _Mapping for format_map method #3877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion stdlib/2/__builtin__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,36 @@ _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')
_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: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you adding this (and Collections) to builtins? They don't exist in builtins at runtime.

Copy link
Contributor Author

@djb4ai djb4ai Mar 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for correcting, I wasn't sure about this, the original stubs for Mapping had those if I just use Protocol[_KT, _VT_co] it keeps saying Invariant type variable '_KT' used in protocol where contravariant one is expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well if nothing else they should be imported from typing.


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]
Expand Down Expand Up @@ -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: ...
Expand Down
26 changes: 25 additions & 1 deletion stdlib/2and3/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,36 @@ _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')
_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]
Expand Down Expand Up @@ -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: ...
Expand Down
1 change: 1 addition & 0 deletions tests/stubtest_whitelists/py36.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/stubtest_whitelists/py37.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/stubtest_whitelists/py38.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/stubtest_whitelists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down