Skip to content

Commit 8c434e0

Browse files
gbleaneysrittauAlexWaygood
authored
Add PEP 675 LiteralString overloads to str class (#7725)
Co-authored-by: Sebastian Rittau <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent e8f20bb commit 8c434e0

File tree

1 file changed

+123
-31
lines changed

1 file changed

+123
-31
lines changed

stdlib/builtins.pyi

+123-31
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ from typing import ( # noqa: Y027
5353
TypeVar,
5454
overload,
5555
)
56-
from typing_extensions import Literal, SupportsIndex, TypeAlias, TypeGuard, final
56+
from typing_extensions import Literal, LiteralString, SupportsIndex, TypeAlias, TypeGuard, final
5757

5858
if sys.version_info >= (3, 9):
5959
from types import GenericAlias
@@ -399,21 +399,39 @@ class str(Sequence[str]):
399399
def __new__(cls: type[Self], object: object = ...) -> Self: ...
400400
@overload
401401
def __new__(cls: type[Self], object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
402-
def capitalize(self) -> str: ...
403-
def casefold(self) -> str: ...
404-
def center(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
402+
@overload
403+
def capitalize(self: LiteralString) -> LiteralString: ...
404+
@overload
405+
def capitalize(self) -> str: ... # type: ignore[misc]
406+
@overload
407+
def casefold(self: LiteralString) -> LiteralString: ...
408+
@overload
409+
def casefold(self) -> str: ... # type: ignore[misc]
410+
@overload
411+
def center(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
412+
@overload
413+
def center(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ... # type: ignore[misc]
405414
def count(self, x: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
406415
def encode(self, encoding: str = ..., errors: str = ...) -> bytes: ...
407416
def endswith(
408417
self, __suffix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
409418
) -> bool: ...
410419
if sys.version_info >= (3, 8):
411-
def expandtabs(self, tabsize: SupportsIndex = ...) -> str: ...
420+
@overload
421+
def expandtabs(self: LiteralString, tabsize: SupportsIndex = ...) -> LiteralString: ...
422+
@overload
423+
def expandtabs(self, tabsize: SupportsIndex = ...) -> str: ... # type: ignore[misc]
412424
else:
413-
def expandtabs(self, tabsize: int = ...) -> str: ...
425+
@overload
426+
def expandtabs(self: LiteralString, tabsize: int = ...) -> LiteralString: ...
427+
@overload
428+
def expandtabs(self, tabsize: int = ...) -> str: ... # type: ignore[misc]
414429

415430
def find(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
416-
def format(self, *args: object, **kwargs: object) -> str: ...
431+
@overload
432+
def format(self: LiteralString, *args: LiteralString, **kwargs: LiteralString) -> LiteralString: ...
433+
@overload
434+
def format(self, *args: object, **kwargs: object) -> str: ... # type: ignore[misc]
417435
def format_map(self, map: _FormatMapMapping) -> str: ...
418436
def index(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
419437
def isalnum(self) -> bool: ...
@@ -430,55 +448,129 @@ class str(Sequence[str]):
430448
def isspace(self) -> bool: ...
431449
def istitle(self) -> bool: ...
432450
def isupper(self) -> bool: ...
433-
def join(self, __iterable: Iterable[str]) -> str: ...
434-
def ljust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
435-
def lower(self) -> str: ...
436-
def lstrip(self, __chars: str | None = ...) -> str: ...
437-
def partition(self, __sep: str) -> tuple[str, str, str]: ...
438-
def replace(self, __old: str, __new: str, __count: SupportsIndex = ...) -> str: ...
451+
@overload
452+
def join(self: LiteralString, __iterable: Iterable[LiteralString]) -> LiteralString: ...
453+
@overload
454+
def join(self, __iterable: Iterable[str]) -> str: ... # type: ignore[misc]
455+
@overload
456+
def ljust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
457+
@overload
458+
def ljust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ... # type: ignore[misc]
459+
@overload
460+
def lower(self: LiteralString) -> LiteralString: ...
461+
@overload
462+
def lower(self) -> str: ... # type: ignore[misc]
463+
@overload
464+
def lstrip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
465+
@overload
466+
def lstrip(self, __chars: str | None = ...) -> str: ... # type: ignore[misc]
467+
@overload
468+
def partition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
469+
@overload
470+
def partition(self, __sep: str) -> tuple[str, str, str]: ... # type: ignore[misc]
471+
@overload
472+
def replace(
473+
self: LiteralString, __old: LiteralString, __new: LiteralString, __count: SupportsIndex = ...
474+
) -> LiteralString: ...
475+
@overload
476+
def replace(self, __old: str, __new: str, __count: SupportsIndex = ...) -> str: ... # type: ignore[misc]
439477
if sys.version_info >= (3, 9):
440-
def removeprefix(self, __prefix: str) -> str: ...
441-
def removesuffix(self, __suffix: str) -> str: ...
478+
@overload
479+
def removeprefix(self: LiteralString, __prefix: LiteralString) -> LiteralString: ...
480+
@overload
481+
def removeprefix(self, __prefix: str) -> str: ... # type: ignore[misc]
482+
@overload
483+
def removesuffix(self: LiteralString, __suffix: LiteralString) -> LiteralString: ...
484+
@overload
485+
def removesuffix(self, __suffix: str) -> str: ... # type: ignore[misc]
442486

443487
def rfind(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
444488
def rindex(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
445-
def rjust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
446-
def rpartition(self, __sep: str) -> tuple[str, str, str]: ...
447-
def rsplit(self, sep: str | None = ..., maxsplit: SupportsIndex = ...) -> list[str]: ...
448-
def rstrip(self, __chars: str | None = ...) -> str: ...
449-
def split(self, sep: str | None = ..., maxsplit: SupportsIndex = ...) -> list[str]: ...
450-
def splitlines(self, keepends: bool = ...) -> list[str]: ...
489+
@overload
490+
def rjust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
491+
@overload
492+
def rjust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ... # type: ignore[misc]
493+
@overload
494+
def rpartition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
495+
@overload
496+
def rpartition(self, __sep: str) -> tuple[str, str, str]: ... # type: ignore[misc]
497+
@overload
498+
def rsplit(self: LiteralString, sep: LiteralString | None = ..., maxsplit: SupportsIndex = ...) -> list[LiteralString]: ...
499+
@overload
500+
def rsplit(self, sep: str | None = ..., maxsplit: SupportsIndex = ...) -> list[str]: ... # type: ignore[misc]
501+
@overload
502+
def rstrip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
503+
@overload
504+
def rstrip(self, __chars: str | None = ...) -> str: ... # type: ignore[misc]
505+
@overload
506+
def split(self: LiteralString, sep: LiteralString | None = ..., maxsplit: SupportsIndex = ...) -> list[LiteralString]: ...
507+
@overload
508+
def split(self, sep: str | None = ..., maxsplit: SupportsIndex = ...) -> list[str]: ... # type: ignore[misc]
509+
@overload
510+
def splitlines(self: LiteralString, keepends: bool = ...) -> list[LiteralString]: ...
511+
@overload
512+
def splitlines(self, keepends: bool = ...) -> list[str]: ... # type: ignore[misc]
451513
def startswith(
452514
self, __prefix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
453515
) -> bool: ...
454-
def strip(self, __chars: str | None = ...) -> str: ...
455-
def swapcase(self) -> str: ...
456-
def title(self) -> str: ...
516+
@overload
517+
def strip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
518+
@overload
519+
def strip(self, __chars: str | None = ...) -> str: ... # type: ignore[misc]
520+
@overload
521+
def swapcase(self: LiteralString) -> LiteralString: ...
522+
@overload
523+
def swapcase(self) -> str: ... # type: ignore[misc]
524+
@overload
525+
def title(self: LiteralString) -> LiteralString: ...
526+
@overload
527+
def title(self) -> str: ... # type: ignore[misc]
457528
def translate(self, __table: Mapping[int, int | str | None] | Sequence[int | str | None]) -> str: ...
458-
def upper(self) -> str: ...
459-
def zfill(self, __width: SupportsIndex) -> str: ...
529+
@overload
530+
def upper(self: LiteralString) -> LiteralString: ...
531+
@overload
532+
def upper(self) -> str: ... # type: ignore[misc]
533+
@overload
534+
def zfill(self: LiteralString, __width: SupportsIndex) -> LiteralString: ...
535+
@overload
536+
def zfill(self, __width: SupportsIndex) -> str: ... # type: ignore[misc]
460537
@staticmethod
461538
@overload
462539
def maketrans(__x: dict[int, _T] | dict[str, _T] | dict[str | int, _T]) -> dict[int, _T]: ...
463540
@staticmethod
464541
@overload
465542
def maketrans(__x: str, __y: str, __z: str | None = ...) -> dict[int, int | None]: ...
466-
def __add__(self, __s: str) -> str: ...
543+
@overload
544+
def __add__(self: LiteralString, __s: LiteralString) -> LiteralString: ...
545+
@overload
546+
def __add__(self, __s: str) -> str: ... # type: ignore[misc]
467547
# Incompatible with Sequence.__contains__
468548
def __contains__(self, __o: str) -> bool: ... # type: ignore[override]
469549
def __eq__(self, __x: object) -> bool: ...
470550
def __ge__(self, __x: str) -> bool: ...
471551
def __getitem__(self, __i: SupportsIndex | slice) -> str: ...
472552
def __gt__(self, __x: str) -> bool: ...
473553
def __hash__(self) -> int: ...
474-
def __iter__(self) -> Iterator[str]: ...
554+
@overload
555+
def __iter__(self: LiteralString) -> Iterator[LiteralString]: ...
556+
@overload
557+
def __iter__(self) -> Iterator[str]: ... # type: ignore[misc]
475558
def __le__(self, __x: str) -> bool: ...
476559
def __len__(self) -> int: ...
477560
def __lt__(self, __x: str) -> bool: ...
478-
def __mod__(self, __x: Any) -> str: ...
479-
def __mul__(self, __n: SupportsIndex) -> str: ...
561+
@overload
562+
def __mod__(self: LiteralString, __x: LiteralString | tuple[LiteralString, ...]) -> LiteralString: ...
563+
@overload
564+
def __mod__(self, __x: Any) -> str: ... # type: ignore[misc]
565+
@overload
566+
def __mul__(self: LiteralString, __n: SupportsIndex) -> LiteralString: ...
567+
@overload
568+
def __mul__(self, __n: SupportsIndex) -> str: ... # type: ignore[misc]
480569
def __ne__(self, __x: object) -> bool: ...
481-
def __rmul__(self, __n: SupportsIndex) -> str: ...
570+
@overload
571+
def __rmul__(self: LiteralString, __n: SupportsIndex) -> LiteralString: ...
572+
@overload
573+
def __rmul__(self, __n: SupportsIndex) -> str: ... # type: ignore[misc]
482574
def __getnewargs__(self) -> tuple[str]: ...
483575

484576
class bytes(ByteString):

0 commit comments

Comments
 (0)