Skip to content

Commit

Permalink
Sync typeshed
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored and JelleZijlstra committed May 1, 2023
1 parent dbb72bb commit 8b356ff
Show file tree
Hide file tree
Showing 81 changed files with 1,300 additions and 255 deletions.
105 changes: 103 additions & 2 deletions mypy/typeshed/stdlib/_ctypes.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import sys
from ctypes import _CArgObject, _PointerLike
from typing_extensions import TypeAlias
from _typeshed import ReadableBuffer, WriteableBuffer
from abc import abstractmethod
from collections.abc import Iterable, Iterator, Mapping, Sequence
from ctypes import CDLL, _CArgObject, _PointerLike
from typing import Any, Generic, TypeVar, overload
from typing_extensions import Self, TypeAlias

if sys.version_info >= (3, 9):
from types import GenericAlias

_T = TypeVar("_T")
_CT = TypeVar("_CT", bound=_CData)

FUNCFLAG_CDECL: int
FUNCFLAG_PYTHONAPI: int
Expand All @@ -27,3 +37,94 @@ if sys.platform == "win32":

FUNCFLAG_HRESULT: int
FUNCFLAG_STDCALL: int

class _CDataMeta(type):
# By default mypy complains about the following two methods, because strictly speaking cls
# might not be a Type[_CT]. However this can never actually happen, because the only class that
# uses _CDataMeta as its metaclass is _CData. So it's safe to ignore the errors here.
def __mul__(cls: type[_CT], other: int) -> type[Array[_CT]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
def __rmul__(cls: type[_CT], other: int) -> type[Array[_CT]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]

class _CData(metaclass=_CDataMeta):
_b_base_: int
_b_needsfree_: bool
_objects: Mapping[Any, int] | None
@classmethod
def from_buffer(cls, source: WriteableBuffer, offset: int = ...) -> Self: ...
@classmethod
def from_buffer_copy(cls, source: ReadableBuffer, offset: int = ...) -> Self: ...
@classmethod
def from_address(cls, address: int) -> Self: ...
@classmethod
def from_param(cls, obj: Any) -> Self | _CArgObject: ...
@classmethod
def in_dll(cls, library: CDLL, name: str) -> Self: ...

class _SimpleCData(Generic[_T], _CData):
value: _T
# The TypeVar can be unsolved here,
# but we can't use overloads without creating many, many mypy false-positive errors
def __init__(self, value: _T = ...) -> None: ... # pyright: ignore[reportInvalidTypeVarUse]

class _CField:
offset: int
size: int

class _StructUnionMeta(_CDataMeta):
_fields_: Sequence[tuple[str, type[_CData]] | tuple[str, type[_CData], int]]
_pack_: int
_anonymous_: Sequence[str]
def __getattr__(self, name: str) -> _CField: ...

class _StructUnionBase(_CData, metaclass=_StructUnionMeta):
def __init__(self, *args: Any, **kw: Any) -> None: ...
def __getattr__(self, name: str) -> Any: ...
def __setattr__(self, name: str, value: Any) -> None: ...

class Union(_StructUnionBase): ...
class Structure(_StructUnionBase): ...

class Array(Generic[_CT], _CData):
@property
@abstractmethod
def _length_(self) -> int: ...
@_length_.setter
def _length_(self, value: int) -> None: ...
@property
@abstractmethod
def _type_(self) -> type[_CT]: ...
@_type_.setter
def _type_(self, value: type[_CT]) -> None: ...
# Note: only available if _CT == c_char
@property
def raw(self) -> bytes: ...
@raw.setter
def raw(self, value: ReadableBuffer) -> None: ...
value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise
# TODO These methods cannot be annotated correctly at the moment.
# All of these "Any"s stand for the array's element type, but it's not possible to use _CT
# here, because of a special feature of ctypes.
# By default, when accessing an element of an Array[_CT], the returned object has type _CT.
# However, when _CT is a "simple type" like c_int, ctypes automatically "unboxes" the object
# and converts it to the corresponding Python primitive. For example, when accessing an element
# of an Array[c_int], a Python int object is returned, not a c_int.
# This behavior does *not* apply to subclasses of "simple types".
# If MyInt is a subclass of c_int, then accessing an element of an Array[MyInt] returns
# a MyInt, not an int.
# This special behavior is not easy to model in a stub, so for now all places where
# the array element type would belong are annotated with Any instead.
def __init__(self, *args: Any) -> None: ...
@overload
def __getitem__(self, __key: int) -> Any: ...
@overload
def __getitem__(self, __key: slice) -> list[Any]: ...
@overload
def __setitem__(self, __key: int, __value: Any) -> None: ...
@overload
def __setitem__(self, __key: slice, __value: Iterable[Any]) -> None: ...
def __iter__(self) -> Iterator[Any]: ...
# Can't inherit from Sized because the metaclass conflict between
# Sized and _CData prevents using _CDataMeta.
def __len__(self) -> int: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
104 changes: 100 additions & 4 deletions mypy/typeshed/stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ from typing import ( # noqa: Y022
overload,
type_check_only,
)
from typing_extensions import Concatenate, Literal, ParamSpec, Self, SupportsIndex, TypeAlias, TypeGuard, final
from typing_extensions import Concatenate, Literal, LiteralString, ParamSpec, Self, SupportsIndex, TypeAlias, TypeGuard, final

if sys.version_info >= (3, 9):
from types import GenericAlias
Expand Down Expand Up @@ -108,6 +108,8 @@ class object:
def __reduce_ex__(self, __protocol: SupportsIndex) -> str | tuple[Any, ...]: ...
else:
def __reduce_ex__(self, __protocol: int) -> str | tuple[Any, ...]: ...
if sys.version_info >= (3, 11):
def __getstate__(self) -> object: ...

def __dir__(self) -> Iterable[str]: ...
def __init_subclass__(cls) -> None: ...
Expand Down Expand Up @@ -416,20 +418,38 @@ class str(Sequence[str]):
def __new__(cls, object: object = ...) -> Self: ...
@overload
def __new__(cls, object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
@overload
def capitalize(self: LiteralString) -> LiteralString: ...
@overload
def capitalize(self) -> str: ... # type: ignore[misc]
@overload
def casefold(self: LiteralString) -> LiteralString: ...
@overload
def casefold(self) -> str: ... # type: ignore[misc]
@overload
def center(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = " ") -> LiteralString: ...
@overload
def center(self, __width: SupportsIndex, __fillchar: str = " ") -> str: ... # type: ignore[misc]
def count(self, x: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
def encode(self, encoding: str = "utf-8", errors: str = "strict") -> bytes: ...
def endswith(
self, __suffix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
) -> bool: ...
if sys.version_info >= (3, 8):
@overload
def expandtabs(self: LiteralString, tabsize: SupportsIndex = 8) -> LiteralString: ...
@overload
def expandtabs(self, tabsize: SupportsIndex = 8) -> str: ... # type: ignore[misc]
else:
@overload
def expandtabs(self: LiteralString, tabsize: int = 8) -> LiteralString: ...
@overload
def expandtabs(self, tabsize: int = 8) -> str: ... # type: ignore[misc]

def find(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
@overload
def format(self: LiteralString, *args: LiteralString, **kwargs: LiteralString) -> LiteralString: ...
@overload
def format(self, *args: object, **kwargs: object) -> str: ...
def format_map(self, map: _FormatMapMapping) -> str: ...
def index(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
Expand All @@ -445,32 +465,91 @@ class str(Sequence[str]):
def isspace(self) -> bool: ...
def istitle(self) -> bool: ...
def isupper(self) -> bool: ...
@overload
def join(self: LiteralString, __iterable: Iterable[LiteralString]) -> LiteralString: ...
@overload
def join(self, __iterable: Iterable[str]) -> str: ... # type: ignore[misc]
@overload
def ljust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = " ") -> LiteralString: ...
@overload
def ljust(self, __width: SupportsIndex, __fillchar: str = " ") -> str: ... # type: ignore[misc]
@overload
def lower(self: LiteralString) -> LiteralString: ...
@overload
def lower(self) -> str: ... # type: ignore[misc]
@overload
def lstrip(self: LiteralString, __chars: LiteralString | None = None) -> LiteralString: ...
@overload
def lstrip(self, __chars: str | None = None) -> str: ... # type: ignore[misc]
@overload
def partition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
@overload
def partition(self, __sep: str) -> tuple[str, str, str]: ... # type: ignore[misc]
@overload
def replace(
self: LiteralString, __old: LiteralString, __new: LiteralString, __count: SupportsIndex = -1
) -> LiteralString: ...
@overload
def replace(self, __old: str, __new: str, __count: SupportsIndex = -1) -> str: ... # type: ignore[misc]
if sys.version_info >= (3, 9):
@overload
def removeprefix(self: LiteralString, __prefix: LiteralString) -> LiteralString: ...
@overload
def removeprefix(self, __prefix: str) -> str: ... # type: ignore[misc]
@overload
def removesuffix(self: LiteralString, __suffix: LiteralString) -> LiteralString: ...
@overload
def removesuffix(self, __suffix: str) -> str: ... # type: ignore[misc]

def rfind(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
def rindex(self, __sub: str, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...) -> int: ...
@overload
def rjust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = " ") -> LiteralString: ...
@overload
def rjust(self, __width: SupportsIndex, __fillchar: str = " ") -> str: ... # type: ignore[misc]
@overload
def rpartition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
@overload
def rpartition(self, __sep: str) -> tuple[str, str, str]: ... # type: ignore[misc]
@overload
def rsplit(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
@overload
def rsplit(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc]
@overload
def rstrip(self: LiteralString, __chars: LiteralString | None = None) -> LiteralString: ...
@overload
def rstrip(self, __chars: str | None = None) -> str: ... # type: ignore[misc]
@overload
def split(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
@overload
def split(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc]
@overload
def splitlines(self: LiteralString, keepends: bool = False) -> list[LiteralString]: ...
@overload
def splitlines(self, keepends: bool = False) -> list[str]: ... # type: ignore[misc]
def startswith(
self, __prefix: str | tuple[str, ...], __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
) -> bool: ...
@overload
def strip(self: LiteralString, __chars: LiteralString | None = None) -> LiteralString: ...
@overload
def strip(self, __chars: str | None = None) -> str: ... # type: ignore[misc]
@overload
def swapcase(self: LiteralString) -> LiteralString: ...
@overload
def swapcase(self) -> str: ... # type: ignore[misc]
@overload
def title(self: LiteralString) -> LiteralString: ...
@overload
def title(self) -> str: ... # type: ignore[misc]
def translate(self, __table: _TranslateTable) -> str: ...
@overload
def upper(self: LiteralString) -> LiteralString: ...
@overload
def upper(self) -> str: ... # type: ignore[misc]
@overload
def zfill(self: LiteralString, __width: SupportsIndex) -> LiteralString: ...
@overload
def zfill(self, __width: SupportsIndex) -> str: ... # type: ignore[misc]
@staticmethod
@overload
Expand All @@ -481,20 +560,35 @@ class str(Sequence[str]):
@staticmethod
@overload
def maketrans(__x: str, __y: str, __z: str) -> dict[int, int | None]: ...
@overload
def __add__(self: LiteralString, __value: LiteralString) -> LiteralString: ...
@overload
def __add__(self, __value: str) -> str: ... # type: ignore[misc]
# Incompatible with Sequence.__contains__
def __contains__(self, __key: str) -> bool: ... # type: ignore[override]
def __eq__(self, __value: object) -> bool: ...
def __ge__(self, __value: str) -> bool: ...
def __getitem__(self, __key: SupportsIndex | slice) -> str: ...
def __gt__(self, __value: str) -> bool: ...
@overload
def __iter__(self: LiteralString) -> Iterator[LiteralString]: ...
@overload
def __iter__(self) -> Iterator[str]: ... # type: ignore[misc]
def __le__(self, __value: str) -> bool: ...
def __len__(self) -> int: ...
def __lt__(self, __value: str) -> bool: ...
@overload
def __mod__(self: LiteralString, __value: LiteralString | tuple[LiteralString, ...]) -> LiteralString: ...
@overload
def __mod__(self, __value: Any) -> str: ...
@overload
def __mul__(self: LiteralString, __value: SupportsIndex) -> LiteralString: ...
@overload
def __mul__(self, __value: SupportsIndex) -> str: ... # type: ignore[misc]
def __ne__(self, __value: object) -> bool: ...
@overload
def __rmul__(self: LiteralString, __value: SupportsIndex) -> LiteralString: ...
@overload
def __rmul__(self, __value: SupportsIndex) -> str: ... # type: ignore[misc]
def __getnewargs__(self) -> tuple[str]: ...

Expand Down Expand Up @@ -940,10 +1034,12 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __init__(self, __iterable: Iterable[tuple[_KT, _VT]]) -> None: ...
@overload
def __init__(self: dict[str, _VT], __iterable: Iterable[tuple[str, _VT]], **kwargs: _VT) -> None: ...
# Next overload is for dict(string.split(sep) for string in iterable)
# Next two overloads are for dict(string.split(sep) for string in iterable)
# Cannot be Iterable[Sequence[_T]] or otherwise dict(["foo", "bar", "baz"]) is not an error
@overload
def __init__(self: dict[str, str], __iterable: Iterable[list[str]]) -> None: ...
@overload
def __init__(self: dict[bytes, bytes], __iterable: Iterable[list[bytes]]) -> None: ...
def __new__(cls, *args: Any, **kwargs: Any) -> Self: ...
def copy(self) -> dict[_KT, _VT]: ...
def keys(self) -> dict_keys[_KT, _VT]: ...
Expand Down Expand Up @@ -1638,11 +1734,11 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit
# Instead, we special-case the most common examples of this: bool and literal integers.
if sys.version_info >= (3, 8):
@overload
def sum(__iterable: Iterable[bool], start: int = 0) -> int: ... # type: ignore[misc]
def sum(__iterable: Iterable[bool | _LiteralInteger], start: int = 0) -> int: ... # type: ignore[misc]

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

@overload
def sum(__iterable: Iterable[_SupportsSumNoDefaultT]) -> _SupportsSumNoDefaultT | Literal[0]: ...
Expand Down
2 changes: 2 additions & 0 deletions mypy/typeshed/stdlib/collections/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __init__(self: UserDict[str, _VT], __iterable: Iterable[tuple[str, _VT]], **kwargs: _VT) -> None: ...
@overload
def __init__(self: UserDict[str, str], __iterable: Iterable[list[str]]) -> None: ...
@overload
def __init__(self: UserDict[bytes, bytes], __iterable: Iterable[list[bytes]]) -> None: ...
def __len__(self) -> int: ...
def __getitem__(self, key: _KT) -> _VT: ...
def __setitem__(self, key: _KT, item: _VT) -> None: ...
Expand Down
4 changes: 3 additions & 1 deletion mypy/typeshed/stdlib/configparser.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ __all__ = [
"ParsingError",
"MissingSectionHeaderError",
"ConfigParser",
"SafeConfigParser",
"RawConfigParser",
"Interpolation",
"BasicInterpolation",
Expand All @@ -29,6 +28,9 @@ __all__ = [
"MAX_INTERPOLATION_DEPTH",
]

if sys.version_info < (3, 12):
__all__ += ["SafeConfigParser"]

_Section: TypeAlias = Mapping[str, str]
_Parser: TypeAlias = MutableMapping[str, _Section]
_ConverterCallback: TypeAlias = Callable[[str], Any]
Expand Down
Loading

0 comments on commit 8b356ff

Please sign in to comment.