Skip to content

Commit

Permalink
Sync typeshed (#12766)
Browse files Browse the repository at this point in the history
Source commit:
python/typeshed@a27f15e

Co-authored-by: hauntsaninja <>
  • Loading branch information
hauntsaninja committed May 13, 2022
1 parent 50a653e commit 7fbf4de
Show file tree
Hide file tree
Showing 36 changed files with 1,263 additions and 182 deletions.
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/_ast.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class FormattedValue(expr):
if sys.version_info >= (3, 10):
__match_args__ = ("value", "conversion", "format_spec")
value: expr
conversion: int | None
conversion: int
format_spec: expr | None

class JoinedStr(expr):
Expand Down
62 changes: 51 additions & 11 deletions mypy/typeshed/stdlib/_csv.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from _typeshed import SupportsWrite
from collections.abc import Iterable, Iterator
from typing import Any, Protocol, Union
from typing import Any, Union
from typing_extensions import Literal, TypeAlias

__version__: str
Expand All @@ -9,6 +10,10 @@ QUOTE_MINIMAL: Literal[0]
QUOTE_NONE: Literal[3]
QUOTE_NONNUMERIC: Literal[2]

# Ideally this would be `QUOTE_ALL | QUOTE_MINIMAL | QUOTE_NONE | QUOTE_NONNUMERIC`
# However, using literals in situations like these can cause false-positives (see #7258)
_QuotingType: TypeAlias = int

class Error(Exception): ...

class Dialect:
Expand All @@ -18,28 +23,63 @@ class Dialect:
doublequote: bool
skipinitialspace: bool
lineterminator: str
quoting: int
strict: int
quoting: _QuotingType
strict: bool
def __init__(self) -> None: ...

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

class _reader(Iterator[list[str]]):
dialect: Dialect
@property
def dialect(self) -> Dialect: ...
line_num: int
def __next__(self) -> list[str]: ...

class _writer:
dialect: Dialect
@property
def dialect(self) -> Dialect: ...
def writerow(self, row: Iterable[Any]) -> Any: ...
def writerows(self, rows: Iterable[Iterable[Any]]) -> None: ...

class _Writer(Protocol):
def write(self, __s: str) -> object: ...

def writer(csvfile: _Writer, dialect: _DialectLike = ..., **fmtparams: Any) -> _writer: ...
def reader(csvfile: Iterable[str], dialect: _DialectLike = ..., **fmtparams: Any) -> _reader: ...
def register_dialect(name: str, dialect: Any = ..., **fmtparams: Any) -> None: ...
def writer(
csvfile: SupportsWrite[str],
dialect: _DialectLike = ...,
*,
delimiter: str = ...,
quotechar: str | None = ...,
escapechar: str | None = ...,
doublequote: bool = ...,
skipinitialspace: bool = ...,
lineterminator: str = ...,
quoting: _QuotingType = ...,
strict: bool = ...,
) -> _writer: ...
def reader(
csvfile: Iterable[str],
dialect: _DialectLike = ...,
*,
delimiter: str = ...,
quotechar: str | None = ...,
escapechar: str | None = ...,
doublequote: bool = ...,
skipinitialspace: bool = ...,
lineterminator: str = ...,
quoting: _QuotingType = ...,
strict: bool = ...,
) -> _reader: ...
def register_dialect(
name: str,
dialect: Any = ...,
*,
delimiter: str = ...,
quotechar: str | None = ...,
escapechar: str | None = ...,
doublequote: bool = ...,
skipinitialspace: bool = ...,
lineterminator: str = ...,
quoting: _QuotingType = ...,
strict: bool = ...,
) -> None: ...
def unregister_dialect(name: str) -> None: ...
def get_dialect(name: str) -> Dialect: ...
def list_dialects() -> list[str]: ...
Expand Down
18 changes: 17 additions & 1 deletion mypy/typeshed/stdlib/_decimal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,23 @@ class FloatOperation(DecimalException, TypeError): ...

def setcontext(__context: Context) -> None: ...
def getcontext() -> Context: ...
def localcontext(ctx: Context | None = ...) -> _ContextManager: ...

if sys.version_info >= (3, 11):
def localcontext(
ctx: Context | None = ...,
*,
prec: int | None = ...,
rounding: str | None = ...,
Emin: int | None = ...,
Emax: int | None = ...,
capitals: int | None = ...,
clamp: int | None = ...,
traps: dict[_TrapType, bool] | None = ...,
flags: dict[_TrapType, bool] | None = ...,
) -> _ContextManager: ...

else:
def localcontext(ctx: Context | None = ...) -> _ContextManager: ...

class Decimal:
def __new__(cls: type[Self], value: _DecimalNew = ..., context: Context | None = ...) -> Self: ...
Expand Down
4 changes: 4 additions & 0 deletions mypy/typeshed/stdlib/_socket.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ SO_SNDLOWAT: int
SO_SNDTIMEO: int
SO_TYPE: int
SO_USELOOPBACK: int
if sys.platform == "linux" and sys.version_info >= (3, 11):
SO_INCOMING_CPU: int
TCP_CORK: int
TCP_DEFER_ACCEPT: int
TCP_FASTOPEN: int
Expand All @@ -271,6 +273,8 @@ TCP_SYNCNT: int
TCP_WINDOW_CLAMP: int
if sys.version_info >= (3, 7):
TCP_NOTSENT_LOWAT: int
if sys.version_info >= (3, 11) and sys.platform == "darwin":
TCP_CONNECTION_INFO: int

# Specifically-documented constants

Expand Down
25 changes: 23 additions & 2 deletions mypy/typeshed/stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import array
import ctypes
import mmap
import pickle
import sys
from collections.abc import Awaitable, Container, Iterable, Set as AbstractSet
from os import PathLike
Expand Down Expand Up @@ -63,12 +64,27 @@ class SupportsAllComparisons(SupportsDunderLT, SupportsDunderGT, SupportsDunderL
SupportsRichComparison: TypeAlias = SupportsDunderLT | SupportsDunderGT
SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison) # noqa: Y001

# Dunder protocols

class SupportsAdd(Protocol):
def __add__(self, __x: Any) -> Any: ...

class SupportsDivMod(Protocol[_T_contra, _T_co]):
def __divmod__(self, __other: _T_contra) -> _T_co: ...

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

# This protocol is generic over the iterator type, while Iterable is
# generic over the type that is iterated over.
class SupportsIter(Protocol[_T_co]):
def __iter__(self) -> _T_co: ...

# This protocol is generic over the iterator type, while AsyncIterable is
# generic over the type that is iterated over.
class SupportsAiter(Protocol[_T_co]):
def __aiter__(self) -> _T_co: ...

class SupportsLenAndGetItem(Protocol[_T_co]):
def __len__(self) -> int: ...
def __getitem__(self, __k: int) -> _T_co: ...
Expand Down Expand Up @@ -194,8 +210,13 @@ class SupportsWrite(Protocol[_T_contra]):
ReadOnlyBuffer: TypeAlias = bytes # stable
# Anything that implements the read-write buffer interface.
# The buffer interface is defined purely on the C level, so we cannot define a normal Protocol
# for it. Instead we have to list the most common stdlib buffer classes in a Union.
WriteableBuffer: TypeAlias = bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData # stable
# for it (until PEP 688 is implemented). Instead we have to list the most common stdlib buffer classes in a Union.
if sys.version_info >= (3, 8):
WriteableBuffer: TypeAlias = (
bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData | pickle.PickleBuffer
) # stable
else:
WriteableBuffer: TypeAlias = bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData # stable
# Same as _WriteableBuffer, but also includes read-only buffer types (like bytes).
ReadableBuffer: TypeAlias = ReadOnlyBuffer | WriteableBuffer # stable

Expand Down
6 changes: 3 additions & 3 deletions mypy/typeshed/stdlib/asyncio/transports.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class ReadTransport(BaseTransport):
class WriteTransport(BaseTransport):
def set_write_buffer_limits(self, high: int | None = ..., low: int | None = ...) -> None: ...
def get_write_buffer_size(self) -> int: ...
if sys.version_info >= (3, 9):
def get_write_buffer_limits(self) -> tuple[int, int]: ...

def get_write_buffer_limits(self) -> tuple[int, int]: ...
def write(self, data: Any) -> None: ...
def writelines(self, list_of_data: list[Any]) -> None: ...
def write_eof(self) -> None: ...
Expand All @@ -53,4 +51,6 @@ class SubprocessTransport(BaseTransport):

class _FlowControlMixin(Transport):
def __init__(self, extra: Mapping[Any, Any] | None = ..., loop: AbstractEventLoop | None = ...) -> None: ...
def set_write_buffer_limits(self, high: int | None = ..., low: int | None = ...) -> None: ...
def get_write_buffer_size(self) -> int: ...
def get_write_buffer_limits(self) -> tuple[int, int]: ...
76 changes: 53 additions & 23 deletions mypy/typeshed/stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ from _typeshed import (
ReadableBuffer,
Self,
StrOrBytesPath,
SupportsAdd,
SupportsAiter,
SupportsAnext,
SupportsDivMod,
SupportsIter,
SupportsKeysAndGetItem,
SupportsLenAndGetItem,
SupportsNext,
Expand Down Expand Up @@ -71,12 +74,6 @@ _SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant
_AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any])
_AwaitableT_co = TypeVar("_AwaitableT_co", bound=Awaitable[Any], covariant=True)

