Skip to content

Commit fc07756

Browse files
authored
Use native SupportsIndex in builtins (#5229)
1 parent fc660d6 commit fc07756

File tree

1 file changed

+40
-43
lines changed

1 file changed

+40
-43
lines changed

stdlib/builtins.pyi

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,11 @@ from typing import (
5959
overload,
6060
runtime_checkable,
6161
)
62-
from typing_extensions import Literal
62+
from typing_extensions import Literal, SupportsIndex
6363

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

67-
class _SupportsIndex(Protocol):
68-
def __index__(self) -> int: ...
69-
7067
class _SupportsTrunc(Protocol):
7168
def __trunc__(self) -> int: ...
7269

@@ -170,7 +167,7 @@ class super(object):
170167

171168
class int:
172169
@overload
173-
def __new__(cls: Type[_T], x: Union[str, bytes, SupportsInt, _SupportsIndex, _SupportsTrunc] = ...) -> _T: ...
170+
def __new__(cls: Type[_T], x: Union[str, bytes, SupportsInt, SupportsIndex, _SupportsTrunc] = ...) -> _T: ...
174171
@overload
175172
def __new__(cls: Type[_T], x: Union[str, bytes, bytearray], base: int) -> _T: ...
176173
if sys.version_info >= (3, 8):
@@ -242,7 +239,7 @@ class int:
242239
def __index__(self) -> int: ...
243240

244241
class float:
245-
def __new__(cls: Type[_T], x: Union[SupportsFloat, _SupportsIndex, str, bytes, bytearray] = ...) -> _T: ...
242+
def __new__(cls: Type[_T], x: Union[SupportsFloat, SupportsIndex, str, bytes, bytearray] = ...) -> _T: ...
246243
def as_integer_ratio(self) -> Tuple[int, int]: ...
247244
def hex(self) -> str: ...
248245
def is_integer(self) -> bool: ...
@@ -299,7 +296,7 @@ class complex:
299296
@overload
300297
def __new__(cls: Type[_T], real: float = ..., imag: float = ...) -> _T: ...
301298
@overload
302-
def __new__(cls: Type[_T], real: Union[str, SupportsComplex, _SupportsIndex]) -> _T: ...
299+
def __new__(cls: Type[_T], real: Union[str, SupportsComplex, SupportsIndex]) -> _T: ...
303300
@property
304301
def real(self) -> float: ...
305302
@property
@@ -335,19 +332,19 @@ class str(Sequence[str]):
335332
def capitalize(self) -> str: ...
336333
def casefold(self) -> str: ...
337334
def center(self, __width: int, __fillchar: str = ...) -> str: ...
338-
def count(self, x: str, __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...) -> int: ...
335+
def count(self, x: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
339336
def encode(self, encoding: str = ..., errors: str = ...) -> bytes: ...
340337
def endswith(
341338
self,
342339
__suffix: Union[str, Tuple[str, ...]],
343-
__start: Optional[_SupportsIndex] = ...,
344-
__end: Optional[_SupportsIndex] = ...,
340+
__start: Optional[SupportsIndex] = ...,
341+
__end: Optional[SupportsIndex] = ...,
345342
) -> bool: ...
346343
def expandtabs(self, tabsize: int = ...) -> str: ...
347-
def find(self, __sub: str, __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...) -> int: ...
344+
def find(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
348345
def format(self, *args: object, **kwargs: object) -> str: ...
349346
def format_map(self, map: _FormatMapMapping) -> str: ...
350-
def index(self, __sub: str, __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...) -> int: ...
347+
def index(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
351348
def isalnum(self) -> bool: ...
352349
def isalpha(self) -> bool: ...
353350
if sys.version_info >= (3, 7):
@@ -370,8 +367,8 @@ class str(Sequence[str]):
370367
if sys.version_info >= (3, 9):
371368
def removeprefix(self, __prefix: str) -> str: ...
372369
def removesuffix(self, __suffix: str) -> str: ...
373-
def rfind(self, __sub: str, __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...) -> int: ...
374-
def rindex(self, __sub: str, __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...) -> int: ...
370+
def rfind(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
371+
def rindex(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
375372
def rjust(self, __width: int, __fillchar: str = ...) -> str: ...
376373
def rpartition(self, __sep: str) -> Tuple[str, str, str]: ...
377374
def rsplit(self, sep: Optional[str] = ..., maxsplit: int = ...) -> List[str]: ...
@@ -381,8 +378,8 @@ class str(Sequence[str]):
381378
def startswith(
382379
self,
383380
__prefix: Union[str, Tuple[str, ...]],
384-
__start: Optional[_SupportsIndex] = ...,
385-
__end: Optional[_SupportsIndex] = ...,
381+
__start: Optional[SupportsIndex] = ...,
382+
__end: Optional[SupportsIndex] = ...,
386383
) -> bool: ...
387384
def strip(self, __chars: Optional[str] = ...) -> str: ...
388385
def swapcase(self) -> str: ...
@@ -430,25 +427,25 @@ class bytes(ByteString):
430427
def capitalize(self) -> bytes: ...
431428
def center(self, __width: int, __fillchar: bytes = ...) -> bytes: ...
432429
def count(
433-
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
430+
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
434431
) -> int: ...
435432
def decode(self, encoding: str = ..., errors: str = ...) -> str: ...
436433
def endswith(
437434
self,
438435
__suffix: Union[bytes, Tuple[bytes, ...]],
439-
__start: Optional[_SupportsIndex] = ...,
440-
__end: Optional[_SupportsIndex] = ...,
436+
__start: Optional[SupportsIndex] = ...,
437+
__end: Optional[SupportsIndex] = ...,
441438
) -> bool: ...
442439
def expandtabs(self, tabsize: int = ...) -> bytes: ...
443440
def find(
444-
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
441+
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
445442
) -> int: ...
446443
if sys.version_info >= (3, 8):
447444
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: int = ...) -> str: ...
448445
else:
449446
def hex(self) -> str: ...
450447
def index(
451-
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
448+
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
452449
) -> int: ...
453450
def isalnum(self) -> bool: ...
454451
def isalpha(self) -> bool: ...
@@ -469,10 +466,10 @@ class bytes(ByteString):
469466
def removeprefix(self, __prefix: bytes) -> bytes: ...
470467
def removesuffix(self, __suffix: bytes) -> bytes: ...
471468
def rfind(
472-
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
469+
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
473470
) -> int: ...
474471
def rindex(
475-
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
472+
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
476473
) -> int: ...
477474
def rjust(self, __width: int, __fillchar: bytes = ...) -> bytes: ...
478475
def rpartition(self, __sep: bytes) -> Tuple[bytes, bytes, bytes]: ...
@@ -483,8 +480,8 @@ class bytes(ByteString):
483480
def startswith(
484481
self,
485482
__prefix: Union[bytes, Tuple[bytes, ...]],
486-
__start: Optional[_SupportsIndex] = ...,
487-
__end: Optional[_SupportsIndex] = ...,
483+
__start: Optional[SupportsIndex] = ...,
484+
__end: Optional[SupportsIndex] = ...,
488485
) -> bool: ...
489486
def strip(self, __bytes: Optional[bytes] = ...) -> bytes: ...
490487
def swapcase(self) -> bytes: ...
@@ -532,27 +529,27 @@ class bytearray(MutableSequence[int], ByteString):
532529
def capitalize(self) -> bytearray: ...
533530
def center(self, __width: int, __fillchar: bytes = ...) -> bytearray: ...
534531
def count(
535-
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
532+
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
536533
) -> int: ...
537534
def copy(self) -> bytearray: ...
538535
def decode(self, encoding: str = ..., errors: str = ...) -> str: ...
539536
def endswith(
540537
self,
541538
__suffix: Union[bytes, Tuple[bytes, ...]],
542-
__start: Optional[_SupportsIndex] = ...,
543-
__end: Optional[_SupportsIndex] = ...,
539+
__start: Optional[SupportsIndex] = ...,
540+
__end: Optional[SupportsIndex] = ...,
544541
) -> bool: ...
545542
def expandtabs(self, tabsize: int = ...) -> bytearray: ...
546543
def extend(self, __iterable_of_ints: Iterable[int]) -> None: ...
547544
def find(
548-
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
545+
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
549546
) -> int: ...
550547
if sys.version_info >= (3, 8):
551548
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: int = ...) -> str: ...
552549
else:
553550
def hex(self) -> str: ...
554551
def index(
555-
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
552+
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
556553
) -> int: ...
557554
def insert(self, __index: int, __item: int) -> None: ...
558555
def isalnum(self) -> bool: ...
@@ -574,10 +571,10 @@ class bytearray(MutableSequence[int], ByteString):
574571
def removesuffix(self, __suffix: bytes) -> bytearray: ...
575572
def replace(self, __old: bytes, __new: bytes, __count: int = ...) -> bytearray: ...
576573
def rfind(
577-
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
574+
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
578575
) -> int: ...
579576
def rindex(
580-
self, __sub: Union[bytes, int], __start: Optional[_SupportsIndex] = ..., __end: Optional[_SupportsIndex] = ...
577+
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
581578
) -> int: ...
582579
def rjust(self, __width: int, __fillchar: bytes = ...) -> bytearray: ...
583580
def rpartition(self, __sep: bytes) -> Tuple[bytearray, bytearray, bytearray]: ...
@@ -588,8 +585,8 @@ class bytearray(MutableSequence[int], ByteString):
588585
def startswith(
589586
self,
590587
__prefix: Union[bytes, Tuple[bytes, ...]],
591-
__start: Optional[_SupportsIndex] = ...,
592-
__end: Optional[_SupportsIndex] = ...,
588+
__start: Optional[SupportsIndex] = ...,
589+
__end: Optional[SupportsIndex] = ...,
593590
) -> bool: ...
594591
def strip(self, __bytes: Optional[bytes] = ...) -> bytearray: ...
595592
def swapcase(self) -> bytearray: ...
@@ -769,14 +766,14 @@ class list(MutableSequence[_T], Generic[_T]):
769766
def __str__(self) -> str: ...
770767
__hash__: None # type: ignore
771768
@overload
772-
def __getitem__(self, i: _SupportsIndex) -> _T: ...
769+
def __getitem__(self, i: SupportsIndex) -> _T: ...
773770
@overload
774771
def __getitem__(self, s: slice) -> List[_T]: ...
775772
@overload
776-
def __setitem__(self, i: _SupportsIndex, o: _T) -> None: ...
773+
def __setitem__(self, i: SupportsIndex, o: _T) -> None: ...
777774
@overload
778775
def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ...
779-
def __delitem__(self, i: Union[_SupportsIndex, slice]) -> None: ...
776+
def __delitem__(self, i: Union[SupportsIndex, slice]) -> None: ...
780777
def __add__(self, x: List[_T]) -> List[_T]: ...
781778
def __iadd__(self: _S, x: Iterable[_T]) -> _S: ...
782779
def __mul__(self, n: int) -> List[_T]: ...
@@ -910,16 +907,16 @@ class range(Sequence[int]):
910907
stop: int
911908
step: int
912909
@overload
913-
def __init__(self, stop: _SupportsIndex) -> None: ...
910+
def __init__(self, stop: SupportsIndex) -> None: ...
914911
@overload
915-
def __init__(self, start: _SupportsIndex, stop: _SupportsIndex, step: _SupportsIndex = ...) -> None: ...
912+
def __init__(self, start: SupportsIndex, stop: SupportsIndex, step: SupportsIndex = ...) -> None: ...
916913
def count(self, value: int) -> int: ...
917914
def index(self, value: int) -> int: ... # type: ignore
918915
def __len__(self) -> int: ...
919916
def __contains__(self, o: object) -> bool: ...
920917
def __iter__(self) -> Iterator[int]: ...
921918
@overload
922-
def __getitem__(self, i: _SupportsIndex) -> int: ...
919+
def __getitem__(self, i: SupportsIndex) -> int: ...
923920
@overload
924921
def __getitem__(self, s: slice) -> range: ...
925922
def __repr__(self) -> str: ...
@@ -954,7 +951,7 @@ def abs(__x: SupportsAbs[_T]) -> _T: ...
954951
def all(__iterable: Iterable[object]) -> bool: ...
955952
def any(__iterable: Iterable[object]) -> bool: ...
956953
def ascii(__obj: object) -> str: ...
957-
def bin(__number: Union[int, _SupportsIndex]) -> str: ...
954+
def bin(__number: Union[int, SupportsIndex]) -> str: ...
958955

959956
if sys.version_info >= (3, 7):
960957
def breakpoint(*args: Any, **kws: Any) -> None: ...
@@ -1024,7 +1021,7 @@ def globals() -> Dict[str, Any]: ...
10241021
def hasattr(__obj: Any, __name: str) -> bool: ...
10251022
def hash(__obj: object) -> int: ...
10261023
def help(*args: Any, **kwds: Any) -> None: ...
1027-
def hex(__number: Union[int, _SupportsIndex]) -> str: ...
1024+
def hex(__number: Union[int, SupportsIndex]) -> str: ...
10281025
def id(__obj: object) -> int: ...
10291026
def input(__prompt: Any = ...) -> str: ...
10301027
@overload
@@ -1114,7 +1111,7 @@ def min(__iterable: Iterable[_T1], *, key: Callable[[_T1], SupportsLessThanT], d
11141111
def next(__i: Iterator[_T]) -> _T: ...
11151112
@overload
11161113
def next(__i: Iterator[_T], default: _VT) -> Union[_T, _VT]: ...
1117-
def oct(__number: Union[int, _SupportsIndex]) -> str: ...
1114+
def oct(__number: Union[int, SupportsIndex]) -> str: ...
11181115

11191116
_OpenFile = Union[AnyPath, int]
11201117
_Opener = Callable[[str, int], int]

0 commit comments

Comments
 (0)