@@ -4,14 +4,14 @@ import types
4
4
from _collections_abc import dict_items , dict_keys , dict_values
5
5
from _typeshed import (
6
6
AnyStr_co ,
7
+ FileDescriptorOrPath ,
7
8
OpenBinaryMode ,
8
9
OpenBinaryModeReading ,
9
10
OpenBinaryModeUpdating ,
10
11
OpenBinaryModeWriting ,
11
12
OpenTextMode ,
12
13
ReadableBuffer ,
13
14
Self ,
14
- StrOrBytesPath ,
15
15
SupportsAdd ,
16
16
SupportsAiter ,
17
17
SupportsAnext ,
@@ -54,7 +54,7 @@ from typing import ( # noqa: Y027
54
54
overload ,
55
55
type_check_only ,
56
56
)
57
- from typing_extensions import Literal , SupportsIndex , TypeAlias , TypeGuard , final
57
+ from typing_extensions import Literal , LiteralString , SupportsIndex , TypeAlias , TypeGuard , final
58
58
59
59
if sys .version_info >= (3 , 9 ):
60
60
from types import GenericAlias
@@ -413,20 +413,38 @@ class str(Sequence[str]):
413
413
def __new__ (cls : type [Self ], object : object = ...) -> Self : ...
414
414
@overload
415
415
def __new__ (cls : type [Self ], object : ReadableBuffer , encoding : str = ..., errors : str = ...) -> Self : ...
416
+ @overload
417
+ def capitalize (self : LiteralString ) -> LiteralString : ...
418
+ @overload
416
419
def capitalize (self ) -> str : ... # type: ignore[misc]
420
+ @overload
421
+ def casefold (self : LiteralString ) -> LiteralString : ...
422
+ @overload
417
423
def casefold (self ) -> str : ... # type: ignore[misc]
424
+ @overload
425
+ def center (self : LiteralString , __width : SupportsIndex , __fillchar : LiteralString = ...) -> LiteralString : ...
426
+ @overload
418
427
def center (self , __width : SupportsIndex , __fillchar : str = ...) -> str : ... # type: ignore[misc]
419
428
def count (self , x : str , __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...) -> int : ...
420
429
def encode (self , encoding : str = ..., errors : str = ...) -> bytes : ...
421
430
def endswith (
422
431
self , __suffix : str | tuple [str , ...], __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...
423
432
) -> bool : ...
424
433
if sys .version_info >= (3 , 8 ):
434
+ @overload
435
+ def expandtabs (self : LiteralString , tabsize : SupportsIndex = ...) -> LiteralString : ...
436
+ @overload
425
437
def expandtabs (self , tabsize : SupportsIndex = ...) -> str : ... # type: ignore[misc]
426
438
else :
439
+ @overload
440
+ def expandtabs (self : LiteralString , tabsize : int = ...) -> LiteralString : ...
441
+ @overload
427
442
def expandtabs (self , tabsize : int = ...) -> str : ... # type: ignore[misc]
428
443
429
444
def find (self , __sub : str , __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...) -> int : ...
445
+ @overload
446
+ def format (self : LiteralString , * args : LiteralString , ** kwargs : LiteralString ) -> LiteralString : ...
447
+ @overload
430
448
def format (self , * args : object , ** kwargs : object ) -> str : ... # type: ignore[misc]
431
449
def format_map (self , map : _FormatMapMapping ) -> str : ...
432
450
def index (self , __sub : str , __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...) -> int : ...
@@ -442,53 +460,127 @@ class str(Sequence[str]):
442
460
def isspace (self ) -> bool : ...
443
461
def istitle (self ) -> bool : ...
444
462
def isupper (self ) -> bool : ...
463
+ @overload
464
+ def join (self : LiteralString , __iterable : Iterable [LiteralString ]) -> LiteralString : ...
465
+ @overload
445
466
def join (self , __iterable : Iterable [str ]) -> str : ... # type: ignore[misc]
467
+ @overload
468
+ def ljust (self : LiteralString , __width : SupportsIndex , __fillchar : LiteralString = ...) -> LiteralString : ...
469
+ @overload
446
470
def ljust (self , __width : SupportsIndex , __fillchar : str = ...) -> str : ... # type: ignore[misc]
471
+ @overload
472
+ def lower (self : LiteralString ) -> LiteralString : ...
473
+ @overload
447
474
def lower (self ) -> str : ... # type: ignore[misc]
475
+ @overload
476
+ def lstrip (self : LiteralString , __chars : LiteralString | None = ...) -> LiteralString : ...
477
+ @overload
448
478
def lstrip (self , __chars : str | None = ...) -> str : ... # type: ignore[misc]
479
+ @overload
480
+ def partition (self : LiteralString , __sep : LiteralString ) -> tuple [LiteralString , LiteralString , LiteralString ]: ...
481
+ @overload
449
482
def partition (self , __sep : str ) -> tuple [str , str , str ]: ... # type: ignore[misc]
483
+ @overload
484
+ def replace (
485
+ self : LiteralString , __old : LiteralString , __new : LiteralString , __count : SupportsIndex = ...
486
+ ) -> LiteralString : ...
487
+ @overload
450
488
def replace (self , __old : str , __new : str , __count : SupportsIndex = ...) -> str : ... # type: ignore[misc]
451
489
if sys .version_info >= (3 , 9 ):
490
+ @overload
491
+ def removeprefix (self : LiteralString , __prefix : LiteralString ) -> LiteralString : ...
492
+ @overload
452
493
def removeprefix (self , __prefix : str ) -> str : ... # type: ignore[misc]
494
+ @overload
495
+ def removesuffix (self : LiteralString , __suffix : LiteralString ) -> LiteralString : ...
496
+ @overload
453
497
def removesuffix (self , __suffix : str ) -> str : ... # type: ignore[misc]
454
498
455
499
def rfind (self , __sub : str , __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...) -> int : ...
456
500
def rindex (self , __sub : str , __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...) -> int : ...
501
+ @overload
502
+ def rjust (self : LiteralString , __width : SupportsIndex , __fillchar : LiteralString = ...) -> LiteralString : ...
503
+ @overload
457
504
def rjust (self , __width : SupportsIndex , __fillchar : str = ...) -> str : ... # type: ignore[misc]
505
+ @overload
506
+ def rpartition (self : LiteralString , __sep : LiteralString ) -> tuple [LiteralString , LiteralString , LiteralString ]: ...
507
+ @overload
458
508
def rpartition (self , __sep : str ) -> tuple [str , str , str ]: ... # type: ignore[misc]
509
+ @overload
510
+ def rsplit (self : LiteralString , sep : LiteralString | None = ..., maxsplit : SupportsIndex = ...) -> list [LiteralString ]: ...
511
+ @overload
459
512
def rsplit (self , sep : str | None = ..., maxsplit : SupportsIndex = ...) -> list [str ]: ... # type: ignore[misc]
513
+ @overload
514
+ def rstrip (self : LiteralString , __chars : LiteralString | None = ...) -> LiteralString : ...
515
+ @overload
460
516
def rstrip (self , __chars : str | None = ...) -> str : ... # type: ignore[misc]
517
+ @overload
518
+ def split (self : LiteralString , sep : LiteralString | None = ..., maxsplit : SupportsIndex = ...) -> list [LiteralString ]: ...
519
+ @overload
461
520
def split (self , sep : str | None = ..., maxsplit : SupportsIndex = ...) -> list [str ]: ... # type: ignore[misc]
521
+ @overload
522
+ def splitlines (self : LiteralString , keepends : bool = ...) -> list [LiteralString ]: ...
523
+ @overload
462
524
def splitlines (self , keepends : bool = ...) -> list [str ]: ... # type: ignore[misc]
463
525
def startswith (
464
526
self , __prefix : str | tuple [str , ...], __start : SupportsIndex | None = ..., __end : SupportsIndex | None = ...
465
527
) -> bool : ...
528
+ @overload
529
+ def strip (self : LiteralString , __chars : LiteralString | None = ...) -> LiteralString : ...
530
+ @overload
466
531
def strip (self , __chars : str | None = ...) -> str : ... # type: ignore[misc]
532
+ @overload
533
+ def swapcase (self : LiteralString ) -> LiteralString : ...
534
+ @overload
467
535
def swapcase (self ) -> str : ... # type: ignore[misc]
536
+ @overload
537
+ def title (self : LiteralString ) -> LiteralString : ...
538
+ @overload
468
539
def title (self ) -> str : ... # type: ignore[misc]
469
540
def translate (self , __table : _TranslateTable ) -> str : ...
541
+ @overload
542
+ def upper (self : LiteralString ) -> LiteralString : ...
543
+ @overload
470
544
def upper (self ) -> str : ... # type: ignore[misc]
545
+ @overload
546
+ def zfill (self : LiteralString , __width : SupportsIndex ) -> LiteralString : ...
547
+ @overload
471
548
def zfill (self , __width : SupportsIndex ) -> str : ... # type: ignore[misc]
472
549
@staticmethod
473
550
@overload
474
551
def maketrans (__x : dict [int , _T ] | dict [str , _T ] | dict [str | int , _T ]) -> dict [int , _T ]: ...
475
552
@staticmethod
476
553
@overload
477
554
def maketrans (__x : str , __y : str , __z : str | None = ...) -> dict [int , int | None ]: ...
555
+ @overload
556
+ def __add__ (self : LiteralString , __s : LiteralString ) -> LiteralString : ...
557
+ @overload
478
558
def __add__ (self , __s : str ) -> str : ... # type: ignore[misc]
479
559
# Incompatible with Sequence.__contains__
480
560
def __contains__ (self , __o : str ) -> bool : ... # type: ignore[override]
481
561
def __eq__ (self , __x : object ) -> bool : ...
482
562
def __ge__ (self , __x : str ) -> bool : ...
483
563
def __getitem__ (self , __i : SupportsIndex | slice ) -> str : ...
484
564
def __gt__ (self , __x : str ) -> bool : ...
565
+ @overload
566
+ def __iter__ (self : LiteralString ) -> Iterator [LiteralString ]: ...
567
+ @overload
485
568
def __iter__ (self ) -> Iterator [str ]: ... # type: ignore[misc]
486
569
def __le__ (self , __x : str ) -> bool : ...
487
570
def __len__ (self ) -> int : ...
488
571
def __lt__ (self , __x : str ) -> bool : ...
572
+ @overload
573
+ def __mod__ (self : LiteralString , __x : LiteralString | tuple [LiteralString , ...]) -> LiteralString : ...
574
+ @overload
489
575
def __mod__ (self , __x : Any ) -> str : ... # type: ignore[misc]
576
+ @overload
577
+ def __mul__ (self : LiteralString , __n : SupportsIndex ) -> LiteralString : ...
578
+ @overload
490
579
def __mul__ (self , __n : SupportsIndex ) -> str : ... # type: ignore[misc]
491
580
def __ne__ (self , __x : object ) -> bool : ...
581
+ @overload
582
+ def __rmul__ (self : LiteralString , __n : SupportsIndex ) -> LiteralString : ...
583
+ @overload
492
584
def __rmul__ (self , __n : SupportsIndex ) -> str : ... # type: ignore[misc]
493
585
def __getnewargs__ (self ) -> tuple [str ]: ...
494
586
@@ -1320,13 +1412,12 @@ def next(__i: SupportsNext[_T]) -> _T: ...
1320
1412
def next (__i : SupportsNext [_T ], __default : _VT ) -> _T | _VT : ...
1321
1413
def oct (__number : int | SupportsIndex ) -> str : ...
1322
1414
1323
- _OpenFile = StrOrBytesPath | int # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed
1324
1415
_Opener : TypeAlias = Callable [[str , int ], int ]
1325
1416
1326
1417
# Text mode: always returns a TextIOWrapper
1327
1418
@overload
1328
1419
def open (
1329
- file : _OpenFile ,
1420
+ file : FileDescriptorOrPath ,
1330
1421
mode : OpenTextMode = ...,
1331
1422
buffering : int = ...,
1332
1423
encoding : str | None = ...,
@@ -1339,7 +1430,7 @@ def open(
1339
1430
# Unbuffered binary mode: returns a FileIO
1340
1431
@overload
1341
1432
def open (
1342
- file : _OpenFile ,
1433
+ file : FileDescriptorOrPath ,
1343
1434
mode : OpenBinaryMode ,
1344
1435
buffering : Literal [0 ],
1345
1436
encoding : None = ...,
@@ -1352,7 +1443,7 @@ def open(
1352
1443
# Buffering is on: return BufferedRandom, BufferedReader, or BufferedWriter
1353
1444
@overload
1354
1445
def open (
1355
- file : _OpenFile ,
1446
+ file : FileDescriptorOrPath ,
1356
1447
mode : OpenBinaryModeUpdating ,
1357
1448
buffering : Literal [- 1 , 1 ] = ...,
1358
1449
encoding : None = ...,
@@ -1363,7 +1454,7 @@ def open(
1363
1454
) -> BufferedRandom : ...
1364
1455
@overload
1365
1456
def open (
1366
- file : _OpenFile ,
1457
+ file : FileDescriptorOrPath ,
1367
1458
mode : OpenBinaryModeWriting ,
1368
1459
buffering : Literal [- 1 , 1 ] = ...,
1369
1460
encoding : None = ...,
@@ -1374,7 +1465,7 @@ def open(
1374
1465
) -> BufferedWriter : ...
1375
1466
@overload
1376
1467
def open (
1377
- file : _OpenFile ,
1468
+ file : FileDescriptorOrPath ,
1378
1469
mode : OpenBinaryModeReading ,
1379
1470
buffering : Literal [- 1 , 1 ] = ...,
1380
1471
encoding : None = ...,
@@ -1387,7 +1478,7 @@ def open(
1387
1478
# Buffering cannot be determined: fall back to BinaryIO
1388
1479
@overload
1389
1480
def open (
1390
- file : _OpenFile ,
1481
+ file : FileDescriptorOrPath ,
1391
1482
mode : OpenBinaryMode ,
1392
1483
buffering : int = ...,
1393
1484
encoding : None = ...,
@@ -1400,7 +1491,7 @@ def open(
1400
1491
# Fallback if mode is not specified
1401
1492
@overload
1402
1493
def open (
1403
- file : _OpenFile ,
1494
+ file : FileDescriptorOrPath ,
1404
1495
mode : str ,
1405
1496
buffering : int = ...,
1406
1497
encoding : str | None = ...,
@@ -1565,11 +1656,11 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit
1565
1656
# Instead, we special-case the most common examples of this: bool and literal integers.
1566
1657
if sys .version_info >= (3 , 8 ):
1567
1658
@overload
1568
- def sum (__iterable : Iterable [bool ], start : int = ...) -> int : ... # type: ignore[misc]
1659
+ def sum (__iterable : Iterable [bool | _LiteralInteger ], start : int = ...) -> int : ... # type: ignore[misc]
1569
1660
1570
1661
else :
1571
1662
@overload
1572
- def sum (__iterable : Iterable [bool ], __start : int = ...) -> int : ... # type: ignore[misc]
1663
+ def sum (__iterable : Iterable [bool | _LiteralInteger ], __start : int = ...) -> int : ... # type: ignore[misc]
1573
1664
1574
1665
@overload
1575
1666
def sum (__iterable : Iterable [_SupportsSumNoDefaultT ]) -> _SupportsSumNoDefaultT | Literal [0 ]: ...
0 commit comments