Skip to content

Use native SupportsIndex in builtins #5229

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

Merged
merged 1 commit into from
Apr 21, 2021
Merged
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
83 changes: 40 additions & 43 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,11 @@ from typing import (
overload,
runtime_checkable,
)
from typing_extensions import Literal
from typing_extensions import Literal, SupportsIndex

if sys.version_info >= (3, 9):
from types import GenericAlias

class _SupportsIndex(Protocol):
def __index__(self) -> int: ...

class _SupportsTrunc(Protocol):
def __trunc__(self) -> int: ...

Expand Down Expand Up @@ -170,7 +167,7 @@ class super(object):

class int:
@overload
def __new__(cls: Type[_T], x: Union[str, bytes, SupportsInt, _SupportsIndex, _SupportsTrunc] = ...) -> _T: ...
def __new__(cls: Type[_T], x: Union[str, bytes, SupportsInt, SupportsIndex, _SupportsTrunc] = ...) -> _T: ...
@overload
def __new__(cls: Type[_T], x: Union[str, bytes, bytearray], base: int) -> _T: ...
if sys.version_info >= (3, 8):
Expand Down Expand Up @@ -242,7 +239,7 @@ class int:
def __index__(self) -> int: ...

class float:
def __new__(cls: Type[_T], x: Union[SupportsFloat, _SupportsIndex, str, bytes, bytearray] = ...) -> _T: ...
def __new__(cls: Type[_T], x: Union[SupportsFloat, SupportsIndex, str, bytes, bytearray] = ...) -> _T: ...
def as_integer_ratio(self) -> Tuple[int, int]: ...
def hex(self) -> str: ...
def is_integer(self) -> bool: ...
Expand Down Expand Up @@ -299,7 +296,7 @@ class complex:
@overload
def __new__(cls: Type[_T], real: float = ..., imag: float = ...) -> _T: ...
@overload
def __new__(cls: Type[_T], real: Union[str, SupportsComplex, _SupportsIndex]) -> _T: ...
def __new__(cls: Type[_T], real: Union[str, SupportsComplex, SupportsIndex]) -> _T: ...
@property
def real(self) -> float: ...
@property
Expand Down Expand Up @@ -335,19 +332,19 @@ class str(Sequence[str]):
def capitalize(self) -> str: ...
def casefold(self) -> str: ...
def center(self, __width: int, __fillchar: str = ...) -> str: ...
def count(self, x: str, __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...) -> int: ...
def count(self, x: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
def encode(self, encoding: str = ..., errors: str = ...) -> bytes: ...
def endswith(
self,
__suffix: Union[str, Tuple[str, ...]],
__start: Optional[_SupportsIndex] = ...,
__end: Optional[_SupportsIndex] = ...,
__start: Optional[SupportsIndex] = ...,
__end: Optional[SupportsIndex] = ...,
) -> bool: ...
def expandtabs(self, tabsize: int = ...) -> str: ...
def find(self, __sub: str, __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...) -> int: ...
def find(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
def format(self, *args: object, **kwargs: object) -> str: ...
def format_map(self, map: _FormatMapMapping) -> str: ...
def index(self, __sub: str, __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...) -> int: ...
def index(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
def isalnum(self) -> bool: ...
def isalpha(self) -> bool: ...
if sys.version_info >= (3, 7):
Expand All @@ -370,8 +367,8 @@ class str(Sequence[str]):
if sys.version_info >= (3, 9):
def removeprefix(self, __prefix: str) -> str: ...
def removesuffix(self, __suffix: str) -> str: ...
def rfind(self, __sub: str, __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...) -> int: ...
def rindex(self, __sub: str, __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...) -> int: ...
def rfind(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
def rindex(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
def rjust(self, __width: int, __fillchar: str = ...) -> str: ...
def rpartition(self, __sep: str) -> Tuple[str, str, str]: ...
def rsplit(self, sep: Optional[str] = ..., maxsplit: int = ...) -> List[str]: ...
Expand All @@ -381,8 +378,8 @@ class str(Sequence[str]):
def startswith(
self,
__prefix: Union[str, Tuple[str, ...]],
__start: Optional[_SupportsIndex] = ...,
__end: Optional[_SupportsIndex] = ...,
__start: Optional[SupportsIndex] = ...,
__end: Optional[SupportsIndex] = ...,
) -> bool: ...
def strip(self, __chars: Optional[str] = ...) -> str: ...
def swapcase(self) -> str: ...
Expand Down Expand Up @@ -430,25 +427,25 @@ class bytes(ByteString):
def capitalize(self) -> bytes: ...
def center(self, __width: int, __fillchar: bytes = ...) -> bytes: ...
def count(
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
) -> int: ...
def decode(self, encoding: str = ..., errors: str = ...) -> str: ...
def endswith(
self,
__suffix: Union[bytes, Tuple[bytes, ...]],
__start: Optional[_SupportsIndex] = ...,
__end: Optional[_SupportsIndex] = ...,
__start: Optional[SupportsIndex] = ...,
__end: Optional[SupportsIndex] = ...,
) -> bool: ...
def expandtabs(self, tabsize: int = ...) -> bytes: ...
def find(
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
) -> int: ...
if sys.version_info >= (3, 8):
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: int = ...) -> str: ...
else:
def hex(self) -> str: ...
def index(
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
) -> int: ...
def isalnum(self) -> bool: ...
def isalpha(self) -> bool: ...
Expand All @@ -469,10 +466,10 @@ class bytes(ByteString):
def removeprefix(self, __prefix: bytes) -> bytes: ...
def removesuffix(self, __suffix: bytes) -> bytes: ...
def rfind(
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
) -> int: ...
def rindex(
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
) -> int: ...
def rjust(self, __width: int, __fillchar: bytes = ...) -> bytes: ...
def rpartition(self, __sep: bytes) -> Tuple[bytes, bytes, bytes]: ...
Expand All @@ -483,8 +480,8 @@ class bytes(ByteString):
def startswith(
self,
__prefix: Union[bytes, Tuple[bytes, ...]],
__start: Optional[_SupportsIndex] = ...,
__end: Optional[_SupportsIndex] = ...,
__start: Optional[SupportsIndex] = ...,
__end: Optional[SupportsIndex] = ...,
) -> bool: ...
def strip(self, __bytes: Optional[bytes] = ...) -> bytes: ...
def swapcase(self) -> bytes: ...
Expand Down Expand Up @@ -532,27 +529,27 @@ class bytearray(MutableSequence[int], ByteString):
def capitalize(self) -> bytearray: ...
def center(self, __width: int, __fillchar: bytes = ...) -> bytearray: ...
def count(
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
) -> int: ...
def copy(self) -> bytearray: ...
def decode(self, encoding: str = ..., errors: str = ...) -> str: ...
def endswith(
self,
__suffix: Union[bytes, Tuple[bytes, ...]],
__start: Optional[_SupportsIndex] = ...,
__end: Optional[_SupportsIndex] = ...,
__start: Optional[SupportsIndex] = ...,
__end: Optional[SupportsIndex] = ...,
) -> bool: ...
def expandtabs(self, tabsize: int = ...) -> bytearray: ...
def extend(self, __iterable_of_ints: Iterable[int]) -> None: ...
def find(
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
) -> int: ...
if sys.version_info >= (3, 8):
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: int = ...) -> str: ...
else:
def hex(self) -> str: ...
def index(
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
) -> int: ...
def insert(self, __index: int, __item: int) -> None: ...
def isalnum(self) -> bool: ...
Expand All @@ -574,10 +571,10 @@ class bytearray(MutableSequence[int], ByteString):
def removesuffix(self, __suffix: bytes) -> bytearray: ...
def replace(self, __old: bytes, __new: bytes, __count: int = ...) -> bytearray: ...
def rfind(
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
) -> int: ...
def rindex(
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
) -> int: ...
def rjust(self, __width: int, __fillchar: bytes = ...) -> bytearray: ...
def rpartition(self, __sep: bytes) -> Tuple[bytearray, bytearray, bytearray]: ...
Expand All @@ -588,8 +585,8 @@ class bytearray(MutableSequence[int], ByteString):
def startswith(
self,
__prefix: Union[bytes, Tuple[bytes, ...]],
__start: Optional[_SupportsIndex] = ...,
__end: Optional[_SupportsIndex] = ...,
__start: Optional[SupportsIndex] = ...,
__end: Optional[SupportsIndex] = ...,
) -> bool: ...
def strip(self, __bytes: Optional[bytes] = ...) -> bytearray: ...
def swapcase(self) -> bytearray: ...
Expand Down Expand Up @@ -769,14 +766,14 @@ class list(MutableSequence[_T], Generic[_T]):
def __str__(self) -> str: ...
__hash__: None # type: ignore
@overload
def __getitem__(self, i: _SupportsIndex) -> _T: ...
def __getitem__(self, i: SupportsIndex) -> _T: ...
@overload
def __getitem__(self, s: slice) -> List[_T]: ...
@overload
def __setitem__(self, i: _SupportsIndex, o: _T) -> None: ...
def __setitem__(self, i: SupportsIndex, o: _T) -> None: ...
@overload
def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ...
def __delitem__(self, i: Union[_SupportsIndex, slice]) -> None: ...
def __delitem__(self, i: Union[SupportsIndex, slice]) -> None: ...
def __add__(self, x: List[_T]) -> List[_T]: ...
def __iadd__(self: _S, x: Iterable[_T]) -> _S: ...
def __mul__(self, n: int) -> List[_T]: ...
Expand Down Expand Up @@ -910,16 +907,16 @@ class range(Sequence[int]):
stop: int
step: int
@overload
def __init__(self, stop: _SupportsIndex) -> None: ...
def __init__(self, stop: SupportsIndex) -> None: ...
@overload
def __init__(self, start: _SupportsIndex, stop: _SupportsIndex, step: _SupportsIndex = ...) -> None: ...
def __init__(self, start: SupportsIndex, stop: SupportsIndex, step: SupportsIndex = ...) -> None: ...
def count(self, value: int) -> int: ...
def index(self, value: int) -> int: ... # type: ignore
def __len__(self) -> int: ...
def __contains__(self, o: object) -> bool: ...
def __iter__(self) -> Iterator[int]: ...
@overload
def __getitem__(self, i: _SupportsIndex) -> int: ...
def __getitem__(self, i: SupportsIndex) -> int: ...
@overload
def __getitem__(self, s: slice) -> range: ...
def __repr__(self) -> str: ...
Expand Down Expand Up @@ -954,7 +951,7 @@ def abs(__x: SupportsAbs[_T]) -> _T: ...
def all(__iterable: Iterable[object]) -> bool: ...
def any(__iterable: Iterable[object]) -> bool: ...
def ascii(__obj: object) -> str: ...
def bin(__number: Union[int, _SupportsIndex]) -> str: ...
def bin(__number: Union[int, SupportsIndex]) -> str: ...

if sys.version_info >= (3, 7):
def breakpoint(*args: Any, **kws: Any) -> None: ...
Expand Down Expand Up @@ -1024,7 +1021,7 @@ def globals() -> Dict[str, Any]: ...
def hasattr(__obj: Any, __name: str) -> bool: ...
def hash(__obj: object) -> int: ...
def help(*args: Any, **kwds: Any) -> None: ...
def hex(__number: Union[int, _SupportsIndex]) -> str: ...
def hex(__number: Union[int, SupportsIndex]) -> str: ...
def id(__obj: object) -> int: ...
def input(__prompt: Any = ...) -> str: ...
@overload
Expand Down Expand Up @@ -1114,7 +1111,7 @@ def min(__iterable: Iterable[_T1], *, key: Callable[[_T1], SupportsLessThanT], d
def next(__i: Iterator[_T]) -> _T: ...
@overload
def next(__i: Iterator[_T], default: _VT) -> Union[_T, _VT]: ...
def oct(__number: Union[int, _SupportsIndex]) -> str: ...
def oct(__number: Union[int, SupportsIndex]) -> str: ...

_OpenFile = Union[AnyPath, int]
_Opener = Callable[[str, int], int]
Expand Down