class _SupportsIter(Protocol[_T_co]):
def __iter__(self) -> _T_co: ...

class _SupportsAiter(Protocol[_T_co]):
def __aiter__(self) -> _T_co: ...

class object:
__doc__: str | None
__dict__: dict[str, Any]
Expand Down Expand Up @@ -122,7 +119,8 @@ class staticmethod(Generic[_R_co]):
if sys.version_info >= (3, 10):
__name__: str
__qualname__: str
__wrapped__: Callable[..., _R_co]
@property
def __wrapped__(self) -> Callable[..., _R_co]: ...
def __call__(self, *args: Any, **kwargs: Any) -> _R_co: ...

class classmethod(Generic[_R_co]):
Expand All @@ -135,7 +133,8 @@ class classmethod(Generic[_R_co]):
if sys.version_info >= (3, 10):
__name__: str
__qualname__: str
__wrapped__: Callable[..., _R_co]
@property
def __wrapped__(self) -> Callable[..., _R_co]: ...

class type:
@property
Expand Down Expand Up @@ -251,11 +250,9 @@ class int:
def __rmod__(self, __x: int) -> int: ...
def __rdivmod__(self, __x: int) -> tuple[int, int]: ...
@overload
def __pow__(self, __x: int, __modulo: Literal[0]) -> NoReturn: ...
@overload
def __pow__(self, __x: int, __modulo: int) -> int: ...
def __pow__(self, __x: Literal[0]) -> Literal[1]: ...
@overload
def __pow__(self, __x: Literal[0], __modulo: None = ...) -> Literal[1]: ...
def __pow__(self, __x: Literal[0], __modulo: None) -> Literal[1]: ...
@overload
def __pow__(self, __x: _PositiveInteger, __modulo: None = ...) -> int: ...
@overload
Expand All @@ -264,6 +261,10 @@ class int:
# return type must be Any as `int | float` causes too many false-positive errors
@overload
def __pow__(self, __x: int, __modulo: None = ...) -> Any: ...
@overload
def __pow__(self, __x: int, __modulo: Literal[0]) -> NoReturn: ...
@overload
def __pow__(self, __x: int, __modulo: int) -> int: ...
def __rpow__(self, __x: int, __mod: int | None = ...) -> Any: ...
def __and__(self, __n: int) -> int: ...
def __or__(self, __n: int) -> int: ...
Expand Down Expand Up @@ -328,7 +329,12 @@ class float:
def __rtruediv__(self, __x: float) -> float: ...
def __rmod__(self, __x: float) -> float: ...
def __rdivmod__(self, __x: float) -> tuple[float, float]: ...
# Returns complex if the argument is negative.
@overload
def __rpow__(self, __x: _PositiveInteger, __modulo: None = ...) -> float: ...
@overload
def __rpow__(self, __x: _NegativeInteger, __mod: None = ...) -> complex: ...
# Returning `complex` for the general case gives too many false-positive errors.
@overload
def __rpow__(self, __x: float, __mod: None = ...) -> Any: ...
def __getnewargs__(self) -> tuple[float]: ...
def __trunc__(self) -> int: ...
Expand Down Expand Up @@ -1092,7 +1098,7 @@ class _PathLike(Protocol[_AnyStr_co]):
def __fspath__(self) -> _AnyStr_co: ...

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

