Skip to content

Commit 85dac76

Browse files
AlexWaygoodJelleZijlstra
authored andcommitted
Sync typeshed
Source commit: python/typeshed@a544b75
1 parent 9cb4872 commit 85dac76

17 files changed

+216
-41
lines changed

mypy/typeshed/stdlib/VERSIONS

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ importlib: 2.7-
152152
importlib.metadata: 3.8-
153153
importlib.metadata._meta: 3.10-
154154
importlib.resources: 3.7-
155+
importlib.resources.abc: 3.11-
155156
inspect: 2.7-
156157
io: 2.7-
157158
ipaddress: 3.3-

mypy/typeshed/stdlib/abc.pyi

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import _typeshed
22
import sys
33
from _typeshed import SupportsWrite
44
from collections.abc import Callable
5-
from typing import Any, Generic, TypeVar
6-
from typing_extensions import Literal
5+
from typing import Any, TypeVar
6+
from typing_extensions import Concatenate, Literal, ParamSpec
77

88
_T = TypeVar("_T")
99
_R_co = TypeVar("_R_co", covariant=True)
1010
_FuncT = TypeVar("_FuncT", bound=Callable[..., Any])
11+
_P = ParamSpec("_P")
1112

1213
# These definitions have special processing in mypy
1314
class ABCMeta(type):
@@ -28,13 +29,13 @@ class ABCMeta(type):
2829

2930
def abstractmethod(funcobj: _FuncT) -> _FuncT: ...
3031

31-
class abstractclassmethod(classmethod[_R_co], Generic[_R_co]):
32+
class abstractclassmethod(classmethod[_T, _P, _R_co]):
3233
__isabstractmethod__: Literal[True]
33-
def __init__(self: abstractclassmethod[_R_co], callable: Callable[..., _R_co]) -> None: ...
34+
def __init__(self, callable: Callable[Concatenate[_T, _P], _R_co]) -> None: ...
3435

35-
class abstractstaticmethod(staticmethod[_R_co], Generic[_R_co]):
36+
class abstractstaticmethod(staticmethod[_P, _R_co]):
3637
__isabstractmethod__: Literal[True]
37-
def __init__(self, callable: Callable[..., _R_co]) -> None: ...
38+
def __init__(self, callable: Callable[_P, _R_co]) -> None: ...
3839

3940
class abstractproperty(property):
4041
__isabstractmethod__: Literal[True]

mypy/typeshed/stdlib/builtins.pyi

+107-14
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ from typing import ( # noqa: Y022
5454
overload,
5555
type_check_only,
5656
)
57-
from typing_extensions import Literal, Self, SupportsIndex, TypeAlias, TypeGuard, final
57+
from typing_extensions import Concatenate, Literal, LiteralString, ParamSpec, Self, SupportsIndex, TypeAlias, TypeGuard, final
5858

5959
if sys.version_info >= (3, 9):
6060
from types import GenericAlias
@@ -75,6 +75,7 @@ _SupportsNextT = TypeVar("_SupportsNextT", bound=SupportsNext[Any], covariant=Tr
7575
_SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant=True)
7676
_AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any])
7777
_AwaitableT_co = TypeVar("_AwaitableT_co", bound=Awaitable[Any], covariant=True)
78+
_P = ParamSpec("_P")
7879

7980
class object:
8081
__doc__: str | None
@@ -113,32 +114,32 @@ class object:
113114
@classmethod
114115
def __subclasshook__(cls, __subclass: type) -> bool: ...
115116

116-
class staticmethod(Generic[_R_co]):
117+
class staticmethod(Generic[_P, _R_co]):
117118
@property
118-
def __func__(self) -> Callable[..., _R_co]: ...
119+
def __func__(self) -> Callable[_P, _R_co]: ...
119120
@property
120121
def __isabstractmethod__(self) -> bool: ...
121-
def __init__(self: staticmethod[_R_co], __f: Callable[..., _R_co]) -> None: ...
122-
def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[..., _R_co]: ...
122+
def __init__(self, __f: Callable[_P, _R_co]) -> None: ...
123+
def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[_P, _R_co]: ...
123124
if sys.version_info >= (3, 10):
124125
__name__: str
125126
__qualname__: str
126127
@property
127-
def __wrapped__(self) -> Callable[..., _R_co]: ...
128-
def __call__(self, *args: Any, **kwargs: Any) -> _R_co: ...
128+
def __wrapped__(self) -> Callable[_P, _R_co]: ...
129+
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R_co: ...
129130

