Skip to content

Commit 7fbf4de

Browse files
authored
Sync typeshed (#12766)
Source commit: python/typeshed@a27f15e Co-authored-by: hauntsaninja <>
1 parent 50a653e commit 7fbf4de

36 files changed

+1263
-182
lines changed

mypy/typeshed/stdlib/_ast.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class FormattedValue(expr):
319319
if sys.version_info >= (3, 10):
320320
__match_args__ = ("value", "conversion", "format_spec")
321321
value: expr
322-
conversion: int | None
322+
conversion: int
323323
format_spec: expr | None
324324

325325
class JoinedStr(expr):

mypy/typeshed/stdlib/_csv.pyi

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from _typeshed import SupportsWrite
12
from collections.abc import Iterable, Iterator
2-
from typing import Any, Protocol, Union
3+
from typing import Any, Union
34
from typing_extensions import Literal, TypeAlias
45

56
__version__: str
@@ -9,6 +10,10 @@ QUOTE_MINIMAL: Literal[0]
910
QUOTE_NONE: Literal[3]
1011
QUOTE_NONNUMERIC: Literal[2]
1112

13+
# Ideally this would be `QUOTE_ALL | QUOTE_MINIMAL | QUOTE_NONE | QUOTE_NONNUMERIC`
14+
# However, using literals in situations like these can cause false-positives (see #7258)
15+
_QuotingType: TypeAlias = int
16+
1217
class Error(Exception): ...
1318

1419
class Dialect:
@@ -18,28 +23,63 @@ class Dialect:
1823
doublequote: bool
1924
skipinitialspace: bool
2025
lineterminator: str
21-
quoting: int
22-
strict: int
26+
quoting: _QuotingType
27+
strict: bool
2328
def __init__(self) -> None: ...
2429

2530
_DialectLike: TypeAlias = Union[str, Dialect, type[Dialect]]
2631

2732
class _reader(Iterator[list[str]]):
28-
dialect: Dialect
33+
@property
34+
def dialect(self) -> Dialect: ...
2935
line_num: int
3036
def __next__(self) -> list[str]: ...
3137

3238
class _writer:
33-
dialect: Dialect
39+
@property
40+
def dialect(self) -> Dialect: ...
3441
def writerow(self, row: Iterable[Any]) -> Any: ...
3542
def writerows(self, rows: Iterable[Iterable[Any]]) -> None: ...
3643

37-
class _Writer(Protocol):
38-
def write(self, __s: str) -> object: ...
39-
40-
def writer(csvfile: _Writer, dialect: _DialectLike = ..., **fmtparams: Any) -> _writer: ...
41-
def reader(csvfile: Iterable[str], dialect: _DialectLike = ..., **fmtparams: Any) -> _reader: ...
42-
def register_dialect(name: str, dialect: Any = ..., **fmtparams: Any) -> None: ...
44+
def writer(
45+
csvfile: SupportsWrite[str],
46+
dialect: _DialectLike = ...,
47+
*,
48+
delimiter: str = ...,
49+
quotechar: str | None = ...,
50+
escapechar: str | None = ...,
51+
doublequote: bool = ...,
52+
skipinitialspace: bool = ...,
53+
lineterminator: str = ...,
54+
quoting: _QuotingType = ...,
55+
strict: bool = ...,
56+
) -> _writer: ...
57+
def reader(
58+
csvfile: Iterable[str],
59+
dialect: _DialectLike = ...,
60+
*,
61+
delimiter: str = ...,
62+
quotechar: str | None = ...,
63+
escapechar: str | None = ...,
64+
doublequote: bool = ...,
65+
skipinitialspace: bool = ...,
66+
lineterminator: str = ...,
67+
quoting: _QuotingType = ...,
68+
strict: bool = ...,
69+
) -> _reader: ...
70+
def register_dialect(
71+
name: str,
72+
dialect: Any = ...,
73+
*,
74+
delimiter: str = ...,
75+
quotechar: str | None = ...,
76+
escapechar: str | None = ...,
77+
doublequote: bool = ...,
78+
skipinitialspace: bool = ...,
79+
lineterminator: str = ...,
80+
quoting: _QuotingType = ...,
81+
strict: bool = ...,
82+
) -> None: ...
4383
def unregister_dialect(name: str) -> None: ...
4484
def get_dialect(name: str) -> Dialect: ...
4585
def list_dialects() -> list[str]: ...

mypy/typeshed/stdlib/_decimal.pyi

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,23 @@ class FloatOperation(DecimalException, TypeError): ...
5252

5353
def setcontext(__context: Context) -> None: ...
5454
def getcontext() -> Context: ...
55-
def localcontext(ctx: Context | None = ...) -> _ContextManager: ...
55+
56+
if sys.version_info >= (3, 11):
57+
def localcontext(
58+
ctx: Context | None = ...,
59+
*,
60+
prec: int | None = ...,
61+
rounding: str | None = ...,
62+
Emin: int | None = ...,
63+
Emax: int | None = ...,
64+
capitals: int | None = ...,
65+
clamp: int | None = ...,
66+
traps: dict[_TrapType, bool] | None = ...,
67+
flags: dict[_TrapType, bool] | None = ...,
68+
) -> _ContextManager: ...
69+
70+
else:
71+
def localcontext(ctx: Context | None = ...) -> _ContextManager: ...
5672

5773
class Decimal:
5874
def __new__(cls: type[Self], value: _DecimalNew = ..., context: Context | None = ...) -> Self: ...

mypy/typeshed/stdlib/_socket.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ SO_SNDLOWAT: int
256256
SO_SNDTIMEO: int
257257
SO_TYPE: int
258258
SO_USELOOPBACK: int
259+
if sys.platform == "linux" and sys.version_info >= (3, 11):
260+
SO_INCOMING_CPU: int
259261
TCP_CORK: int
260262
TCP_DEFER_ACCEPT: int
261263
TCP_FASTOPEN: int
@@ -271,6 +273,8 @@ TCP_SYNCNT: int
271273
TCP_WINDOW_CLAMP: int
272274
if sys.version_info >= (3, 7):
273275
TCP_NOTSENT_LOWAT: int
276+
if sys.version_info >= (3, 11) and sys.platform == "darwin":
277+
TCP_CONNECTION_INFO: int
274278

275279
# Specifically-documented constants
276280

mypy/typeshed/stdlib/_typeshed/__init__.pyi

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import array
66
import ctypes
77
import mmap
8+
import pickle
89
import sys
910
from collections.abc import Awaitable, Container, Iterable, Set as AbstractSet
1011
from os import PathLike
@@ -63,12 +64,27 @@ class SupportsAllComparisons(SupportsDunderLT, SupportsDunderGT, SupportsDunderL
6364
SupportsRichComparison: TypeAlias = SupportsDunderLT | SupportsDunderGT
6465
SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison) # noqa: Y001
6566

67+
# Dunder protocols
68+
69+
class SupportsAdd(Protocol):
70+
def __add__(self, __x: Any) -> Any: ...
71+
6672
class SupportsDivMod(Protocol[_T_contra, _T_co]):
6773
def __divmod__(self, __other: _T_contra) -> _T_co: ...
6874

6975
class SupportsRDivMod(Protocol[_T_contra, _T_co]):
7076
def __rdivmod__(self, __other: _T_contra) -> _T_co: ...
7177

78+
# This protocol is generic over the iterator type, while Iterable is
79+
# generic over the type that is iterated over.
80+
class SupportsIter(Protocol[_T_co]):
81+
def __iter__(self) -> _T_co: ...
82+
83+
# This protocol is generic over the iterator type, while AsyncIterable is
84+
# generic over the type that is iterated over.
85+
class SupportsAiter(Protocol[_T_co]):
86+
def __aiter__(self) -> _T_co: ...
87+
7288
class SupportsLenAndGetItem(Protocol[_T_co]):
7389
def __len__(self) -> int: ...
7490
def __getitem__(self, __k: int) -> _T_co: ...
@@ -194,8 +210,13 @@ class SupportsWrite(Protocol[_T_contra]):
194210
ReadOnlyBuffer: TypeAlias = bytes # stable
195211
# Anything that implements the read-write buffer interface.
196212
# The buffer interface is defined purely on the C level, so we cannot define a normal Protocol
197-
# for it. Instead we have to list the most common stdlib buffer classes in a Union.
198-
WriteableBuffer: TypeAlias = bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData # stable
213+
# for it (until PEP 688 is implemented). Instead we have to list the most common stdlib buffer classes in a Union.
214+
if sys.version_info >= (3, 8):
215+
WriteableBuffer: TypeAlias = (
216+
bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData | pickle.PickleBuffer
217+
) # stable
218+
else:
219+
WriteableBuffer: TypeAlias = bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData # stable
199220
# Same as _WriteableBuffer, but also includes read-only buffer types (like bytes).
200221
ReadableBuffer: TypeAlias = ReadOnlyBuffer | WriteableBuffer # stable
201222

mypy/typeshed/stdlib/asyncio/transports.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ class ReadTransport(BaseTransport):
2828
class WriteTransport(BaseTransport):
2929
def set_write_buffer_limits(self, high: int | None = ..., low: int | None = ...) -> None: ...
3030
def get_write_buffer_size(self) -> int: ...
31-
if sys.version_info >= (3, 9):
32-
def get_write_buffer_limits(self) -> tuple[int, int]: ...
33-
31+
def get_write_buffer_limits(self) -> tuple[int, int]: ...
3432
def write(self, data: Any) -> None: ...
3533
def writelines(self, list_of_data: list[Any]) -> None: ...
3634
def write_eof(self) -> None: ...
@@ -53,4 +51,6 @@ class SubprocessTransport(BaseTransport):
5351

5452
class _FlowControlMixin(Transport):
5553
def __init__(self, extra: Mapping[Any, Any] | None = ..., loop: AbstractEventLoop | None = ...) -> None: ...
54+
def set_write_buffer_limits(self, high: int | None = ..., low: int | None = ...) -> None: ...
55+
def get_write_buffer_size(self) -> int: ...
5656
def get_write_buffer_limits(self) -> tuple[int, int]: ...

mypy/typeshed/stdlib/builtins.pyi

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ from _typeshed import (
1111
ReadableBuffer,
1212
Self,
1313
StrOrBytesPath,
14+
SupportsAdd,
15+
SupportsAiter,
1416
SupportsAnext,
1517
SupportsDivMod,
18+
SupportsIter,
1619
SupportsKeysAndGetItem,
1720
SupportsLenAndGetItem,
1821
SupportsNext,
@@ -71,12 +74,6 @@ _SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant
7174
_AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any])
7275
_AwaitableT_co = TypeVar("_AwaitableT_co", bound=Awaitable[Any], covariant=True)
7376