class _SupportsSynchronousAnext(Protocol[_AwaitableT_co]):
def __anext__(self) -> _AwaitableT_co: ...
Expand Down Expand Up @@ -1144,9 +1150,22 @@ def eval(
) -> Any: ...

# Comment above regarding `eval` applies to `exec` as well
def exec(
__source: str | ReadableBuffer | CodeType, __globals: dict[str, Any] | None = ..., __locals: Mapping[str, object] | None = ...
) -> None: ...
if sys.version_info >= (3, 11):
def exec(
__source: str | ReadableBuffer | CodeType,
__globals: dict[str, Any] | None = ...,
__locals: Mapping[str, object] | None = ...,
*,
closure: tuple[_Cell, ...] | None = ...,
) -> None: ...

else:
def exec(
__source: str | ReadableBuffer | CodeType,
__globals: dict[str, Any] | None = ...,
__locals: Mapping[str, object] | None = ...,
) -> None: ...

def exit(code: object = ...) -> NoReturn: ...

class filter(Iterator[_T], Generic[_T]):
Expand Down Expand Up @@ -1183,8 +1202,14 @@ def help(request: object = ...) -> None: ...
def hex(__number: int | SupportsIndex) -> str: ...
def id(__obj: object) -> int: ...
def input(__prompt: object = ...) -> str: ...

