Skip to content

Commit 9d27e2f

Browse files
committed
Sync typeshed
Source commit: python/typeshed@73ebb9d
1 parent 8104d01 commit 9d27e2f

33 files changed

+368
-241
lines changed

mypy/typeshed/stdlib/_asyncio.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from asyncio.events import AbstractEventLoop
3-
from collections.abc import Awaitable, Callable, Coroutine, Generator, Iterable
3+
from collections.abc import Awaitable, Callable, Coroutine, Generator
44
from contextvars import Context
55
from types import FrameType
66
from typing import Any, Literal, TextIO, TypeVar
@@ -13,7 +13,7 @@ _T = TypeVar("_T")
1313
_T_co = TypeVar("_T_co", covariant=True)
1414
_TaskYieldType: TypeAlias = Future[object] | None
1515

16-
class Future(Awaitable[_T], Iterable[_T]):
16+
class Future(Awaitable[_T]):
1717
_state: str
1818
@property
1919
def _exception(self) -> BaseException | None: ...

mypy/typeshed/stdlib/_ctypes.pyi

+5-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ class Array(_CData, Generic[_CT], metaclass=_PyCArrayType):
286286
def _type_(self) -> type[_CT]: ...
287287
@_type_.setter
288288
def _type_(self, value: type[_CT]) -> None: ...
289-
raw: bytes # Note: only available if _CT == c_char
289+
# Note: only available if _CT == c_char
290+
@property
291+
def raw(self) -> bytes: ...
292+
@raw.setter
293+
def raw(self, value: ReadableBuffer) -> None: ...
290294
value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise
291295
# TODO These methods cannot be annotated correctly at the moment.
292296
# All of these "Any"s stand for the array's element type, but it's not possible to use _CT

mypy/typeshed/stdlib/_decimal.pyi

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ _TrapType: TypeAlias = type[DecimalException]
2727
__version__: Final[str]
2828
__libmpdec_version__: Final[str]
2929

30-
ROUND_DOWN: Final[str]
31-
ROUND_HALF_UP: Final[str]
32-
ROUND_HALF_EVEN: Final[str]
33-
ROUND_CEILING: Final[str]
34-
ROUND_FLOOR: Final[str]
35-
ROUND_UP: Final[str]
36-
ROUND_HALF_DOWN: Final[str]
37-
ROUND_05UP: Final[str]
30+
ROUND_DOWN: Final = "ROUND_DOWN"
31+
ROUND_HALF_UP: Final = "ROUND_HALF_UP"
32+
ROUND_HALF_EVEN: Final = "ROUND_HALF_EVEN"
33+
ROUND_CEILING: Final = "ROUND_CEILING"
34+
ROUND_FLOOR: Final = "ROUND_FLOOR"
35+
ROUND_UP: Final = "ROUND_UP"
36+
ROUND_HALF_DOWN: Final = "ROUND_HALF_DOWN"
37+
ROUND_05UP: Final = "ROUND_05UP"
3838
HAVE_CONTEXTVAR: Final[bool]
3939
HAVE_THREADS: Final[bool]
4040
MAX_EMAX: Final[int]

mypy/typeshed/stdlib/_socket.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ if sys.platform == "win32":
7878
SO_EXCLUSIVEADDRUSE: int
7979
if sys.platform != "win32":
8080
SO_REUSEPORT: int
81-
if sys.platform != "darwin" or sys.version_info >= (3, 13):
81+
if sys.platform != "darwin":
8282
SO_BINDTODEVICE: int
8383

8484
if sys.platform != "win32" and sys.platform != "darwin":

mypy/typeshed/stdlib/argparse.pyi

+10-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from _typeshed import SupportsWrite, sentinel
33
from collections.abc import Callable, Generator, Iterable, Sequence
44
from re import Pattern
5-
from typing import IO, Any, ClassVar, Final, Generic, NewType, NoReturn, Protocol, TypeVar, overload
5+
from typing import IO, Any, ClassVar, Final, Generic, NoReturn, Protocol, TypeVar, overload
66
from typing_extensions import Self, TypeAlias, deprecated
77