74-
class _SupportsIter(Protocol[_T_co]):
75-
def __iter__(self) -> _T_co: ...
76-
77-
class _SupportsAiter(Protocol[_T_co]):
78-
def __aiter__(self) -> _T_co: ...
79-
8077
class object:
8178
__doc__: str | None
8279
__dict__: dict[str, Any]
@@ -122,7 +119,8 @@ class staticmethod(Generic[_R_co]):
122119
if sys.version_info >= (3, 10):
123120
__name__: str
124121
__qualname__: str
125-
__wrapped__: Callable[..., _R_co]
122+
@property
123+
def __wrapped__(self) -> Callable[..., _R_co]: ...
126124
def __call__(self, *args: Any, **kwargs: Any) -> _R_co: ...
127125

128126
class classmethod(Generic[_R_co]):
@@ -135,7 +133,8 @@ class classmethod(Generic[_R_co]):
135133
if sys.version_info >= (3, 10):
136134
__name__: str
137135
__qualname__: str
138-
__wrapped__: Callable[..., _R_co]
136+
@property
137+
def __wrapped__(self) -> Callable[..., _R_co]: ...
139138

140139
class type:
141140
@property
@@ -251,11 +250,9 @@ class int:
251250
def __rmod__(self, __x: int) -> int: ...
252251
def __rdivmod__(self, __x: int) -> tuple[int, int]: ...
253252
@overload
254-
def __pow__(self, __x: int, __modulo: Literal[0]) -> NoReturn: ...
255-
@overload
256-
def __pow__(self, __x: int, __modulo: int) -> int: ...
253+
def __pow__(self, __x: Literal[0]) -> Literal[1]: ...
257254
@overload
258-
def __pow__(self, __x: Literal[0], __modulo: None = ...) -> Literal[1]: ...
255+
def __pow__(self, __x: Literal[0], __modulo: None) -> Literal[1]: ...
259256
@overload
260257
def __pow__(self, __x: _PositiveInteger, __modulo: None = ...) -> int: ...
261258
@overload
@@ -264,6 +261,10 @@ class int:
264261
# return type must be Any as `int | float` causes too many false-positive errors
265262
@overload
266263
def __pow__(self, __x: int, __modulo: None = ...) -> Any: ...
264+
@overload
265+
def __pow__(self, __x: int, __modulo: Literal[0]) -> NoReturn: ...
266+
@overload
267+
def __pow__(self, __x: int, __modulo: int) -> int: ...
267268
def __rpow__(self, __x: int, __mod: int | None = ...) -> Any: ...
268269
def __and__(self, __n: int) -> int: ...
269270
def __or__(self, __n: int) -> int: ...
@@ -328,7 +329,12 @@ class float:
328329
def __rtruediv__(self, __x: float) -> float: ...
329330
def __rmod__(self, __x: float) -> float: ...
330331
def __rdivmod__(self, __x: float) -> tuple[float, float]: ...
331-
# Returns complex if the argument is negative.
332+
@overload
333+
def __rpow__(self, __x: _PositiveInteger, __modulo: None = ...) -> float: ...
334+
@overload
335+
def __rpow__(self, __x: _NegativeInteger, __mod: None = ...) -> complex: ...
336+
# Returning `complex` for the general case gives too many false-positive errors.
337+
@overload
332338
def __rpow__(self, __x: float, __mod: None = ...) -> Any: ...
333339
def __getnewargs__(self) -> tuple[float]: ...
334340
def __trunc__(self) -> int: ...
@@ -1092,7 +1098,7 @@ class _PathLike(Protocol[_AnyStr_co]):
10921098
def __fspath__(self) -> _AnyStr_co: ...
10931099

