Skip to content

Commit 9bb102a

Browse files
cdce8pJelleZijlstra
authored andcommitted
Sync typeshed
Source commit: python/typeshed@5ce34dc
1 parent 3804f7e commit 9bb102a

File tree

4 files changed

+191
-64
lines changed

4 files changed

+191
-64
lines changed

mypy/typeshed/stdlib/_ctypes.pyi

+5-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ class Array(_CData, Generic[_CT]):
167167
def _type_(self) -> type[_CT]: ...
168168
@_type_.setter
169169
def _type_(self, value: type[_CT]) -> None: ...
170-
raw: bytes # Note: only available if _CT == c_char
170+
# Note: only available if _CT == c_char
171+
@property
172+
def raw(self) -> bytes: ...
173+
@raw.setter
174+
def raw(self, value: ReadableBuffer) -> None: ...
171175
value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise
172176
# TODO These methods cannot be annotated correctly at the moment.
173177
# All of these "Any"s stand for the array's element type, but it's not possible to use _CT

mypy/typeshed/stdlib/builtins.pyi

+89-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ from typing import ( # noqa: Y022
6161
from typing_extensions import ( # noqa: Y023
6262
Concatenate,
6363
Literal,
64+
LiteralString,
6465
ParamSpec,
6566
Self,
6667
TypeAlias,
@@ -434,19 +435,31 @@ class str(Sequence[str]):
434435
def __new__(cls, object: object = ...) -> Self: ...
435436
@overload
436437
def __new__(cls, object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
438+
@overload
439+
def capitalize(self: LiteralString) -> LiteralString: ...
440+
@overload
437441
def capitalize(self) -> str: ... # type: ignore[misc]
442+
@overload
443+
def casefold(self: LiteralString) -> LiteralString: ...
444+
@overload
438445
def casefold(self) -> str: ... # type: ignore[misc]
446+
@overload
447+
def center(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = " ") -> LiteralString: ...
448+
@overload
439449
def center(self, __width: SupportsIndex, __fillchar: str = " ") -> str: ... # type: ignore[misc]
440450
def count(self, x: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
441451
def encode(self, encoding: str = "utf-8", errors: str = "strict") -> bytes: ...
442452
def endswith(
443453
self, __suffix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
444454
) -> bool: ...
445455
@overload
446-
def expandtabs(self: str, tabsize: SupportsIndex = 8) -> str: ...
456+
def expandtabs(self: LiteralString, tabsize: SupportsIndex = 8) -> LiteralString: ...
447457
@overload
448458
def expandtabs(self, tabsize: SupportsIndex = 8) -> str: ... # type: ignore[misc]
449459
def find(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
460+
@overload
461+
def format(self: LiteralString, *args: LiteralString, **kwargs: LiteralString) -> LiteralString: ...
462+
@overload
450463
def format(self, *args: object, **kwargs: object) -> str: ...
451464
def format_map(self, map: _FormatMapMapping) -> str: ...
452465
def index(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
@@ -462,32 +475,91 @@ class str(Sequence[str]):
462475
def isspace(self) -> bool: ...
463476
def istitle(self) -> bool: ...
464477
def isupper(self) -> bool: ...
478+
@overload
479+
def join(self: LiteralString, __iterable: Iterable[LiteralString]) -> LiteralString: ...
480+
@overload
465481
def join(self, __iterable: Iterable[str]) -> str: ... # type: ignore[misc]
482+
@overload
483+
def ljust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = " ") -> LiteralString: ...
484+
@overload
466485
def ljust(self, __width: SupportsIndex, __fillchar: str = " ") -> str: ... # type: ignore[misc]
486+
@overload
487+
def lower(self: LiteralString) -> LiteralString: ...
488+
@overload
467489
def lower(self) -> str: ... # type: ignore[misc]
490+
@overload
491+
def lstrip(self: LiteralString, __chars: LiteralString | None = None) -> LiteralString: ...
492+
@overload
468493
def lstrip(self, __chars: str | None = None) -> str: ... # type: ignore[misc]
494+
@overload
495+
def partition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
496+
@overload
469497
def partition(self, __sep: str) -> tuple[str, str, str]: ... # type: ignore[misc]
498+
@overload
499+
def replace(
500+
self: LiteralString, __old: LiteralString, __new: LiteralString, __count: SupportsIndex = -1
501+
) -> LiteralString: ...
502+
@overload
470503
def replace(self, __old: str, __new: str, __count: SupportsIndex = -1) -> str: ... # type: ignore[misc]
471504
if sys.version_info >= (3, 9):
505+
@overload
506+
def removeprefix(self: LiteralString, __prefix: LiteralString) -> LiteralString: ...
507+
@overload
472508
def removeprefix(self, __prefix: str) -> str: ... # type: ignore[misc]
509+
@overload
510+
def removesuffix(self: LiteralString, __suffix: LiteralString) -> LiteralString: ...
511+
@overload
473512
def removesuffix(self, __suffix: str) -> str: ... # type: ignore[misc]
474513

475514
def rfind(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
476515
def rindex(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
516+
@overload
517+
def rjust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = " ") -> LiteralString: ...
518+
@overload
477519
def rjust(self, __width: SupportsIndex, __fillchar: str = " ") -> str: ... # type: ignore[misc]
520+
@overload
521+
def rpartition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
522+
@overload
478523
def rpartition(self, __sep: str) -> tuple[str, str, str]: ... # type: ignore[misc]
524+
@overload
525+
def rsplit(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
526+
@overload
479527
def rsplit(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc]
528+
@overload
529+
def rstrip(self: LiteralString, __chars: LiteralString | None = None) -> LiteralString: ...
530+
@overload
480531
def rstrip(self, __chars: str | None = None) -> str: ... # type: ignore[misc]
532+
@overload
533+
def split(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
534+
@overload
481535
def split(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc]
536+
@overload
537+
def splitlines(self: LiteralString, keepends: bool = False) -> list[LiteralString]: ...
538+
@overload
482539
def splitlines(self, keepends: bool = False) -> list[str]: ... # type: ignore[misc]
483540
def startswith(
484541
self, __prefix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
485542
) -> bool: ...
543+
@overload
544+
def strip(self: LiteralString, __chars: LiteralString | None = None) -> LiteralString: ...
545+
@overload
486546
def strip(self, __chars: str | None = None) -> str: ... # type: ignore[misc]
547+
@overload
548+
def swapcase(self: LiteralString) -> LiteralString: ...
549+
@overload
487550
def swapcase(self) -> str: ... # type: ignore[misc]
551+
@overload
552+
def title(self: LiteralString) -> LiteralString: ...
553+
@overload
488554
def title(self) -> str: ... # type: ignore[misc]
489555
def translate(self, __table: _TranslateTable) -> str: ...
556+
@overload
557+
def upper(self: LiteralString) -> LiteralString: ...
558+
@overload
490559
def upper(self) -> str: ... # type: ignore[misc]
560+
@overload
561+
def zfill(self: LiteralString, __width: SupportsIndex) -> LiteralString: ...
562+
@overload
491563
def zfill(self, __width: SupportsIndex) -> str: ... # type: ignore[misc]
492564
@staticmethod
493565
@overload
@@ -498,6 +570,9 @@ class str(Sequence[str]):
498570
@staticmethod
499571
@overload
500572
def maketrans(__x: str, __y: str, __z: str) -> dict[int, int | None]: ...
573+
@overload
574+
def __add__(self: LiteralString, __value: LiteralString) -> LiteralString: ...
575+
@overload
501576
def __add__(self, __value: str) -> str: ... # type: ignore[misc]
502577
# Incompatible with Sequence.__contains__
503578
def __contains__(self, __key: str) -> bool: ... # type: ignore[override]
@@ -506,13 +581,25 @@ class str(Sequence[str]):
506581
def __getitem__(self, __key: SupportsIndex | slice) -> str: ...
507582
def __gt__(self, __value: str) -> bool: ...
508583
def __hash__(self) -> int: ...
584+
@overload
585+
def __iter__(self: LiteralString) -> Iterator[LiteralString]: ...
586+
@overload
509587
def __iter__(self) -> Iterator[str]: ... # type: ignore[misc]
510588
def __le__(self, __value: str) -> bool: ...
511589
def __len__(self) -> int: ...
512590
def __lt__(self, __value: str) -> bool: ...
591+
@overload
592+
def __mod__(self: LiteralString, __value: LiteralString | tuple[LiteralString, ...]) -> LiteralString: ...
593+
@overload
513594
def __mod__(self, __value: Any) -> str: ...
595+
@overload
596+
def __mul__(self: LiteralString, __value: SupportsIndex) -> LiteralString: ...
597+
@overload
514598
def __mul__(self, __value: SupportsIndex) -> str: ... # type: ignore[misc]
515599
def __ne__(self, __value: object) -> bool: ...
600+
@overload
601+
def __rmul__(self: LiteralString, __value: SupportsIndex) -> LiteralString: ...
602+
@overload
516603
def __rmul__(self, __value: SupportsIndex) -> str: ... # type: ignore[misc]
517604
def __getnewargs__(self) -> tuple[str]: ...
518605

@@ -1593,7 +1680,7 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit
15931680
# without creating many false-positive errors (see #7578).
15941681
# Instead, we special-case the most common examples of this: bool and literal integers.
15951682
@overload
1596-
def sum(__iterable: Iterable[bool], start: int = 0) -> int: ... # type: ignore[overload-overlap]
1683+
def sum(__iterable: Iterable[bool | _LiteralInteger], start: int = 0) -> int: ... # type: ignore[overload-overlap]
15971684
@overload
15981685
def sum(__iterable: Iterable[_SupportsSumNoDefaultT]) -> _SupportsSumNoDefaultT | Literal[0]: ...
15991686
@overload

mypy/typeshed/stdlib/functools.pyi

+26-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import sys
22
import types
3-
from _typeshed import IdentityFunction, SupportsAllComparisons, SupportsItems
3+
from _typeshed import SupportsAllComparisons, SupportsItems
44
from collections.abc import Callable, Hashable, Iterable, Sequence, Sized
55
from typing import Any, Generic, Literal, NamedTuple, TypedDict, TypeVar, final, overload
6-
from typing_extensions import Self, TypeAlias
6+
from typing_extensions import ParamSpec, Self, TypeAlias
77

88
if sys.version_info >= (3, 9):
99
from types import GenericAlias
@@ -27,10 +27,12 @@ __all__ = [
2727
if sys.version_info >= (3, 9):
2828
__all__ += ["cache"]
2929

30-
_AnyCallable: TypeAlias = Callable[..., object]
31-
3230
_T = TypeVar("_T")
3331
_S = TypeVar("_S")
32+
_PWrapped = ParamSpec("_PWrapped")
33+
_RWrapped = TypeVar("_RWrapped")
34+
_PWrapper = ParamSpec("_PWrapper")
35+
_RWrapper = TypeVar("_RWrapper")
3436

3537
@overload
3638
def reduce(__function: Callable[[_T, _S], _T], __sequence: Iterable[_S], __initial: _T) -> _T: ...
@@ -80,31 +82,41 @@ else:
8082
]
8183
WRAPPER_UPDATES: tuple[Literal["__dict__"]]
8284

85+
class _Wrapped(Generic[_PWrapped, _RWrapped, _PWrapper, _RWrapper]):
86+
__wrapped__: Callable[_PWrapped, _RWrapped]
87+
def __call__(self, *args: _PWrapper.args, **kwargs: _PWrapper.kwargs) -> _RWrapper: ...
88+
# as with ``Callable``, we'll assume that these attributes exist
89+
__name__: str
90+
__qualname__: str
91+
92+
class _Wrapper(Generic[_PWrapped, _RWrapped]):
93+
def __call__(self, f: Callable[_PWrapper, _RWrapper]) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWrapper]: ...
94+
8395
if sys.version_info >= (3, 12):
8496
def update_wrapper(
85-
wrapper: _T,
86-
wrapped: _AnyCallable,
97+
wrapper: Callable[_PWrapper, _RWrapper],
98+
wrapped: Callable[_PWrapped, _RWrapped],
8799
assigned: Sequence[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__", "__type_params__"),
88100
updated: Sequence[str] = ("__dict__",),
89-
) -> _T: ...
101+
) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWrapper]: ...
90102
def wraps(
91-
wrapped: _AnyCallable,
103+
wrapped: Callable[_PWrapped, _RWrapped],
92104
assigned: Sequence[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__", "__type_params__"),
93105
updated: Sequence[str] = ("__dict__",),
94-
) -> IdentityFunction: ...
106+
) -> _Wrapper[_PWrapped, _RWrapped]: ...
95107

96108
else:
97109
def update_wrapper(
98-
wrapper: _T,
99-
wrapped: _AnyCallable,
110+
wrapper: Callable[_PWrapper, _RWrapper],
111+
wrapped: Callable[_PWrapped, _RWrapped],
100112
assigned: Sequence[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__"),
101113
updated: Sequence[str] = ("__dict__",),
102-
) -> _T: ...
114+
) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWrapper]: ...
103115
def wraps(
104-
wrapped: _AnyCallable,
116+
wrapped: Callable[_PWrapped, _RWrapped],
105117
assigned: Sequence[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__"),
106118
updated: Sequence[str] = ("__dict__",),
107-
) -> IdentityFunction: ...
119+
) -> _Wrapper[_PWrapped, _RWrapped]: ...
108120

109121
def total_ordering(cls: type[_T]) -> type[_T]: ...
110122
def cmp_to_key(mycmp: Callable[[_T, _T], int]) -> Callable[[_T], SupportsAllComparisons]: ...

0 commit comments

Comments
 (0)