130-
class classmethod(Generic[_R_co]):
131+
class classmethod(Generic[_T, _P, _R_co]):
131132
@property
132-
def __func__(self) -> Callable[..., _R_co]: ...
133+
def __func__(self) -> Callable[Concatenate[_T, _P], _R_co]: ...
133134
@property
134135
def __isabstractmethod__(self) -> bool: ...
135-
def __init__(self: classmethod[_R_co], __f: Callable[..., _R_co]) -> None: ...
136-
def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[..., _R_co]: ...
136+
def __init__(self, __f: Callable[Concatenate[_T, _P], _R_co]) -> None: ...
137+
def __get__(self, __instance: _T, __owner: type[_T] | None = None) -> Callable[_P, _R_co]: ...
137138
if sys.version_info >= (3, 10):
138139
__name__: str
139140
__qualname__: str
140141
@property
141-
def __wrapped__(self) -> Callable[..., _R_co]: ...
142+
def __wrapped__(self) -> Callable[Concatenate[_T, _P], _R_co]: ...
142143

143144
class type:
144145
@property
@@ -415,20 +416,38 @@ class str(Sequence[str]):
415416
def __new__(cls, object: object = ...) -> Self: ...
416417
@overload
417418
def __new__(cls, object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
419+
@overload
420+
def capitalize(self: LiteralString) -> LiteralString: ...
421+
@overload
418422
def capitalize(self) -> str: ... # type: ignore[misc]
423+
@overload
424+
def casefold(self: LiteralString) -> LiteralString: ...
425+
@overload
419426
def casefold(self) -> str: ... # type: ignore[misc]
427+
@overload
428+
def center(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = " ") -> LiteralString: ...
429+
@overload
420430
def center(self, __width: SupportsIndex, __fillchar: str = " ") -> str: ... # type: ignore[misc]
421431
def count(self, x: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
422432
def encode(self, encoding: str = "utf-8", errors: str = "strict") -> bytes: ...
423433
def endswith(
424434
self, __suffix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
425435
) -> bool: ...
426436
if sys.version_info >= (3, 8):
437+
@overload
438+
def expandtabs(self: LiteralString, tabsize: SupportsIndex = 8) -> LiteralString: ...
439+
@overload
427440
def expandtabs(self, tabsize: SupportsIndex = 8) -> str: ... # type: ignore[misc]
428441
else:
442+
@overload
443+
def expandtabs(self: LiteralString, tabsize: int = 8) -> LiteralString: ...
444+
@overload
429445
def expandtabs(self, tabsize: int = 8) -> str: ... # type: ignore[misc]
430446

431447
def find(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
448+
@overload
449+
def format(self: LiteralString, *args: LiteralString, **kwargs: LiteralString) -> LiteralString: ...
450+
@overload
432451
def format(self, *args: object, **kwargs: object) -> str: ...
433452
def format_map(self, map: _FormatMapMapping) -> str: ...
434453
def index(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
@@ -444,32 +463,91 @@ class str(Sequence[str]):
444463
def isspace(self) -> bool: ...
445464
def istitle(self) -> bool: ...
446465
def isupper(self) -> bool: ...
466+
@overload
467+
def join(self: LiteralString, __iterable: Iterable[LiteralString]) -> LiteralString: ...
468+
@overload
447469
def join(self, __iterable: Iterable[str]) -> str: ... # type: ignore[misc]
470+
@overload
471+
def ljust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = " ") -> LiteralString: ...
472+
@overload
448473
def ljust(self, __width: SupportsIndex, __fillchar: str = " ") -> str: ... # type: ignore[misc]
474+
@overload
475+
def lower(self: LiteralString) -> LiteralString: ...
476+
@overload
449477
def lower(self) -> str: ... # type: ignore[misc]
478+
@overload
479+
def lstrip(self: LiteralString, __chars: LiteralString | None = None) -> LiteralString: ...
480+
@overload
450481
def lstrip(self, __chars: str | None = None) -> str: ... # type: ignore[misc]
482+
@overload
483+
def partition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
484+
@overload
451485
def partition(self, __sep: str) -> tuple[str, str, str]: ... # type: ignore[misc]
486+
@overload
487+
def replace(
488+
self: LiteralString, __old: LiteralString, __new: LiteralString, __count: SupportsIndex = -1
489+
) -> LiteralString: ...
490+
@overload
452491
def replace(self, __old: str, __new: str, __count: SupportsIndex = -1) -> str: ... # type: ignore[misc]
453492
if sys.version_info >= (3, 9):
493+
@overload
494+
def removeprefix(self: LiteralString, __prefix: LiteralString) -> LiteralString: ...
495+
@overload
454496
def removeprefix(self, __prefix: str) -> str: ... # type: ignore[misc]
497+
@overload
498+
def removesuffix(self: LiteralString, __suffix: LiteralString) -> LiteralString: ...
499+
@overload
455500
def removesuffix(self, __suffix: str) -> str: ... # type: ignore[misc]
456501

457502
def rfind(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
458503
def rindex(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
504+
@overload
505+
def rjust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = " ") -> LiteralString: ...
506+
@overload
459507
def rjust(self, __width: SupportsIndex, __fillchar: str = " ") -> str: ... # type: ignore[misc]
508+
@overload
509+
def rpartition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
510+
@overload
460511
def rpartition(self, __sep: str) -> tuple[str, str, str]: ... # type: ignore[misc]
512+
@overload
513+
def rsplit(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
514+
@overload
461515
def rsplit(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc]
516+
@overload
517+
def rstrip(self: LiteralString, __chars: LiteralString | None = None) -> LiteralString: ...
518+
@overload
462519
def rstrip(self, __chars: str | None = None) -> str: ... # type: ignore[misc]
520+
@overload
521+
def split(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
522+
@overload
463523
def split(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc]
524+
@overload
525+
def splitlines(self: LiteralString, keepends: bool = False) -> list[LiteralString]: ...
526+
@overload
464527
def splitlines(self, keepends: bool = False) -> list[str]: ... # type: ignore[misc]
465528
def startswith(
466529
self, __prefix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
467530
) -> bool: ...
531+
@overload
532+
def strip(self: LiteralString, __chars: LiteralString | None = None) -> LiteralString: ...
533+
@overload
468534
def strip(self, __chars: str | None = None) -> str: ... # type: ignore[misc]
535+
@overload
536+
def swapcase(self: LiteralString) -> LiteralString: ...
537+
@overload
469538
def swapcase(self) -> str: ... # type: ignore[misc]
539+
@overload
540+
def title(self: LiteralString) -> LiteralString: ...
541+
@overload
470542
def title(self) -> str: ... # type: ignore[misc]
471543
def translate(self, __table: _TranslateTable) -> str: ...
544+
@overload
545+
def upper(self: LiteralString) -> LiteralString: ...
546+
@overload
472547
def upper(self) -> str: ... # type: ignore[misc]
548+
@overload
549+
def zfill(self: LiteralString, __width: SupportsIndex) -> LiteralString: ...
550+
@overload
473551
def zfill(self, __width: SupportsIndex) -> str: ... # type: ignore[misc]
474552
@staticmethod
475553
@overload
@@ -480,20 +558,35 @@ class str(Sequence[str]):
480558
@staticmethod
481559
@overload
482560
def maketrans(__x: str, __y: str, __z: str) -> dict[int, int | None]: ...
561+
@overload
562+
def __add__(self: LiteralString, __value: LiteralString) -> LiteralString: ...
563+
@overload
483564
def __add__(self, __value: str) -> str: ... # type: ignore[misc]
484565
# Incompatible with Sequence.__contains__
485566
def __contains__(self, __key: str) -> bool: ... # type: ignore[override]
486567
def __eq__(self, __value: object) -> bool: ...
487568
def __ge__(self, __value: str) -> bool: ...
488569
def __getitem__(self, __key: SupportsIndex | slice) -> str: ...
489570
def __gt__(self, __value: str) -> bool: ...
571+
@overload
572+
def __iter__(self: LiteralString) -> Iterator[LiteralString]: ...
573+
@overload
490574
def __iter__(self) -> Iterator[str]: ... # type: ignore[misc]
491575
def __le__(self, __value: str) -> bool: ...
492576
def __len__(self) -> int: ...
493577
def __lt__(self, __value: str) -> bool: ...
578+
@overload
579+
def __mod__(self: LiteralString, __value: LiteralString | tuple[LiteralString, ...]) -> LiteralString: ...
580+
@overload
494581
def __mod__(self, __value: Any) -> str: ...
582+
@overload
583+
def __mul__(self: LiteralString, __value: SupportsIndex) -> LiteralString: ...
584+
@overload
495585
def __mul__(self, __value: SupportsIndex) -> str: ... # type: ignore[misc]
496586
def __ne__(self, __value: object) -> bool: ...
587+
@overload
588+
def __rmul__(self: LiteralString, __value: SupportsIndex) -> LiteralString: ...
589+
@overload
497590
def __rmul__(self, __value: SupportsIndex) -> str: ... # type: ignore[misc]
498591
def __getnewargs__(self) -> tuple[str]: ...
499592

@@ -1637,11 +1730,11 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit
16371730
# Instead, we special-case the most common examples of this: bool and literal integers.
16381731
if sys.version_info >= (3, 8):
16391732
@overload
1640-
def sum(__iterable: Iterable[bool], start: int = 0) -> int: ... # type: ignore[misc]
1733+
def sum(__iterable: Iterable[bool | _LiteralInteger], start: int = 0) -> int: ... # type: ignore[misc]
16411734

16421735
else:
16431736
@overload
1644-
def sum(__iterable: Iterable[bool], __start: int = 0) -> int: ... # type: ignore[misc]
1737+
def sum(__iterable: Iterable[bool | _LiteralInteger], __start: int = 0) -> int: ... # type: ignore[misc]
16451738

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

mypy/typeshed/stdlib/codecs.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ class StreamRecoder(BinaryIO):
272272
def readlines(self, sizehint: int | None = None) -> list[bytes]: ...
273273
def __next__(self) -> bytes: ...
274274
def __iter__(self) -> Self: ...
275+
# Base class accepts more types than just bytes
275276
def write(self, data: bytes) -> None: ... # type: ignore[override]
276-
def writelines(self, list: Iterable[bytes]) -> None: ...
277+
def writelines(self, list: Iterable[bytes]) -> None: ... # type: ignore[override]
277278
def reset(self) -> None: ...
278279
def __getattr__(self, name: str) -> Any: ...
279280
def __enter__(self) -> Self: ...

mypy/typeshed/stdlib/ctypes/__init__.pyi

+5-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,11 @@ class Array(Generic[_CT], _CData):
271271
def _type_(self) -> type[_CT]: ...
272272
@_type_.setter
273273
def _type_(self, value: type[_CT]) -> None: ...
274-
raw: bytes # Note: only available if _CT == c_char
274+
# Note: only available if _CT == c_char
275+
@property
276+
def raw(self) -> bytes: ...
277+
@raw.setter
278+
def raw(self, value: ReadableBuffer) -> None: ...
275279
value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise
276280
# TODO These methods cannot be annotated correctly at the moment.
277281
# All of these "Any"s stand for the array's element type, but it's not possible to use _CT

mypy/typeshed/stdlib/functools.pyi

+25-6
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, NamedTuple, TypeVar, overload
6-
from typing_extensions import Literal, Self, TypeAlias, final
6+
from typing_extensions import Literal, ParamSpec, Self, TypeAlias, final
77

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

31-
_AnyCallable: TypeAlias = Callable[..., object]
32-
3331
_T = TypeVar("_T")
3432
_S = TypeVar("_S")
33+
_PWrapped = ParamSpec("_PWrapped")
34+
_RWrapped = TypeVar("_RWrapped")
35+
_PWrapper = ParamSpec("_PWrapper")
36+
_RWapper = TypeVar("_RWapper")
3537

3638
@overload
3739
def reduce(function: Callable[[_T, _S], _T], sequence: Iterable[_S], initial: _T) -> _T: ...
@@ -67,8 +69,25 @@ WRAPPER_ASSIGNMENTS: tuple[
6769
]
6870
WRAPPER_UPDATES: tuple[Literal["__dict__"]]
6971

70-
def update_wrapper(wrapper: _T, wrapped: _AnyCallable, assigned: Sequence[str] = ..., updated: Sequence[str] = ...) -> _T: ...
71-
def wraps(wrapped: _AnyCallable, assigned: Sequence[str] = ..., updated: Sequence[str] = ...) -> IdentityFunction: ...
72+
class _Wrapped(Generic[_PWrapped, _RWrapped, _PWrapper, _RWapper]):
73+
__wrapped__: Callable[_PWrapped, _RWrapped]
74+
def __call__(self, *args: _PWrapper.args, **kwargs: _PWrapper.kwargs) -> _RWapper: ...
75+
# as with ``Callable``, we'll assume that these attributes exist
76+
__name__: str
77+
__qualname__: str
78+
79+
class _Wrapper(Generic[_PWrapped, _RWrapped]):
80+
def __call__(self, f: Callable[_PWrapper, _RWapper]) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWapper]: ...
81+
82+
def update_wrapper(
83+
wrapper: Callable[_PWrapper, _RWapper],
84+
wrapped: Callable[_PWrapped, _RWrapped],
85+
assigned: Sequence[str] = ...,
86+
updated: Sequence[str] = ...,
87+
) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWapper]: ...
88+
def wraps(
89+
wrapped: Callable[_PWrapped, _RWrapped], assigned: Sequence[str] = ..., updated: Sequence[str] = ...
90+
) -> _Wrapper[_PWrapped, _RWrapped]: ...
7291
def total_ordering(cls: type[_T]) -> type[_T]: ...
7392
def cmp_to_key(mycmp: Callable[[_T, _T], int]) -> Callable[[_T], SupportsAllComparisons]: ...
7493

mypy/typeshed/stdlib/http/client.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class HTTPMessage(email.message.Message):
101101

102102
def parse_headers(fp: io.BufferedIOBase, _class: Callable[[], email.message.Message] = ...) -> HTTPMessage: ...
103103

104-
class HTTPResponse(io.BufferedIOBase, BinaryIO):
104+
class HTTPResponse(io.BufferedIOBase, BinaryIO): # type: ignore[misc] # incompatible method definitions in the base classes
105105
msg: HTTPMessage
106106
headers: HTTPMessage
107107
version: int

mypy/typeshed/stdlib/importlib/abc.pyi

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ast
12
import sys
23
import types
34
from _typeshed import (
@@ -7,6 +8,7 @@ from _typeshed import (
78
OpenBinaryModeWriting,
89
OpenTextMode,
910
ReadableBuffer,
11+
StrPath,
1012
)
1113
from abc import ABCMeta, abstractmethod
1214
from collections.abc import Iterator, Mapping, Sequence
@@ -52,7 +54,9 @@ class InspectLoader(Loader):
5254
def get_source(self, fullname: str) -> str | None: ...
5355
def exec_module(self, module: types.ModuleType) -> None: ...
5456
@staticmethod
55-
def source_to_code(data: ReadableBuffer | str, path: str = "<string>") -> types.CodeType: ...
57+
def source_to_code(
58+
data: ReadableBuffer | str | _ast.Module | _ast.Expression | _ast.Interactive, path: ReadableBuffer | StrPath = "<string>"
59+
) -> types.CodeType: ...
5660

5761
class ExecutionLoader(InspectLoader):
5862
@abstractmethod

0 commit comments

Comments
 (0)