10941100
if sys.version_info >= (3, 10):
1095-
def aiter(__async_iterable: _SupportsAiter[_SupportsAnextT]) -> _SupportsAnextT: ...
1101+
def aiter(__async_iterable: SupportsAiter[_SupportsAnextT]) -> _SupportsAnextT: ...
10961102

10971103
class _SupportsSynchronousAnext(Protocol[_AwaitableT_co]):
10981104
def __anext__(self) -> _AwaitableT_co: ...
@@ -1144,9 +1150,22 @@ def eval(
11441150
) -> Any: ...
11451151

11461152
# Comment above regarding `eval` applies to `exec` as well
1147-
def exec(
1148-
__source: str | ReadableBuffer | CodeType, __globals: dict[str, Any] | None = ..., __locals: Mapping[str, object] | None = ...
1149-
) -> None: ...
1153+
if sys.version_info >= (3, 11):
1154+
def exec(
1155+
__source: str | ReadableBuffer | CodeType,
1156+
__globals: dict[str, Any] | None = ...,
1157+
__locals: Mapping[str, object] | None = ...,
1158+
*,
1159+
closure: tuple[_Cell, ...] | None = ...,
1160+
) -> None: ...
1161+
1162+
else:
1163+
def exec(
1164+
__source: str | ReadableBuffer | CodeType,
1165+
__globals: dict[str, Any] | None = ...,
1166+
__locals: Mapping[str, object] | None = ...,
1167+
) -> None: ...
1168+
11501169
def exit(code: object = ...) -> NoReturn: ...
11511170

