Skip to content

Commit 56aab63

Browse files
committed
Add LiteralString overloads to str class
1 parent 63fb9af commit 56aab63

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

stdlib/builtins.pyi

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ from typing import ( # noqa: Y027
4949
TypeVar,
5050
overload,
5151
)
52-
from typing_extensions import Literal, SupportsIndex, TypeAlias, TypeGuard, final
52+
from typing_extensions import Literal, LiteralString, SupportsIndex, TypeAlias, TypeGuard, final
5353

5454
if sys.version_info >= (3, 9):
5555
from types import GenericAlias
@@ -394,20 +394,38 @@ class str(Sequence[str]):
394394
def __new__(cls: type[Self], object: object = ...) -> Self: ...
395395
@overload
396396
def __new__(cls: type[Self], object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
397+
@overload
398+
def capitalize(self: LiteralString) -> LiteralString: ...
399+
@overload
397400
def capitalize(self) -> str: ...
401+
@overload
402+
def casefold(self: LiteralString) -> LiteralString: ...
403+
@overload
398404
def casefold(self) -> str: ...
405+
@overload
406+
def center(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
407+
@overload
399408
def center(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
400409
def count(self, x: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
401410
def encode(self, encoding: str = ..., errors: str = ...) -> bytes: ...
402411
def endswith(
403412
self, __suffix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
404413
) -> bool: ...
405414
if sys.version_info >= (3, 8):
415+
@overload
416+
def expandtabs(self: LiteralString, tabsize: SupportsIndex = ...) -> LiteralString: ...
417+
@overload
406418
def expandtabs(self, tabsize: SupportsIndex = ...) -> str: ...
407419
else:
420+
@overload
421+
def expandtabs(self: LiteralString, tabsize: int = ...) -> LiteralString: ...
422+
@overload
408423
def expandtabs(self, tabsize: int = ...) -> str: ...
409424

410425
def find(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
426+
@overload
427+
def format(self: LiteralString, *args: LiteralString, **kwargs: LiteralString) -> LiteralString: ...
428+
@overload
411429
def format(self, *args: object, **kwargs: object) -> str: ...
412430
def format_map(self, map: _FormatMapMapping) -> str: ...
413431
def index(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
@@ -425,39 +443,99 @@ class str(Sequence[str]):
425443
def isspace(self) -> bool: ...
426444
def istitle(self) -> bool: ...
427445
def isupper(self) -> bool: ...
446+
@overload
447+
def join(self: LiteralString, __iterable: Iterable[LiteralString]) -> LiteralString: ...
448+
@overload
428449
def join(self, __iterable: Iterable[str]) -> str: ...
450+
@overload
451+
def ljust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
452+
@overload
429453
def ljust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
454+
@overload
455+
def lower(self: LiteralString) -> LiteralString: ...
456+
@overload
430457
def lower(self) -> str: ...
458+
@overload
459+
def lstrip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
460+
@overload
431461
def lstrip(self, __chars: str | None = ...) -> str: ...
462+
@overload
463+
def partition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
464+
@overload
432465
def partition(self, __sep: str) -> tuple[str, str, str]: ...
466+
@overload
467+
def replace(self: LiteralString, __old: LiteralString, __new: LiteralString, __count: SupportsIndex = ...) -> LiteralString: ...
468+
@overload
433469
def replace(self, __old: str, __new: str, __count: SupportsIndex = ...) -> str: ...
434470
if sys.version_info >= (3, 9):
471+
@overload
472+
def removeprefix(self: LiteralString, __prefix: LiteralString) -> LiteralString: ...
473+
@overload
435474
def removeprefix(self, __prefix: str) -> str: ...
475+
@overload
476+
def removesuffix(self: LiteralString, __suffix: LiteralString) -> LiteralString: ...
477+
@overload
436478
def removesuffix(self, __suffix: str) -> str: ...
437479

438480
def rfind(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
439481
def rindex(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
482+
@overload
483+
def rjust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
484+
@overload
440485
def rjust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
486+
@overload
487+
def rpartition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
488+
@overload
441489
def rpartition(self, __sep: str) -> tuple[str, str, str]: ...
490+
@overload
491+
def rsplit(self: LiteralString, sep: LiteralString | None = ..., maxsplit: SupportsIndex = ...) -> list[LiteralString]: ...
492+
@overload
442493
def rsplit(self, sep: str | None = ..., maxsplit: SupportsIndex = ...) -> list[str]: ...
494+
@overload
495+
def rstrip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
496+
@overload
443497
def rstrip(self, __chars: str | None = ...) -> str: ...
498+
@overload
499+
def split(self: LiteralString, sep: LiteralString | None = ..., maxsplit: SupportsIndex = ...) -> list[LiteralString]: ...
500+
@overload
444501
def split(self, sep: str | None = ..., maxsplit: SupportsIndex = ...) -> list[str]: ...
502+
@overload
503+
def splitlines(self: LiteralString, keepends: bool = ...) -> list[LiteralString]: ...
504+
@overload
445505
def splitlines(self, keepends: bool = ...) -> list[str]: ...
446506
def startswith(
447507
self, __prefix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
448508
) -> bool: ...
509+
@overload
510+
def strip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
511+
@overload
449512
def strip(self, __chars: str | None = ...) -> str: ...
513+
@overload
514+
def swapcase(self: LiteralString) -> LiteralString: ...
515+
@overload
450516
def swapcase(self) -> str: ...
517+
@overload
518+
def title(self: LiteralString) -> LiteralString: ...
519+
@overload
451520
def title(self) -> str: ...
452521
def translate(self, __table: Mapping[int, int | str | None] | Sequence[int | str | None]) -> str: ...
522+
@overload
523+
def upper(self: LiteralString) -> LiteralString: ...
524+
@overload
453525
def upper(self) -> str: ...
526+
@overload
527+
def zfill(self: LiteralString, __width: SupportsIndex) -> LiteralString: ...
528+
@overload
454529
def zfill(self, __width: SupportsIndex) -> str: ...
455530
@staticmethod
456531
@overload
457532
def maketrans(__x: dict[int, _T] | dict[str, _T] | dict[str | int, _T]) -> dict[int, _T]: ...
458533
@staticmethod
459534
@overload
460535
def maketrans(__x: str, __y: str, __z: str | None = ...) -> dict[int, int | None]: ...
536+
@overload
537+
def __add__(self: LiteralString, __s: LiteralString) -> LiteralString: ...
538+
@overload
461539
def __add__(self, __s: str) -> str: ...
462540
# Incompatible with Sequence.__contains__
463541
def __contains__(self, __o: str) -> bool: ... # type: ignore[override]
@@ -466,11 +544,20 @@ class str(Sequence[str]):
466544
def __getitem__(self, __i: SupportsIndex | slice) -> str: ...
467545
def __gt__(self, __x: str) -> bool: ...
468546
def __hash__(self) -> int: ...
547+
@overload
548+
def __iter__(self: LiteralString) -> Iterator[LiteralString]: ...
549+
@overload
469550
def __iter__(self) -> Iterator[str]: ...
470551
def __le__(self, __x: str) -> bool: ...
471552
def __len__(self) -> int: ...
472553
def __lt__(self, __x: str) -> bool: ...
554+
@overload
555+
def __mod__(self: LiteralString, __x: LiteralString | tuple[LiteralString, ...]) -> LiteralString: ...
556+
@overload
473557
def __mod__(self, __x: Any) -> str: ...
558+
@overload
559+
def __mul__(self: LiteralString, __n: SupportsIndex) -> LiteralString: ...
560+
@overload
474561
def __mul__(self, __n: SupportsIndex) -> str: ...
475562
def __ne__(self, __x: object) -> bool: ...
476563
def __rmul__(self, __n: SupportsIndex) -> str: ...

0 commit comments

Comments
 (0)