class _GetItemIterable(Protocol[_T_co]):
def __getitem__(self, __i: int) -> _T_co: ...

@overload
def iter(__iterable: SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
@overload
def iter(__iterable: _SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
def iter(__iterable: _GetItemIterable[_T]) -> Iterator[_T]: ...
@overload
def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]: ...
@overload
Expand Down Expand Up @@ -1423,6 +1448,10 @@ if sys.version_info >= (3, 8):
@overload
def pow(base: int, exp: int, mod: None = ...) -> Any: ...
@overload
def pow(base: _PositiveInteger, exp: float, mod: None = ...) -> float: ...
@overload
def pow(base: _NegativeInteger, exp: float, mod: None = ...) -> complex: ...
@overload
def pow(base: float, exp: int, mod: None = ...) -> float: ...
# float base & float exp could return float or complex
# return type must be Any (same as complex base, complex exp),
Expand Down Expand Up @@ -1456,6 +1485,10 @@ else:
@overload
def pow(__base: int, __exp: int, __mod: None = ...) -> Any: ...
@overload
def pow(__base: _PositiveInteger, __exp: float, __mod: None = ...) -> float: ...
@overload
def pow(__base: _NegativeInteger, __exp: float, __mod: None = ...) -> complex: ...
@overload
def pow(__base: float, __exp: int, __mod: None = ...) -> float: ...
@overload
def pow(__base: float, __exp: complex | _SupportsSomeKindOfPow, __mod: None = ...) -> Any: ...
Expand Down Expand Up @@ -1501,11 +1534,8 @@ def sorted(
@overload
def sorted(__iterable: Iterable[_T], *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> list[_T]: ...

class _SupportsSum(Protocol):
def __add__(self, __x: Any) -> Any: ...

_SumT = TypeVar("_SumT", bound=_SupportsSum)
_SumS = TypeVar("_SumS", bound=_SupportsSum)
_SumT = TypeVar("_SumT", bound=SupportsAdd)
_SumS = TypeVar("_SumS", bound=SupportsAdd)

@overload
def sum(__iterable: Iterable[_SumT]) -> _SumT | Literal[0]: ...
Expand Down
Loading

0 comments on commit 7fbf4de

Please sign in to comment.