11521171
class filter(Iterator[_T], Generic[_T]):
@@ -1183,8 +1202,14 @@ def help(request: object = ...) -> None: ...
11831202
def hex(__number: int | SupportsIndex) -> str: ...
11841203
def id(__obj: object) -> int: ...
11851204
def input(__prompt: object = ...) -> str: ...
1205+
1206+
class _GetItemIterable(Protocol[_T_co]):
1207+
def __getitem__(self, __i: int) -> _T_co: ...
1208+
1209+
@overload
1210+
def iter(__iterable: SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
11861211
@overload
1187-
def iter(__iterable: _SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
1212+
def iter(__iterable: _GetItemIterable[_T]) -> Iterator[_T]: ...
11881213
@overload
11891214
def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]: ...
11901215
@overload
@@ -1423,6 +1448,10 @@ if sys.version_info >= (3, 8):
14231448
@overload
14241449
def pow(base: int, exp: int, mod: None = ...) -> Any: ...
14251450
@overload
1451+
def pow(base: _PositiveInteger, exp: float, mod: None = ...) -> float: ...
1452+
@overload
1453+
def pow(base: _NegativeInteger, exp: float, mod: None = ...) -> complex: ...
1454+
@overload
14261455
def pow(base: float, exp: int, mod: None = ...) -> float: ...
14271456
# float base & float exp could return float or complex
14281457
# return type must be Any (same as complex base, complex exp),
@@ -1456,6 +1485,10 @@ else:
14561485
@overload
14571486
def pow(__base: int, __exp: int, __mod: None = ...) -> Any: ...
14581487
@overload
1488+
def pow(__base: _PositiveInteger, __exp: float, __mod: None = ...) -> float: ...
1489+
@overload
1490+
def pow(__base: _NegativeInteger, __exp: float, __mod: None = ...) -> complex: ...
1491+
@overload
14591492
def pow(__base: float, __exp: int, __mod: None = ...) -> float: ...
14601493
@overload
14611494
def pow(__base: float, __exp: complex | _SupportsSomeKindOfPow, __mod: None = ...) -> Any: ...
@@ -1501,11 +1534,8 @@ def sorted(
15011534
@overload
15021535
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> list[_T]: ...
15031536

1504-
class _SupportsSum(Protocol):
1505-
def __add__(self, __x: Any) -> Any: ...
1506-
1507-
_SumT = TypeVar("_SumT", bound=_SupportsSum)
1508-
_SumS = TypeVar("_SumS", bound=_SupportsSum)
1537+
_SumT = TypeVar("_SumT", bound=SupportsAdd)
1538+
_SumS = TypeVar("_SumS", bound=SupportsAdd)
15091539

15101540
@overload
15111541
def sum(__iterable: Iterable[_SumT]) -> _SumT | Literal[0]: ...

0 commit comments

Comments
 (0)