@@ -56,6 +56,7 @@ from typing import ( # noqa: Y022
5656from typing_extensions import (
5757 Concatenate ,
5858 Literal ,
59+ LiteralString ,
5960 ParamSpec ,
6061 Self ,
6162 SupportsIndex ,
@@ -213,6 +214,8 @@ _NegativeInteger: TypeAlias = Literal[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -
213214_LiteralInteger = _PositiveInteger | _NegativeInteger | Literal [0 ] # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed
214215
215216class int :
217+ @overload
218+ def __new__ (cls ) -> Literal [0 ]: ...
216219 @overload
217220 def __new__ (cls , __x : str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self : ...
218221 @overload
@@ -431,24 +434,44 @@ class _TranslateTable(Protocol):
431434 def __getitem__ (self , __key : int ) -> str | int | None : ...
432435
433436class str (Sequence [str ]):
437+ @overload
438+ def __new__ (cls ) -> Literal ["" ]: ...
434439 @overload
435440 def __new__ (cls , object : object = ...) -> Self : ...
436441 @overload
437442 def __new__ (cls , object : ReadableBuffer , encoding : str = ..., errors : str = ...) -> Self : ...
443+ @overload
444+ def capitalize (self : LiteralString ) -> LiteralString : ...
445+ @overload
438446 def capitalize (self ) -> str : ... # type: ignore[misc]
447+ @overload
448+ def casefold (self : LiteralString ) -> LiteralString : ...
449+ @overload
439450 def casefold (self ) -> str : ... # type: ignore[misc]
451+ @overload
452+ def center (self : LiteralString , __width : SupportsIndex , __fillchar : LiteralString = " " ) -> LiteralString : ...
453+ @overload
440454 def center (self , __width : SupportsIndex , __fillchar : str = " " ) -> str : ... # type: ignore[misc]
441455 def count (self , x : str , __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...) -> int : ...
442456 def encode (self , encoding : str = "utf-8" , errors : str = "strict" ) -> bytes : ...
443457 def endswith (
444458 self , __suffix : str | tuple [str , ...], __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...
445459 ) -> bool : ...
446460 if sys .version_info >= (3 , 8 ):
461+ @overload
462+ def expandtabs (self : LiteralString , tabsize : SupportsIndex = 8 ) -> LiteralString : ...
463+ @overload
447464 def expandtabs (self , tabsize : SupportsIndex = 8 ) -> str : ... # type: ignore[misc]
448465 else :
466+ @overload
467+ def expandtabs (self : LiteralString , tabsize : int = 8 ) -> LiteralString : ...
468+ @overload
449469 def expandtabs (self , tabsize : int = 8 ) -> str : ... # type: ignore[misc]
450470
451471 def find (self , __sub : str , __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...) -> int : ...
472+ @overload
473+ def format (self : LiteralString , * args : LiteralString , ** kwargs : LiteralString ) -> LiteralString : ...
474+ @overload
452475 def format (self , * args : object , ** kwargs : object ) -> str : ...
453476 def format_map (self , map : _FormatMapMapping ) -> str : ...
454477 def index (self , __sub : str , __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...) -> int : ...
@@ -464,32 +487,91 @@ class str(Sequence[str]):
464487 def isspace (self ) -> bool : ...
465488 def istitle (self ) -> bool : ...
466489 def isupper (self ) -> bool : ...
490+ @overload
491+ def join (self : LiteralString , __iterable : Iterable [LiteralString ]) -> LiteralString : ...
492+ @overload
467493 def join (self , __iterable : Iterable [str ]) -> str : ... # type: ignore[misc]
494+ @overload
495+ def ljust (self : LiteralString , __width : SupportsIndex , __fillchar : LiteralString = " " ) -> LiteralString : ...
496+ @overload
468497 def ljust (self , __width : SupportsIndex , __fillchar : str = " " ) -> str : ... # type: ignore[misc]
498+ @overload
499+ def lower (self : LiteralString ) -> LiteralString : ...
500+ @overload
469501 def lower (self ) -> str : ... # type: ignore[misc]
502+ @overload
503+ def lstrip (self : LiteralString , __chars : LiteralString | None = None ) -> LiteralString : ...
504+ @overload
470505 def lstrip (self , __chars : str | None = None ) -> str : ... # type: ignore[misc]
506+ @overload
507+ def partition (self : LiteralString , __sep : LiteralString ) -> tuple [LiteralString , LiteralString , LiteralString ]: ...
508+ @overload
471509 def partition (self , __sep : str ) -> tuple [str , str , str ]: ... # type: ignore[misc]
510+ @overload
511+ def replace (
512+ self : LiteralString , __old : LiteralString , __new : LiteralString , __count : SupportsIndex = - 1
513+ ) -> LiteralString : ...
514+ @overload
472515 def replace (self , __old : str , __new : str , __count : SupportsIndex = - 1 ) -> str : ... # type: ignore[misc]
473516 if sys .version_info >= (3 , 9 ):
517+ @overload
518+ def removeprefix (self : LiteralString , __prefix : LiteralString ) -> LiteralString : ...
519+ @overload
474520 def removeprefix (self , __prefix : str ) -> str : ... # type: ignore[misc]
521+ @overload
522+ def removesuffix (self : LiteralString , __suffix : LiteralString ) -> LiteralString : ...
523+ @overload
475524 def removesuffix (self , __suffix : str ) -> str : ... # type: ignore[misc]
476525
477526 def rfind (self , __sub : str , __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...) -> int : ...
478527 def rindex (self , __sub : str , __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...) -> int : ...
528+ @overload
529+ def rjust (self : LiteralString , __width : SupportsIndex , __fillchar : LiteralString = " " ) -> LiteralString : ...
530+ @overload
479531 def rjust (self , __width : SupportsIndex , __fillchar : str = " " ) -> str : ... # type: ignore[misc]
532+ @overload
533+ def rpartition (self : LiteralString , __sep : LiteralString ) -> tuple [LiteralString , LiteralString , LiteralString ]: ...
534+ @overload
480535 def rpartition (self , __sep : str ) -> tuple [str , str , str ]: ... # type: ignore[misc]
536+ @overload
537+ def rsplit (self : LiteralString , sep : LiteralString | None = None , maxsplit : SupportsIndex = - 1 ) -> list [LiteralString ]: ...
538+ @overload
481539 def rsplit (self , sep : str | None = None , maxsplit : SupportsIndex = - 1 ) -> list [str ]: ... # type: ignore[misc]
540+ @overload
541+ def rstrip (self : LiteralString , __chars : LiteralString | None = None ) -> LiteralString : ...
542+ @overload
482543 def rstrip (self , __chars : str | None = None ) -> str : ... # type: ignore[misc]
544+ @overload
545+ def split (self : LiteralString , sep : LiteralString | None = None , maxsplit : SupportsIndex = - 1 ) -> list [LiteralString ]: ...
546+ @overload
483547 def split (self , sep : str | None = None , maxsplit : SupportsIndex = - 1 ) -> list [str ]: ... # type: ignore[misc]
548+ @overload
549+ def splitlines (self : LiteralString , keepends : bool = False ) -> list [LiteralString ]: ...
550+ @overload
484551 def splitlines (self , keepends : bool = False ) -> list [str ]: ... # type: ignore[misc]
485552 def startswith (
486553 self , __prefix : str | tuple [str , ...], __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...
487554 ) -> bool : ...
555+ @overload
556+ def strip (self : LiteralString , __chars : LiteralString | None = None ) -> LiteralString : ...
557+ @overload
488558 def strip (self , __chars : str | None = None ) -> str : ... # type: ignore[misc]
559+ @overload
560+ def swapcase (self : LiteralString ) -> LiteralString : ...
561+ @overload
489562 def swapcase (self ) -> str : ... # type: ignore[misc]
563+ @overload
564+ def title (self : LiteralString ) -> LiteralString : ...
565+ @overload
490566 def title (self ) -> str : ... # type: ignore[misc]
491567 def translate (self , __table : _TranslateTable ) -> str : ...
568+ @overload
569+ def upper (self : LiteralString ) -> LiteralString : ...
570+ @overload
492571 def upper (self ) -> str : ... # type: ignore[misc]
572+ @overload
573+ def zfill (self : LiteralString , __width : SupportsIndex ) -> LiteralString : ...
574+ @overload
493575 def zfill (self , __width : SupportsIndex ) -> str : ... # type: ignore[misc]
494576 @staticmethod
495577 @overload
@@ -500,6 +582,9 @@ class str(Sequence[str]):
500582 @staticmethod
501583 @overload
502584 def maketrans (__x : str , __y : str , __z : str ) -> dict [int , int | None ]: ...
585+ @overload
586+ def __add__ (self : LiteralString , __value : LiteralString ) -> LiteralString : ...
587+ @overload
503588 def __add__ (self , __value : str ) -> str : ... # type: ignore[misc]
504589 # Incompatible with Sequence.__contains__
505590 def __contains__ (self , __key : str ) -> bool : ... # type: ignore[override]
@@ -508,17 +593,31 @@ class str(Sequence[str]):
508593 def __getitem__ (self , __key : SupportsIndex | slice ) -> str : ...
509594 def __gt__ (self , __value : str ) -> bool : ...
510595 def __hash__ (self ) -> int : ...
596+ @overload
597+ def __iter__ (self : LiteralString ) -> Iterator [LiteralString ]: ...
598+ @overload
511599 def __iter__ (self ) -> Iterator [str ]: ... # type: ignore[misc]
512600 def __le__ (self , __value : str ) -> bool : ...
513601 def __len__ (self ) -> int : ...
514602 def __lt__ (self , __value : str ) -> bool : ...
603+ @overload
604+ def __mod__ (self : LiteralString , __value : LiteralString | tuple [LiteralString , ...]) -> LiteralString : ...
605+ @overload
515606 def __mod__ (self , __value : Any ) -> str : ...
607+ @overload
608+ def __mul__ (self : LiteralString , __value : SupportsIndex ) -> LiteralString : ...
609+ @overload
516610 def __mul__ (self , __value : SupportsIndex ) -> str : ... # type: ignore[misc]
517611 def __ne__ (self , __value : object ) -> bool : ...
612+ @overload
613+ def __rmul__ (self : LiteralString , __value : SupportsIndex ) -> LiteralString : ...
614+ @overload
518615 def __rmul__ (self , __value : SupportsIndex ) -> str : ... # type: ignore[misc]
519616 def __getnewargs__ (self ) -> tuple [str ]: ...
520617
521618class bytes (Sequence [int ]):
619+ @overload
620+ def __new__ (cls ) -> Literal [b"" ]: ...
522621 @overload
523622 def __new__ (cls , __o : Iterable [SupportsIndex ] | SupportsIndex | SupportsBytes | ReadableBuffer ) -> Self : ...
524623 @overload
@@ -805,8 +904,21 @@ class memoryview(Sequence[int]):
805904 def __buffer__ (self , __flags : int ) -> memoryview : ...
806905 def __release_buffer__ (self , __buffer : memoryview ) -> None : ...
807906
907+ class _Truthy (Protocol ):
908+ def __bool__ (self ) -> Literal [True ]: ...
909+
910+ class _Falsy (Protocol ):
911+ def __bool__ (self ) -> Literal [False ]: ...
912+
808913@final
809914class bool (int ):
915+ @overload
916+ def __new__ (cls ) -> Literal [False ]: ...
917+ @overload
918+ def __new__ (cls , __o : _Truthy ) -> Literal [True ]: ...
919+ @overload
920+ def __new__ (cls , __o : _Falsy ) -> Literal [False ]: ...
921+ @overload
810922 def __new__ (cls , __o : object = ...) -> Self : ...
811923 # The following overloads could be represented more elegantly with a TypeVar("_B", bool, int),
812924 # however mypy has a bug regarding TypeVar constraints (https://github.com/python/mypy/issues/11880).
@@ -1676,11 +1788,11 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit
16761788# Instead, we special-case the most common examples of this: bool and literal integers.
16771789if sys .version_info >= (3 , 8 ):
16781790 @overload
1679- def sum (__iterable : Iterable [bool ], start : int = 0 ) -> int : ... # type: ignore[misc]
1791+ def sum (__iterable : Iterable [bool | _LiteralInteger ], start : int = 0 ) -> int : ... # type: ignore[misc]
16801792
16811793else :
16821794 @overload
1683- def sum (__iterable : Iterable [bool ], __start : int = 0 ) -> int : ... # type: ignore[misc]
1795+ def sum (__iterable : Iterable [bool | _LiteralInteger ], __start : int = 0 ) -> int : ... # type: ignore[misc]
16841796
16851797@overload
16861798def sum (__iterable : Iterable [_SupportsSumNoDefaultT ]) -> _SupportsSumNoDefaultT | Literal [0 ]: ...
0 commit comments