88
__all__ = [
@@ -33,25 +33,14 @@ _ActionT = TypeVar("_ActionT", bound=Action)
3333
_ArgumentParserT = TypeVar("_ArgumentParserT", bound=ArgumentParser)
3434
_N = TypeVar("_N")
3535
_ActionType: TypeAlias = Callable[[str], Any] | FileType | str
36-
# more precisely, Literal["store", "store_const", "store_true",
37-
# "store_false", "append", "append_const", "count", "help", "version",
38-
# "extend"], but using this would make it hard to annotate callers
39-
# that don't use a literal argument
40-
_ActionStr: TypeAlias = str
41-
# more precisely, Literal["?", "*", "+", "...", "A...",
42-
# "==SUPPRESS=="], but using this would make it hard to annotate
43-
# callers that don't use a literal argument
44-
_NArgsStr: TypeAlias = str
4536

4637
ONE_OR_MORE: Final = "+"
4738
OPTIONAL: Final = "?"
4839
PARSER: Final = "A..."
4940
REMAINDER: Final = "..."
50-
_SUPPRESS_T = NewType("_SUPPRESS_T", str)
51-
SUPPRESS: _SUPPRESS_T | str # not using Literal because argparse sometimes compares SUPPRESS with is
52-
# the | str is there so that foo = argparse.SUPPRESS; foo = "test" checks out in mypy
41+
SUPPRESS: Final = "==SUPPRESS=="
5342
ZERO_OR_MORE: Final = "*"
54-
_UNRECOGNIZED_ARGS_ATTR: Final[str] # undocumented
43+
_UNRECOGNIZED_ARGS_ATTR: Final = "_unrecognized_args" # undocumented
5544

5645
class ArgumentError(Exception):
5746
argument_name: str | None
@@ -86,8 +75,13 @@ class _ActionsContainer:
8675
def add_argument(
8776
self,
8877
*name_or_flags: str,
89-
action: _ActionStr | type[Action] = ...,
90-
nargs: int | _NArgsStr | _SUPPRESS_T | None = None,
78+
# str covers predefined actions ("store_true", "count", etc.)
79+
# and user registered actions via the `register` method.
80+
action: str | type[Action] = ...,
81+
# more precisely, Literal["?", "*", "+", "...", "A...", "==SUPPRESS=="],
82+
# but using this would make it hard to annotate callers that don't use a
83+
# literal argument and for subclasses to override this method.
84+
nargs: int | str | None = None,
9185
const: Any = ...,
9286
default: Any = ...,
9387
type: _ActionType = ...,

mypy/typeshed/stdlib/bdb.pyi

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
from _typeshed import ExcInfo, TraceFunction, Unused
3-
from collections.abc import Callable, Iterable, Mapping
3+
from collections.abc import Callable, Iterable, Iterator, Mapping
4+
from contextlib import contextmanager
45
from types import CodeType, FrameType, TracebackType
56
from typing import IO, Any, Final, SupportsInt, TypeVar
67
from typing_extensions import ParamSpec
@@ -30,6 +31,10 @@ class Bdb:
3031
def __init__(self, skip: Iterable[str] | None = None) -> None: ...
3132
def canonic(self, filename: str) -> str: ...
3233
def reset(self) -> None: ...
34+
if sys.version_info >= (3, 12):
35+
@contextmanager
36+
def set_enterframe(self, frame: FrameType) -> Iterator[None]: ...
37+
3338
def trace_dispatch(self, frame: FrameType, event: str, arg: Any) -> TraceFunction: ...
3439
def dispatch_line(self, frame: FrameType) -> TraceFunction: ...
3540
def dispatch_call(self, frame: FrameType, arg: None) -> TraceFunction: ...

mypy/typeshed/stdlib/builtins.pyi

+105-7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ from typing import ( # noqa: Y022
6464
from typing_extensions import ( # noqa: Y023
6565
Concatenate,
6666
Literal,
67+
LiteralString,
6768
ParamSpec,
6869
Self,
6970
TypeAlias,
@@ -441,16 +442,31 @@ class str(Sequence[str]):
441442
def __new__(cls, object: object = ...) -> Self: ...
442443
@overload
443444
def __new__(cls, object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
445+
@overload
446+
def capitalize(self: LiteralString) -> LiteralString: ...
447+
@overload
444448
def capitalize(self) -> str: ... # type: ignore[misc]
449+
@overload
450+
def casefold(self: LiteralString) -> LiteralString: ...
451+
@overload
445452
def casefold(self) -> str: ... # type: ignore[misc]
453+
@overload
454+
def center(self: LiteralString, width: SupportsIndex, fillchar: LiteralString = " ", /) -> LiteralString: ...
455+
@overload
446456
def center(self, width: SupportsIndex, fillchar: str = " ", /) -> str: ... # type: ignore[misc]
447457
def count(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
448458
def encode(self, encoding: str = "utf-8", errors: str = "strict") -> bytes: ...
449459
def endswith(
450460
self, suffix: str | tuple[str, ...], start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /
451461
) -> bool: ...
462+
@overload
463+
def expandtabs(self: LiteralString, tabsize: SupportsIndex = 8) -> LiteralString: ...
464+
@overload
452465
def expandtabs(self, tabsize: SupportsIndex = 8) -> str: ... # type: ignore[misc]
453466
def find(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
467+
@overload
468+
def format(self: LiteralString, *args: LiteralString, **kwargs: LiteralString) -> LiteralString: ...
469+
@overload
454470
def format(self, *args: object, **kwargs: object) -> str: ...
455471
def format_map(self, mapping: _FormatMapMapping, /) -> str: ...
456472
def index(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
@@ -466,35 +482,99 @@ class str(Sequence[str]):
466482
def isspace(self) -> bool: ...
467483
def istitle(self) -> bool: ...
468484
def isupper(self) -> bool: ...
485+
@overload
486+
def join(self: LiteralString, iterable: Iterable[LiteralString], /) -> LiteralString: ...
487+
@overload
469488
def join(self, iterable: Iterable[str], /) -> str: ... # type: ignore[misc]
489+
@overload
490+
def ljust(self: LiteralString, width: SupportsIndex, fillchar: LiteralString = " ", /) -> LiteralString: ...
491+
@overload
470492
def ljust(self, width: SupportsIndex, fillchar: str = " ", /) -> str: ... # type: ignore[misc]
493+
@overload
494+
def lower(self: LiteralString) -> LiteralString: ...
495+
@overload
471496
def lower(self) -> str: ... # type: ignore[misc]
497+
@overload
498+
def lstrip(self: LiteralString, chars: LiteralString | None = None, /) -> LiteralString: ...
499+
@overload
472500
def lstrip(self, chars: str | None = None, /) -> str: ... # type: ignore[misc]
501+
@overload
502+
def partition(self: LiteralString, sep: LiteralString, /) -> tuple[LiteralString, LiteralString, LiteralString]: ...
503+
@overload
473504
def partition(self, sep: str, /) -> tuple[str, str, str]: ... # type: ignore[misc]
474505
if sys.version_info >= (3, 13):
506+
@overload
507+
def replace(
508+
self: LiteralString, old: LiteralString, new: LiteralString, /, count: SupportsIndex = -1
509+
) -> LiteralString: ...
510+
@overload
475511
def replace(self, old: str, new: str, /, count: SupportsIndex = -1) -> str: ... # type: ignore[misc]
476512
else:
513+
@overload
514+
def replace(
515+
self: LiteralString, old: LiteralString, new: LiteralString, count: SupportsIndex = -1, /
516+
) -> LiteralString: ...
517+
@overload
477518
def replace(self, old: str, new: str, count: SupportsIndex = -1, /) -> str: ... # type: ignore[misc]
478519
if sys.version_info >= (3, 9):
520+
@overload
521+
def removeprefix(self: LiteralString, prefix: LiteralString, /) -> LiteralString: ...
522+
@overload
479523
def removeprefix(self, prefix: str, /) -> str: ... # type: ignore[misc]
524+
@overload
525+
def removesuffix(self: LiteralString, suffix: LiteralString, /) -> LiteralString: ...
526+
@overload
480527
def removesuffix(self, suffix: str, /) -> str: ... # type: ignore[misc]
481528

482529
def rfind(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
483530
def rindex(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
531+
@overload
532+
def rjust(self: LiteralString, width: SupportsIndex, fillchar: LiteralString = " ", /) -> LiteralString: ...
533+
@overload
484534
def rjust(self, width: SupportsIndex, fillchar: str = " ", /) -> str: ... # type: ignore[misc]
535+
@overload
536+
def rpartition(self: LiteralString, sep: LiteralString, /) -> tuple[LiteralString, LiteralString, LiteralString]: ...
537+
@overload
485538
def rpartition(self, sep: str, /) -> tuple[str, str, str]: ... # type: ignore[misc]
539+
@overload
540+
def rsplit(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
541+
@overload
486542
def rsplit(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc]
543+
@overload
544+
def rstrip(self: LiteralString, chars: LiteralString | None = None, /) -> LiteralString: ...
545+
@overload
487546
def rstrip(self, chars: str | None = None, /) -> str: ... # type: ignore[misc]
547+
@overload
548+
def split(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
549+
@overload
488550
def split(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc]
551+
@overload
552+
def splitlines(self: LiteralString, keepends: bool = False) -> list[LiteralString]: ...
553+
@overload
489554
def splitlines(self, keepends: bool = False) -> list[str]: ... # type: ignore[misc]
490555
def startswith(
491556
self, prefix: str | tuple[str, ...], start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /
492557
) -> bool: ...
558+
@overload
559+
def strip(self: LiteralString, chars: LiteralString | None = None, /) -> LiteralString: ...
560+
@overload
493561
def strip(self, chars: str | None = None, /) -> str: ... # type: ignore[misc]
562+
@overload
563+
def swapcase(self: LiteralString) -> LiteralString: ...
564+
@overload
494565
def swapcase(self) -> str: ... # type: ignore[misc]
566+
@overload
567+
def title(self: LiteralString) -> LiteralString: ...
568+
@overload
495569
def title(self) -> str: ... # type: ignore[misc]
496570
def translate(self, table: _TranslateTable, /) -> str: ...
571+
@overload
572+
def upper(self: LiteralString) -> LiteralString: ...
573+
@overload
497574
def upper(self) -> str: ... # type: ignore[misc]
575+
@overload
576+
def zfill(self: LiteralString, width: SupportsIndex, /) -> LiteralString: ...
577+
@overload
498578
def zfill(self, width: SupportsIndex, /) -> str: ... # type: ignore[misc]
499579
@staticmethod
500580
@overload
@@ -505,21 +585,39 @@ class str(Sequence[str]):
505585
@staticmethod
506586
@overload
507587
def maketrans(x: str, y: str, z: str, /) -> dict[int, int | None]: ...
588+
@overload
589+
def __add__(self: LiteralString, value: LiteralString, /) -> LiteralString: ...
590+
@overload
508591
def __add__(self, value: str, /) -> str: ... # type: ignore[misc]
509592
# Incompatible with Sequence.__contains__
510593
def __contains__(self, key: str, /) -> bool: ... # type: ignore[override]
511594
def __eq__(self, value: object, /) -> bool: ...
512595
def __ge__(self, value: str, /) -> bool: ...
513-
def __getitem__(self, key: SupportsIndex | slice, /) -> str: ...
596+
@overload
597+
def __getitem__(self: LiteralString, key: SupportsIndex | slice, /) -> LiteralString: ...
598+
@overload
599+
def __getitem__(self, key: SupportsIndex | slice, /) -> str: ... # type: ignore[misc]
514600
def __gt__(self, value: str, /) -> bool: ...
515601
def __hash__(self) -> int: ...
602+
@overload
603+
def __iter__(self: LiteralString) -> Iterator[LiteralString]: ...
604+
@overload
516605
def __iter__(self) -> Iterator[str]: ... # type: ignore[misc]
517606
def __le__(self, value: str, /) -> bool: ...
518607
def __len__(self) -> int: ...
519608
def __lt__(self, value: str, /) -> bool: ...
609+
@overload
610+
def __mod__(self: LiteralString, value: LiteralString | tuple[LiteralString, ...], /) -> LiteralString: ...
611+
@overload
520612
def __mod__(self, value: Any, /) -> str: ...
613+
@overload
614+
def __mul__(self: LiteralString, value: SupportsIndex, /) -> LiteralString: ...
615+
@overload
521616
def __mul__(self, value: SupportsIndex, /) -> str: ... # type: ignore[misc]
522617
def __ne__(self, value: object, /) -> bool: ...
618+
@overload
619+
def __rmul__(self: LiteralString, value: SupportsIndex, /) -> LiteralString: ...
620+
@overload
523621
def __rmul__(self, value: SupportsIndex, /) -> str: ... # type: ignore[misc]
524622
def __getnewargs__(self) -> tuple[str]: ...
525623

@@ -1130,7 +1228,7 @@ class frozenset(AbstractSet[_T_co]):
11301228
if sys.version_info >= (3, 9):
11311229
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
11321230

1133-
class enumerate(Iterator[tuple[int, _T]]):
1231+
class enumerate(Generic[_T]):
11341232
def __new__(cls, iterable: Iterable[_T], start: int = 0) -> Self: ...
11351233
def __iter__(self) -> Self: ...
11361234
def __next__(self) -> tuple[int, _T]: ...
@@ -1324,7 +1422,7 @@ else:
13241422

13251423
exit: _sitebuiltins.Quitter
13261424

1327-
class filter(Iterator[_T]):
1425+
class filter(Generic[_T]):
13281426
@overload
13291427
def __new__(cls, function: None, iterable: Iterable[_T | None], /) -> Self: ...
13301428
@overload
@@ -1389,7 +1487,7 @@ license: _sitebuiltins._Printer
13891487

13901488
def locals() -> dict[str, Any]: ...
13911489

1392-
class map(Iterator[_S]):
1490+
class map(Generic[_S]):
13931491
@overload
13941492
def __new__(cls, func: Callable[[_T1], _S], iterable: Iterable[_T1], /) -> Self: ...
13951493
@overload
@@ -1632,7 +1730,7 @@ def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = None) -> complex
16321730

16331731
quit: _sitebuiltins.Quitter
16341732

1635-
class reversed(Iterator[_T]):
1733+
class reversed(Generic[_T]):
16361734
@overload
16371735
def __new__(cls, sequence: Reversible[_T], /) -> Iterator[_T]: ... # type: ignore[misc]
16381736
@overload
@@ -1680,7 +1778,7 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit
16801778
# without creating many false-positive errors (see #7578).
16811779
# Instead, we special-case the most common examples of this: bool and literal integers.
16821780
@overload
1683-
def sum(iterable: Iterable[bool], /, start: int = 0) -> int: ...
1781+
def sum(iterable: Iterable[bool | _LiteralInteger], /, start: int = 0) -> int: ...
16841782
@overload
16851783
def sum(iterable: Iterable[_SupportsSumNoDefaultT], /) -> _SupportsSumNoDefaultT | Literal[0]: ...
16861784
@overload
@@ -1693,7 +1791,7 @@ def vars(object: type, /) -> types.MappingProxyType[str, Any]: ...
16931791
@overload
16941792
def vars(object: Any = ..., /) -> dict[str, Any]: ...
16951793

1696-
class zip(Iterator[_T_co]):
1794+
class zip(Generic[_T_co]):
16971795
if sys.version_info >= (3, 10):
16981796
@overload
16991797
def __new__(cls, *, strict: bool = ...) -> zip[Any]: ...

mypy/typeshed/stdlib/cmath.pyi

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from typing import SupportsComplex, SupportsFloat, SupportsIndex
1+
from typing import Final, SupportsComplex, SupportsFloat, SupportsIndex
22
from typing_extensions import TypeAlias
33

4-
e: float
5-
pi: float
6-
inf: float
7-
infj: complex
8-
nan: float
9-
nanj: complex
10-
tau: float
4+
e: Final[float]
5+
pi: Final[float]
6+
inf: Final[float]
7+
infj: Final[complex]
8+
nan: Final[float]
9+
nanj: Final[complex]
10+
tau: Final[float]
1111

1212
_C: TypeAlias = SupportsFloat | SupportsComplex | SupportsIndex | complex
1313

0 commit comments

Comments
 (0)