Skip to content

Commit 50f5858

Browse files
authored
Use SupportsIndex where applicable for 5 classes (#5694)
Use `SupportsIndex` where applicable: * `str` * `bytes` * `bytearray` * `memoryview` * `markupsafe.Markup`
1 parent c522e8e commit 50f5858

File tree

2 files changed

+83
-70
lines changed

2 files changed

+83
-70
lines changed

stdlib/builtins.pyi

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,16 @@ class str(Sequence[str]):
334334
def __new__(cls: Type[_T], o: bytes, encoding: str = ..., errors: str = ...) -> _T: ...
335335
def capitalize(self) -> str: ...
336336
def casefold(self) -> str: ...
337-
def center(self, __width: int, __fillchar: str = ...) -> str: ...
337+
def center(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
338338
def count(self, x: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
339339
def encode(self, encoding: str = ..., errors: str = ...) -> bytes: ...
340340
def endswith(
341341
self, __suffix: Union[str, Tuple[str, ...]], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
342342
) -> bool: ...
343-
def expandtabs(self, tabsize: int = ...) -> str: ...
343+
if sys.version_info >= (3, 8):
344+
def expandtabs(self, tabsize: SupportsIndex = ...) -> str: ...
345+
else:
346+
def expandtabs(self, tabsize: int = ...) -> str: ...
344347
def find(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
345348
def format(self, *args: object, **kwargs: object) -> str: ...
346349
def format_map(self, map: _FormatMapMapping) -> str: ...
@@ -359,21 +362,21 @@ class str(Sequence[str]):
359362
def istitle(self) -> bool: ...
360363
def isupper(self) -> bool: ...
361364
def join(self, __iterable: Iterable[str]) -> str: ...
362-
def ljust(self, __width: int, __fillchar: str = ...) -> str: ...
365+
def ljust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
363366
def lower(self) -> str: ...
364367
def lstrip(self, __chars: Optional[str] = ...) -> str: ...
365368
def partition(self, __sep: str) -> Tuple[str, str, str]: ...
366-
def replace(self, __old: str, __new: str, __count: int = ...) -> str: ...
369+
def replace(self, __old: str, __new: str, __count: SupportsIndex = ...) -> str: ...
367370
if sys.version_info >= (3, 9):
368371
def removeprefix(self, __prefix: str) -> str: ...
369372
def removesuffix(self, __suffix: str) -> str: ...
370373
def rfind(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
371374
def rindex(self, __sub: str, __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...) -> int: ...
372-
def rjust(self, __width: int, __fillchar: str = ...) -> str: ...
375+
def rjust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
373376
def rpartition(self, __sep: str) -> Tuple[str, str, str]: ...
374-
def rsplit(self, sep: Optional[str] = ..., maxsplit: int = ...) -> List[str]: ...
377+
def rsplit(self, sep: Optional[str] = ..., maxsplit: SupportsIndex = ...) -> List[str]: ...
375378
def rstrip(self, __chars: Optional[str] = ...) -> str: ...
376-
def split(self, sep: Optional[str] = ..., maxsplit: int = ...) -> List[str]: ...
379+
def split(self, sep: Optional[str] = ..., maxsplit: SupportsIndex = ...) -> List[str]: ...
377380
def splitlines(self, keepends: bool = ...) -> List[str]: ...
378381
def startswith(
379382
self, __prefix: Union[str, Tuple[str, ...]], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
@@ -383,7 +386,7 @@ class str(Sequence[str]):
383386
def title(self) -> str: ...
384387
def translate(self, __table: Union[Mapping[int, Union[int, str, None]], Sequence[Union[int, str, None]]]) -> str: ...
385388
def upper(self) -> str: ...
386-
def zfill(self, __width: int) -> str: ...
389+
def zfill(self, __width: SupportsIndex) -> str: ...
387390
@staticmethod
388391
@overload
389392
def maketrans(__x: Union[Dict[int, _T], Dict[str, _T], Dict[Union[str, int], _T]]) -> Dict[int, _T]: ...
@@ -403,28 +406,28 @@ class str(Sequence[str]):
403406
def __len__(self) -> int: ...
404407
def __lt__(self, x: str) -> bool: ...
405408
def __mod__(self, x: Any) -> str: ...
406-
def __mul__(self, n: int) -> str: ...
409+
def __mul__(self, n: SupportsIndex) -> str: ...
407410
def __ne__(self, x: object) -> bool: ...
408411
def __repr__(self) -> str: ...
409-
def __rmul__(self, n: int) -> str: ...
412+
def __rmul__(self, n: SupportsIndex) -> str: ...
410413
def __str__(self) -> str: ...
411414
def __getnewargs__(self) -> Tuple[str]: ...
412415

413416
class bytes(ByteString):
414417
@overload
415-
def __new__(cls: Type[_T], ints: Iterable[int]) -> _T: ...
418+
def __new__(cls: Type[_T], ints: Iterable[SupportsIndex]) -> _T: ...
416419
@overload
417420
def __new__(cls: Type[_T], string: str, encoding: str, errors: str = ...) -> _T: ...
418421
@overload
419-
def __new__(cls: Type[_T], length: int) -> _T: ...
422+
def __new__(cls: Type[_T], length: SupportsIndex) -> _T: ...
420423
@overload
421424
def __new__(cls: Type[_T]) -> _T: ...
422425
@overload
423426
def __new__(cls: Type[_T], o: SupportsBytes) -> _T: ...
424427
def capitalize(self) -> bytes: ...
425-
def center(self, __width: int, __fillchar: bytes = ...) -> bytes: ...
428+
def center(self, __width: SupportsIndex, __fillchar: bytes = ...) -> bytes: ...
426429
def count(
427-
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
430+
self, __sub: bytes | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
428431
) -> int: ...
429432
def decode(self, encoding: str = ..., errors: str = ...) -> str: ...
430433
def endswith(
@@ -433,16 +436,19 @@ class bytes(ByteString):
433436
__start: Optional[SupportsIndex] = ...,
434437
__end: Optional[SupportsIndex] = ...,
435438
) -> bool: ...
436-
def expandtabs(self, tabsize: int = ...) -> bytes: ...
439+
if sys.version_info >= (3, 8):
440+
def expandtabs(self, tabsize: SupportsIndex = ...) -> bytes: ...
441+
else:
442+
def expandtabs(self, tabsize: int = ...) -> bytes: ...
437443
def find(
438-
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
444+
self, __sub: bytes | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
439445
) -> int: ...
440446
if sys.version_info >= (3, 8):
441-
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: int = ...) -> str: ...
447+
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: SupportsIndex = ...) -> str: ...
442448
else:
443449
def hex(self) -> str: ...
444450
def index(
445-
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
451+
self, __sub: bytes | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
446452
) -> int: ...
447453
def isalnum(self) -> bool: ...
448454
def isalpha(self) -> bool: ...
@@ -454,25 +460,25 @@ class bytes(ByteString):
454460
def istitle(self) -> bool: ...
455461
def isupper(self) -> bool: ...
456462
def join(self, __iterable_of_bytes: Iterable[Union[ByteString, memoryview]]) -> bytes: ...
457-
def ljust(self, __width: int, __fillchar: bytes = ...) -> bytes: ...
463+
def ljust(self, __width: SupportsIndex, __fillchar: bytes = ...) -> bytes: ...
458464
def lower(self) -> bytes: ...
459465
def lstrip(self, __bytes: Optional[bytes] = ...) -> bytes: ...
460466
def partition(self, __sep: bytes) -> Tuple[bytes, bytes, bytes]: ...
461-
def replace(self, __old: bytes, __new: bytes, __count: int = ...) -> bytes: ...
467+
def replace(self, __old: bytes, __new: bytes, __count: SupportsIndex = ...) -> bytes: ...
462468
if sys.version_info >= (3, 9):
463469
def removeprefix(self, __prefix: bytes) -> bytes: ...
464470
def removesuffix(self, __suffix: bytes) -> bytes: ...
465471
def rfind(
466-
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
472+
self, __sub: bytes | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
467473
) -> int: ...
468474
def rindex(
469-
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
475+
self, __sub: bytes | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
470476
) -> int: ...
471-
def rjust(self, __width: int, __fillchar: bytes = ...) -> bytes: ...
477+
def rjust(self, __width: SupportsIndex, __fillchar: bytes = ...) -> bytes: ...
472478
def rpartition(self, __sep: bytes) -> Tuple[bytes, bytes, bytes]: ...
473-
def rsplit(self, sep: Optional[bytes] = ..., maxsplit: int = ...) -> List[bytes]: ...
479+
def rsplit(self, sep: Optional[bytes] = ..., maxsplit: SupportsIndex = ...) -> List[bytes]: ...
474480
def rstrip(self, __bytes: Optional[bytes] = ...) -> bytes: ...
475-
def split(self, sep: Optional[bytes] = ..., maxsplit: int = ...) -> List[bytes]: ...
481+
def split(self, sep: Optional[bytes] = ..., maxsplit: SupportsIndex = ...) -> List[bytes]: ...
476482
def splitlines(self, keepends: bool = ...) -> List[bytes]: ...
477483
def startswith(
478484
self,
@@ -485,7 +491,7 @@ class bytes(ByteString):
485491
def title(self) -> bytes: ...
486492
def translate(self, __table: Optional[bytes], delete: bytes = ...) -> bytes: ...
487493
def upper(self) -> bytes: ...
488-
def zfill(self, __width: int) -> bytes: ...
494+
def zfill(self, __width: SupportsIndex) -> bytes: ...
489495
@classmethod
490496
def fromhex(cls, __s: str) -> bytes: ...
491497
@staticmethod
@@ -496,15 +502,15 @@ class bytes(ByteString):
496502
def __repr__(self) -> str: ...
497503
def __hash__(self) -> int: ...
498504
@overload
499-
def __getitem__(self, i: int) -> int: ...
505+
def __getitem__(self, i: SupportsIndex) -> int: ...
500506
@overload
501507
def __getitem__(self, s: slice) -> bytes: ...
502508
def __add__(self, s: bytes) -> bytes: ...
503-
def __mul__(self, n: int) -> bytes: ...
504-
def __rmul__(self, n: int) -> bytes: ...
509+
def __mul__(self, n: SupportsIndex) -> bytes: ...
510+
def __rmul__(self, n: SupportsIndex) -> bytes: ...
505511
def __mod__(self, value: Any) -> bytes: ...
506512
# Incompatible with Sequence.__contains__
507-
def __contains__(self, o: Union[int, bytes]) -> bool: ... # type: ignore
513+
def __contains__(self, o: SupportsIndex | bytes) -> bool: ... # type: ignore
508514
def __eq__(self, x: object) -> bool: ...
509515
def __ne__(self, x: object) -> bool: ...
510516
def __lt__(self, x: bytes) -> bool: ...
@@ -517,16 +523,16 @@ class bytearray(MutableSequence[int], ByteString):
517523
@overload
518524
def __init__(self) -> None: ...
519525
@overload
520-
def __init__(self, ints: Iterable[int]) -> None: ...
526+
def __init__(self, ints: Iterable[SupportsIndex]) -> None: ...
521527
@overload
522528
def __init__(self, string: str, encoding: str, errors: str = ...) -> None: ...
523529
@overload
524-
def __init__(self, length: int) -> None: ...
525-
def append(self, __item: int) -> None: ...
530+
def __init__(self, length: SupportsIndex) -> None: ...
531+
def append(self, __item: SupportsIndex) -> None: ...
526532
def capitalize(self) -> bytearray: ...
527-
def center(self, __width: int, __fillchar: bytes = ...) -> bytearray: ...
533+
def center(self, __width: SupportsIndex, __fillchar: bytes = ...) -> bytearray: ...
528534
def count(
529-
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
535+
self, __sub: bytes | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
530536
) -> int: ...
531537
def copy(self) -> bytearray: ...
532538
def decode(self, encoding: str = ..., errors: str = ...) -> str: ...
@@ -536,19 +542,22 @@ class bytearray(MutableSequence[int], ByteString):
536542
__start: Optional[SupportsIndex] = ...,
537543
__end: Optional[SupportsIndex] = ...,
538544
) -> bool: ...
539-
def expandtabs(self, tabsize: int = ...) -> bytearray: ...
540-
def extend(self, __iterable_of_ints: Iterable[int]) -> None: ...
545+
if sys.version_info >= (3, 8):
546+
def expandtabs(self, tabsize: SupportsIndex = ...) -> bytearray: ...
547+
else:
548+
def expandtabs(self, tabsize: int = ...) -> bytearray: ...
549+
def extend(self, __iterable_of_ints: Iterable[SupportsIndex]) -> None: ...
541550
def find(
542-
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
551+
self, __sub: bytes | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
543552
) -> int: ...
544553
if sys.version_info >= (3, 8):
545-
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: int = ...) -> str: ...
554+
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: SupportsIndex = ...) -> str: ...
546555
else:
547556
def hex(self) -> str: ...
548557
def index(
549-
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
558+
self, __sub: bytes | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
550559
) -> int: ...
551-
def insert(self, __index: int, __item: int) -> None: ...
560+
def insert(self, __index: SupportsIndex, __item: SupportsIndex) -> None: ...
552561
def isalnum(self) -> bool: ...
553562
def isalpha(self) -> bool: ...
554563
if sys.version_info >= (3, 7):
@@ -559,25 +568,25 @@ class bytearray(MutableSequence[int], ByteString):
559568
def istitle(self) -> bool: ...
560569
def isupper(self) -> bool: ...
561570
def join(self, __iterable_of_bytes: Iterable[Union[ByteString, memoryview]]) -> bytearray: ...
562-
def ljust(self, __width: int, __fillchar: bytes = ...) -> bytearray: ...
571+
def ljust(self, __width: SupportsIndex, __fillchar: bytes = ...) -> bytearray: ...
563572
def lower(self) -> bytearray: ...
564573
def lstrip(self, __bytes: Optional[bytes] = ...) -> bytearray: ...
565574
def partition(self, __sep: bytes) -> Tuple[bytearray, bytearray, bytearray]: ...
566575
if sys.version_info >= (3, 9):
567576
def removeprefix(self, __prefix: bytes) -> bytearray: ...
568577
def removesuffix(self, __suffix: bytes) -> bytearray: ...
569-
def replace(self, __old: bytes, __new: bytes, __count: int = ...) -> bytearray: ...
578+
def replace(self, __old: bytes, __new: bytes, __count: SupportsIndex = ...) -> bytearray: ...
570579
def rfind(
571-
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
580+
self, __sub: bytes | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
572581
) -> int: ...
573582
def rindex(
574-
self, __sub: Union[bytes, int], __start: Optional[SupportsIndex] = ..., __end: Optional[SupportsIndex] = ...
583+
self, __sub: bytes | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
575584
) -> int: ...
576-
def rjust(self, __width: int, __fillchar: bytes = ...) -> bytearray: ...
585+
def rjust(self, __width: SupportsIndex, __fillchar: bytes = ...) -> bytearray: ...
577586
def rpartition(self, __sep: bytes) -> Tuple[bytearray, bytearray, bytearray]: ...
578-
def rsplit(self, sep: Optional[bytes] = ..., maxsplit: int = ...) -> List[bytearray]: ...
587+
def rsplit(self, sep: Optional[bytes] = ..., maxsplit: SupportsIndex = ...) -> List[bytearray]: ...
579588
def rstrip(self, __bytes: Optional[bytes] = ...) -> bytearray: ...
580-
def split(self, sep: Optional[bytes] = ..., maxsplit: int = ...) -> List[bytearray]: ...
589+
def split(self, sep: Optional[bytes] = ..., maxsplit: SupportsIndex = ...) -> List[bytearray]: ...
581590
def splitlines(self, keepends: bool = ...) -> List[bytearray]: ...
582591
def startswith(
583592
self,
@@ -590,7 +599,7 @@ class bytearray(MutableSequence[int], ByteString):
590599
def title(self) -> bytearray: ...
591600
def translate(self, __table: Optional[bytes], delete: bytes = ...) -> bytearray: ...
592601
def upper(self) -> bytearray: ...
593-
def zfill(self, __width: int) -> bytearray: ...
602+
def zfill(self, __width: SupportsIndex) -> bytearray: ...
594603
@classmethod
595604
def fromhex(cls, __string: str) -> bytearray: ...
596605
@staticmethod
@@ -601,22 +610,22 @@ class bytearray(MutableSequence[int], ByteString):
601610
def __repr__(self) -> str: ...
602611
__hash__: None # type: ignore
603612
@overload
604-
def __getitem__(self, i: int) -> int: ...
613+
def __getitem__(self, i: SupportsIndex) -> int: ...
605614
@overload
606615
def __getitem__(self, s: slice) -> bytearray: ...
607616
@overload
608-
def __setitem__(self, i: int, x: int) -> None: ...
617+
def __setitem__(self, i: SupportsIndex, x: SupportsIndex) -> None: ...
609618
@overload
610-
def __setitem__(self, s: slice, x: Union[Iterable[int], bytes]) -> None: ...
611-
def __delitem__(self, i: Union[int, slice]) -> None: ...
619+
def __setitem__(self, s: slice, x: Iterable[SupportsIndex] | bytes) -> None: ...
620+
def __delitem__(self, i: SupportsIndex | slice) -> None: ...
612621
def __add__(self, s: bytes) -> bytearray: ...
613622
def __iadd__(self, s: Iterable[int]) -> bytearray: ...
614-
def __mul__(self, n: int) -> bytearray: ...
615-
def __rmul__(self, n: int) -> bytearray: ...
616-
def __imul__(self, n: int) -> bytearray: ...
623+
def __mul__(self, n: SupportsIndex) -> bytearray: ...
624+
def __rmul__(self, n: SupportsIndex) -> bytearray: ...
625+
def __imul__(self, n: SupportsIndex) -> bytearray: ...
617626
def __mod__(self, value: Any) -> bytes: ...
618627
# Incompatible with Sequence.__contains__
619-
def __contains__(self, o: Union[int, bytes]) -> bool: ... # type: ignore
628+
def __contains__(self, o: SupportsIndex | bytes) -> bool: ... # type: ignore
620629
def __eq__(self, x: object) -> bool: ...
621630
def __ne__(self, x: object) -> bool: ...
622631
def __lt__(self, x: bytes) -> bool: ...
@@ -645,7 +654,7 @@ class memoryview(Sized, Sequence[int]):
645654
) -> None: ...
646655
def cast(self, format: str, shape: Union[List[int], Tuple[int]] = ...) -> memoryview: ...
647656
@overload
648-
def __getitem__(self, i: int) -> int: ...
657+
def __getitem__(self, i: SupportsIndex) -> int: ...
649658
@overload
650659
def __getitem__(self, s: slice) -> memoryview: ...
651660
def __contains__(self, x: object) -> bool: ...
@@ -654,7 +663,7 @@ class memoryview(Sized, Sequence[int]):
654663
@overload
655664
def __setitem__(self, s: slice, o: bytes) -> None: ...
656665
@overload
657-
def __setitem__(self, i: int, o: int) -> None: ...
666+
def __setitem__(self, i: SupportsIndex, o: SupportsIndex) -> None: ...
658667
if sys.version_info >= (3, 8):
659668
def tobytes(self, order: Optional[Literal["C", "F", "A"]] = ...) -> bytes: ...
660669
else:
@@ -664,7 +673,7 @@ class memoryview(Sized, Sequence[int]):
664673
def toreadonly(self) -> memoryview: ...
665674
def release(self) -> None: ...
666675
if sys.version_info >= (3, 8):
667-
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: int = ...) -> str: ...
676+
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: SupportsIndex = ...) -> str: ...
668677
else:
669678
def hex(self) -> str: ...
670679

0 commit comments

Comments
 (0)