Skip to content

Commit e81d52f

Browse files
author
mypybot
committed
Sync typeshed
Source commit: python/typeshed@ea0ae21
1 parent 28e5436 commit e81d52f

24 files changed

+415
-251
lines changed

mypy/typeshed/stdlib/_typeshed/__init__.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ AnyStr_co = TypeVar("AnyStr_co", str, bytes, covariant=True) # noqa: Y001
3636
# "Incomplete | None" instead of "Any | None".
3737
Incomplete: TypeAlias = Any
3838

39+
# To describe a function parameter that is unused and will work with anything.
40+
Unused: TypeAlias = object
41+
3942
# stable
4043
class IdentityFunction(Protocol):
4144
def __call__(self, __x: _T) -> _T: ...
@@ -205,6 +208,7 @@ class HasFileno(Protocol):
205208

206209
FileDescriptor: TypeAlias = int # stable
207210
FileDescriptorLike: TypeAlias = int | HasFileno # stable
211+
FileDescriptorOrPath: TypeAlias = int | StrOrBytesPath
208212

209213
# stable
210214
class SupportsRead(Protocol[_T_co]):

mypy/typeshed/stdlib/builtins.pyi

Lines changed: 103 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import types
44
from _collections_abc import dict_items, dict_keys, dict_values
55
from _typeshed import (
66
AnyStr_co,
7+
FileDescriptorOrPath,
78
OpenBinaryMode,
89
OpenBinaryModeReading,
910
OpenBinaryModeUpdating,
1011
OpenBinaryModeWriting,
1112
OpenTextMode,
1213
ReadableBuffer,
1314
Self,
14-
StrOrBytesPath,
1515
SupportsAdd,
1616
SupportsAiter,
1717
SupportsAnext,
@@ -54,7 +54,7 @@ from typing import ( # noqa: Y027
5454
overload,
5555
type_check_only,
5656
)
57-
from typing_extensions import Literal, SupportsIndex, TypeAlias, TypeGuard, final
57+
from typing_extensions import Literal, LiteralString, SupportsIndex, TypeAlias, TypeGuard, final
5858

5959
if sys.version_info >= (3, 9):
6060
from types import GenericAlias
@@ -413,20 +413,38 @@ class str(Sequence[str]):
413413
def __new__(cls: type[Self], object: object = ...) -> Self: ...
414414
@overload
415415
def __new__(cls: type[Self], object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
416+
@overload
417+
def capitalize(self: LiteralString) -> LiteralString: ...
418+
@overload
416419
def capitalize(self) -> str: ... # type: ignore[misc]
420+
@overload
421+
def casefold(self: LiteralString) -> LiteralString: ...
422+
@overload
417423
def casefold(self) -> str: ... # type: ignore[misc]
424+
@overload
425+
def center(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
426+
@overload
418427
def center(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ... # type: ignore[misc]
419428
def count(self, x: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
420429
def encode(self, encoding: str = ..., errors: str = ...) -> bytes: ...
421430
def endswith(
422431
self, __suffix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
423432
) -> bool: ...
424433
if sys.version_info >= (3, 8):
434+
@overload
435+
def expandtabs(self: LiteralString, tabsize: SupportsIndex = ...) -> LiteralString: ...
436+
@overload
425437
def expandtabs(self, tabsize: SupportsIndex = ...) -> str: ... # type: ignore[misc]
426438
else:
439+
@overload
440+
def expandtabs(self: LiteralString, tabsize: int = ...) -> LiteralString: ...
441+
@overload
427442
def expandtabs(self, tabsize: int = ...) -> str: ... # type: ignore[misc]
428443

429444
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
430448
def format(self, *args: object, **kwargs: object) -> str: ... # type: ignore[misc]
431449
def format_map(self, map: _FormatMapMapping) -> str: ...
432450
def index(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
@@ -442,53 +460,127 @@ class str(Sequence[str]):
442460
def isspace(self) -> bool: ...
443461
def istitle(self) -> bool: ...
444462
def isupper(self) -> bool: ...
463+
@overload
464+
def join(self: LiteralString, __iterable: Iterable[LiteralString]) -> LiteralString: ...
465+
@overload
445466
def join(self, __iterable: Iterable[str]) -> str: ... # type: ignore[misc]
467+
@overload
468+
def ljust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
469+
@overload
446470
def ljust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ... # type: ignore[misc]
471+
@overload
472+
def lower(self: LiteralString) -> LiteralString: ...
473+
@overload
447474
def lower(self) -> str: ... # type: ignore[misc]
475+
@overload
476+
def lstrip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
477+
@overload
448478
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
449482
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
450488
def replace(self, __old: str, __new: str, __count: SupportsIndex = ...) -> str: ... # type: ignore[misc]
451489
if sys.version_info >= (3, 9):
490+
@overload
491+
def removeprefix(self: LiteralString, __prefix: LiteralString) -> LiteralString: ...
492+
@overload
452493
def removeprefix(self, __prefix: str) -> str: ... # type: ignore[misc]
494+
@overload
495+
def removesuffix(self: LiteralString, __suffix: LiteralString) -> LiteralString: ...
496+
@overload
453497
def removesuffix(self, __suffix: str) -> str: ... # type: ignore[misc]
454498

455499
def rfind(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
456500
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
457504
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
458508
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
459512
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
460516
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
461520
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
462524
def splitlines(self, keepends: bool = ...) -> list[str]: ... # type: ignore[misc]
463525
def startswith(
464526
self, __prefix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
465527
) -> bool: ...
528+
@overload
529+
def strip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
530+
@overload
466531
def strip(self, __chars: str | None = ...) -> str: ... # type: ignore[misc]
532+
@overload
533+
def swapcase(self: LiteralString) -> LiteralString: ...
534+
@overload
467535
def swapcase(self) -> str: ... # type: ignore[misc]
536+
@overload
537+
def title(self: LiteralString) -> LiteralString: ...
538+
@overload
468539
def title(self) -> str: ... # type: ignore[misc]
469540
def translate(self, __table: _TranslateTable) -> str: ...
541+
@overload
542+
def upper(self: LiteralString) -> LiteralString: ...
543+
@overload
470544
def upper(self) -> str: ... # type: ignore[misc]
545+
@overload
546+
def zfill(self: LiteralString, __width: SupportsIndex) -> LiteralString: ...
547+
@overload
471548
def zfill(self, __width: SupportsIndex) -> str: ... # type: ignore[misc]
472549
@staticmethod
473550
@overload
474551
def maketrans(__x: dict[int, _T] | dict[str, _T] | dict[str | int, _T]) -> dict[int, _T]: ...
475552
@staticmethod
476553
@overload
477554
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
478558
def __add__(self, __s: str) -> str: ... # type: ignore[misc]
479559
# Incompatible with Sequence.__contains__
480560
def __contains__(self, __o: str) -> bool: ... # type: ignore[override]
481561
def __eq__(self, __x: object) -> bool: ...
482562
def __ge__(self, __x: str) -> bool: ...
483563
def __getitem__(self, __i: SupportsIndex | slice) -> str: ...
484564
def __gt__(self, __x: str) -> bool: ...
565+
@overload
566+
def __iter__(self: LiteralString) -> Iterator[LiteralString]: ...
567+
@overload
485568
def __iter__(self) -> Iterator[str]: ... # type: ignore[misc]
486569
def __le__(self, __x: str) -> bool: ...
487570
def __len__(self) -> int: ...
488571
def __lt__(self, __x: str) -> bool: ...
572+
@overload
573+
def __mod__(self: LiteralString, __x: LiteralString | tuple[LiteralString, ...]) -> LiteralString: ...
574+
@overload
489575
def __mod__(self, __x: Any) -> str: ... # type: ignore[misc]
576+
@overload
577+
def __mul__(self: LiteralString, __n: SupportsIndex) -> LiteralString: ...
578+
@overload
490579
def __mul__(self, __n: SupportsIndex) -> str: ... # type: ignore[misc]
491580
def __ne__(self, __x: object) -> bool: ...
581+
@overload
582+
def __rmul__(self: LiteralString, __n: SupportsIndex) -> LiteralString: ...
583+
@overload
492584
def __rmul__(self, __n: SupportsIndex) -> str: ... # type: ignore[misc]
493585
def __getnewargs__(self) -> tuple[str]: ...
494586

@@ -1320,13 +1412,12 @@ def next(__i: SupportsNext[_T]) -> _T: ...
13201412
def next(__i: SupportsNext[_T], __default: _VT) -> _T | _VT: ...
13211413
def oct(__number: int | SupportsIndex) -> str: ...
13221414

1323-
_OpenFile = StrOrBytesPath | int # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed
13241415
_Opener: TypeAlias = Callable[[str, int], int]
13251416

13261417
# Text mode: always returns a TextIOWrapper
13271418
@overload
13281419
def open(
1329-
file: _OpenFile,
1420+
file: FileDescriptorOrPath,
13301421
mode: OpenTextMode = ...,
13311422
buffering: int = ...,
13321423
encoding: str | None = ...,
@@ -1339,7 +1430,7 @@ def open(
13391430
# Unbuffered binary mode: returns a FileIO
13401431
@overload
13411432
def open(
1342-
file: _OpenFile,
1433+
file: FileDescriptorOrPath,
13431434
mode: OpenBinaryMode,
13441435
buffering: Literal[0],
13451436
encoding: None = ...,
@@ -1352,7 +1443,7 @@ def open(
13521443
# Buffering is on: return BufferedRandom, BufferedReader, or BufferedWriter
13531444
@overload
13541445
def open(
1355-
file: _OpenFile,
1446+
file: FileDescriptorOrPath,
13561447
mode: OpenBinaryModeUpdating,
13571448
buffering: Literal[-1, 1] = ...,
13581449
encoding: None = ...,
@@ -1363,7 +1454,7 @@ def open(
13631454
) -> BufferedRandom: ...
13641455
@overload
13651456
def open(
1366-
file: _OpenFile,
1457+
file: FileDescriptorOrPath,
13671458
mode: OpenBinaryModeWriting,
13681459
buffering: Literal[-1, 1] = ...,
13691460
encoding: None = ...,
@@ -1374,7 +1465,7 @@ def open(
13741465
) -> BufferedWriter: ...
13751466
@overload
13761467
def open(
1377-
file: _OpenFile,
1468+
file: FileDescriptorOrPath,
13781469
mode: OpenBinaryModeReading,
13791470
buffering: Literal[-1, 1] = ...,
13801471
encoding: None = ...,
@@ -1387,7 +1478,7 @@ def open(
13871478
# Buffering cannot be determined: fall back to BinaryIO
13881479
@overload
13891480
def open(
1390-
file: _OpenFile,
1481+
file: FileDescriptorOrPath,
13911482
mode: OpenBinaryMode,
13921483
buffering: int = ...,
13931484
encoding: None = ...,
@@ -1400,7 +1491,7 @@ def open(
14001491
# Fallback if mode is not specified
14011492
@overload
14021493
def open(
1403-
file: _OpenFile,
1494+
file: FileDescriptorOrPath,
14041495
mode: str,
14051496
buffering: int = ...,
14061497
encoding: str | None = ...,
@@ -1565,11 +1656,11 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit
15651656
# Instead, we special-case the most common examples of this: bool and literal integers.
15661657
if sys.version_info >= (3, 8):
15671658
@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]
15691660

15701661
else:
15711662
@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]
15731664

15741665
@overload
15751666
def sum(__iterable: Iterable[_SupportsSumNoDefaultT]) -> _SupportsSumNoDefaultT | Literal[0]: ...

mypy/typeshed/stdlib/compileall.pyi

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ __all__ = ["compile_dir", "compile_file", "compile_path"]
88
class _SupportsSearch(Protocol):
99
def search(self, string: str) -> Any: ...
1010

11-
if sys.version_info >= (3, 9):
11+
if sys.version_info >= (3, 10):
1212
def compile_dir(
1313
dir: StrPath,
1414
maxlevels: int | None = ...,
@@ -21,7 +21,7 @@ if sys.version_info >= (3, 9):
2121
workers: int = ...,
2222
invalidation_mode: PycInvalidationMode | None = ...,
2323
*,
24-
stripdir: str | None = ..., # TODO: change to StrPath | None once https://bugs.python.org/issue40447 is resolved
24+
stripdir: StrPath | None = ...,
2525
prependdir: StrPath | None = ...,
2626
limit_sl_dest: StrPath | None = ...,
2727
hardlink_dupes: bool = ...,
@@ -36,7 +36,41 @@ if sys.version_info >= (3, 9):
3636
optimize: int = ...,
3737
invalidation_mode: PycInvalidationMode | None = ...,
3838
*,
39-
stripdir: str | None = ..., # TODO: change to StrPath | None once https://bugs.python.org/issue40447 is resolved
39+
stripdir: StrPath | None = ...,
40+
prependdir: StrPath | None = ...,
41+
limit_sl_dest: StrPath | None = ...,
42+
hardlink_dupes: bool = ...,
43+
) -> int: ...
44+
45+
elif sys.version_info >= (3, 9):
46+
def compile_dir(
47+
dir: StrPath,
48+
maxlevels: int | None = ...,
49+
ddir: StrPath | None = ...,
50+
force: bool = ...,
51+
rx: _SupportsSearch | None = ...,
52+
quiet: int = ...,
53+
legacy: bool = ...,
54+
optimize: int = ...,
55+
workers: int = ...,
56+
invalidation_mode: PycInvalidationMode | None = ...,
57+
*,
58+
stripdir: str | None = ..., # https://bugs.python.org/issue40447
59+
prependdir: StrPath | None = ...,
60+
limit_sl_dest: StrPath | None = ...,
61+
hardlink_dupes: bool = ...,
62+
) -> int: ...
63+
def compile_file(
64+
fullname: StrPath,
65+
ddir: StrPath | None = ...,
66+
force: bool = ...,
67+
rx: _SupportsSearch | None = ...,
68+
quiet: int = ...,
69+
legacy: bool = ...,
70+
optimize: int = ...,
71+
invalidation_mode: PycInvalidationMode | None = ...,
72+
*,
73+
stripdir: str | None = ..., # https://bugs.python.org/issue40447
4074
prependdir: StrPath | None = ...,
4175
limit_sl_dest: StrPath | None = ...,
4276
hardlink_dupes: bool = ...,

mypy/typeshed/stdlib/contextlib.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22
import sys
3-
from _typeshed import Self, StrOrBytesPath
3+
from _typeshed import FileDescriptorOrPath, Self
44
from abc import abstractmethod
55
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Generator, Iterator
66
from types import TracebackType
@@ -193,7 +193,7 @@ else:
193193
def __exit__(self, *exctype: object) -> None: ...
194194

195195
if sys.version_info >= (3, 11):
196-
_T_fd_or_any_path = TypeVar("_T_fd_or_any_path", bound=int | StrOrBytesPath)
196+
_T_fd_or_any_path = TypeVar("_T_fd_or_any_path", bound=FileDescriptorOrPath)
197197

198198
class chdir(AbstractContextManager[None], Generic[_T_fd_or_any_path]):
199199
path: _T_fd_or_any_path

mypy/typeshed/stdlib/ctypes/__init__.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ class Array(Generic[_CT], _CData):
266266
def _type_(self) -> type[_CT]: ...
267267
@_type_.setter
268268
def _type_(self, value: type[_CT]) -> None: ...
269-
raw: bytes # Note: only available if _CT == c_char
269+
# Note: only available if _CT == c_char
270+
@property
271+
def raw(self) -> bytes: ...
272+
@raw.setter
273+
def raw(self, value: ReadableBuffer) -> None: ...
270274
value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise
271275
# TODO These methods cannot be annotated correctly at the moment.
272276
# All of these "Any"s stand for the array's element type, but it's not possible to use _CT

mypy/typeshed/stdlib/distutils/dist.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from _typeshed import StrOrBytesPath, SupportsWrite
1+
from _typeshed import FileDescriptorOrPath, SupportsWrite
22
from collections.abc import Iterable, Mapping
33
from distutils.cmd import Command
44
from typing import IO, Any
55

66
class DistributionMetadata:
7-
def __init__(self, path: int | StrOrBytesPath | None = ...) -> None: ...
7+
def __init__(self, path: FileDescriptorOrPath | None = ...) -> None: ...
88
name: str | None
99
version: str | None
1010
author: str | None

0 commit comments

Comments
 (0)