Skip to content

Commit 848b2d3

Browse files
Sync typeshed (#15242)
Source commit: python/typeshed@15a0c28
1 parent 5e93a46 commit 848b2d3

File tree

10 files changed

+137
-95
lines changed

10 files changed

+137
-95
lines changed

mypy/typeshed/stdlib/_ctypes.pyi

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import sys
22
from _typeshed import ReadableBuffer, WriteableBuffer
33
from abc import abstractmethod
4-
from collections.abc import Iterable, Iterator, Mapping, Sequence
5-
from ctypes import CDLL, _CArgObject, _PointerLike
6-
from typing import Any, Generic, TypeVar, overload
4+
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
5+
from ctypes import CDLL
6+
from typing import Any, ClassVar, Generic, TypeVar, overload
77
from typing_extensions import Self, TypeAlias
88

99
if sys.version_info >= (3, 9):
@@ -38,6 +38,10 @@ if sys.platform == "win32":
3838
FUNCFLAG_HRESULT: int
3939
FUNCFLAG_STDCALL: int
4040

41+
def FormatError(code: int = ...) -> str: ...
42+
def get_last_error() -> int: ...
43+
def set_last_error(value: int) -> int: ...
44+
4145
class _CDataMeta(type):
4246
# By default mypy complains about the following two methods, because strictly speaking cls
4347
# might not be a Type[_CT]. However this can never actually happen, because the only class that
@@ -66,6 +70,53 @@ class _SimpleCData(Generic[_T], _CData):
6670
# but we can't use overloads without creating many, many mypy false-positive errors
6771
def __init__(self, value: _T = ...) -> None: ... # pyright: ignore[reportInvalidTypeVarUse]
6872

73+
class _CanCastTo(_CData): ...
74+
class _PointerLike(_CanCastTo): ...
75+
76+
class _Pointer(Generic[_CT], _PointerLike, _CData):
77+
_type_: type[_CT]
78+
contents: _CT
79+
@overload
80+
def __init__(self) -> None: ...
81+
@overload
82+
def __init__(self, arg: _CT) -> None: ...
83+
@overload
84+
def __getitem__(self, __key: int) -> Any: ...
85+
@overload
86+
def __getitem__(self, __key: slice) -> list[Any]: ...
87+
def __setitem__(self, __key: int, __value: Any) -> None: ...
88+
89+
def POINTER(type: type[_CT]) -> type[_Pointer[_CT]]: ...
90+
def pointer(__arg: _CT) -> _Pointer[_CT]: ...
91+
92+
class _CArgObject: ...
93+
94+
def byref(obj: _CData, offset: int = ...) -> _CArgObject: ...
95+
96+
_ECT: TypeAlias = Callable[[type[_CData] | None, CFuncPtr, tuple[_CData, ...]], _CData]
97+
_PF: TypeAlias = tuple[int] | tuple[int, str | None] | tuple[int, str | None, Any]
98+
99+
class CFuncPtr(_PointerLike, _CData):
100+
restype: type[_CData] | Callable[[int], Any] | None
101+
argtypes: Sequence[type[_CData]]
102+
errcheck: _ECT
103+
_flags_: ClassVar[int] # Abstract attribute that must be defined on subclasses
104+
@overload
105+
def __init__(self) -> None: ...
106+
@overload
107+
def __init__(self, __address: int) -> None: ...
108+
@overload
109+
def __init__(self, __callable: Callable[..., Any]) -> None: ...
110+
@overload
111+
def __init__(self, __func_spec: tuple[str | int, CDLL], __paramflags: tuple[_PF, ...] | None = ...) -> None: ...
112+
if sys.platform == "win32":
113+
@overload
114+
def __init__(
115+
self, __vtbl_index: int, __name: str, __paramflags: tuple[_PF, ...] | None = ..., __iid: _CData | None = ...
116+
) -> None: ...
117+
118+
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
119+
69120
class _CField:
70121
offset: int
71122
size: int
@@ -124,3 +175,12 @@ class Array(Generic[_CT], _CData):
124175
def __len__(self) -> int: ...
125176
if sys.version_info >= (3, 9):
126177
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
178+
179+
class ArgumentError(Exception): ...
180+
181+
def addressof(obj: _CData) -> int: ...
182+
def alignment(obj_or_type: _CData | type[_CData]) -> int: ...
183+
def get_errno() -> int: ...
184+
def resize(obj: _CData, size: int) -> None: ...
185+
def set_errno(value: int) -> int: ...
186+
def sizeof(obj_or_type: _CData | type[_CData]) -> int: ...

mypy/typeshed/stdlib/asyncio/__init__.pyi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import sys
2+
from collections.abc import Coroutine, Generator
3+
from typing import Any, TypeVar
4+
from typing_extensions import TypeAlias
25

36
# As at runtime, this depends on all submodules defining __all__ accurately.
47
from .base_events import *
@@ -28,3 +31,11 @@ if sys.platform == "win32":
2831
from .windows_events import *
2932
else:
3033
from .unix_events import *
34+
35+
_T = TypeVar("_T")
36+
37+
# Aliases imported by multiple submodules in typeshed
38+
if sys.version_info >= (3, 12):
39+
_CoroutineLike: TypeAlias = Coroutine[Any, Any, _T]
40+
else:
41+
_CoroutineLike: TypeAlias = Generator[Any, None, _T] | Coroutine[Any, Any, _T]

mypy/typeshed/stdlib/asyncio/base_events.pyi

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import ssl
22
import sys
33
from _typeshed import FileDescriptorLike, ReadableBuffer, WriteableBuffer
4+
from asyncio import _CoroutineLike
45
from asyncio.events import AbstractEventLoop, AbstractServer, Handle, TimerHandle, _TaskFactory
56
from asyncio.futures import Future
67
from asyncio.protocols import BaseProtocol
78
from asyncio.tasks import Task
89
from asyncio.transports import BaseTransport, DatagramTransport, ReadTransport, SubprocessTransport, Transport, WriteTransport
9-
from collections.abc import Awaitable, Callable, Coroutine, Generator, Iterable, Sequence
10+
from collections.abc import Awaitable, Callable, Generator, Iterable, Sequence
1011
from contextvars import Context
1112
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
1213
from typing import IO, Any, TypeVar, overload
@@ -86,13 +87,11 @@ class BaseEventLoop(AbstractEventLoop):
8687
def create_future(self) -> Future[Any]: ...
8788
# Tasks methods
8889
if sys.version_info >= (3, 11):
89-
def create_task(
90-
self, coro: Coroutine[Any, Any, _T] | Generator[Any, None, _T], *, name: object = None, context: Context | None = None
91-
) -> Task[_T]: ...
90+
def create_task(self, coro: _CoroutineLike[_T], *, name: object = None, context: Context | None = None) -> Task[_T]: ...
9291
elif sys.version_info >= (3, 8):
93-
def create_task(self, coro: Coroutine[Any, Any, _T] | Generator[Any, None, _T], *, name: object = None) -> Task[_T]: ...
92+
def create_task(self, coro: _CoroutineLike[_T], *, name: object = None) -> Task[_T]: ...
9493
else:
95-
def create_task(self, coro: Coroutine[Any, Any, _T] | Generator[Any, None, _T]) -> Task[_T]: ...
94+
def create_task(self, coro: _CoroutineLike[_T]) -> Task[_T]: ...
9695

9796
def set_task_factory(self, factory: _TaskFactory | None) -> None: ...
9897
def get_task_factory(self) -> _TaskFactory | None: ...

mypy/typeshed/stdlib/asyncio/events.pyi

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
88
from typing import IO, Any, Protocol, TypeVar, overload
99
from typing_extensions import Literal, Self, TypeAlias
1010

11+
from . import _CoroutineLike
1112
from .base_events import Server
1213
from .futures import Future
1314
from .protocols import BaseProtocol
@@ -158,20 +159,14 @@ class AbstractEventLoop:
158159
if sys.version_info >= (3, 11):
159160
@abstractmethod
160161
def create_task(
161-
self,
162-
coro: Coroutine[Any, Any, _T] | Generator[Any, None, _T],
163-
*,
164-
name: str | None = None,
165-
context: Context | None = None,
162+
self, coro: _CoroutineLike[_T], *, name: str | None = None, context: Context | None = None
166163
) -> Task[_T]: ...
167164
elif sys.version_info >= (3, 8):
168165
@abstractmethod
169-
def create_task(
170-
self, coro: Coroutine[Any, Any, _T] | Generator[Any, None, _T], *, name: str | None = None
171-
) -> Task[_T]: ...
166+
def create_task(self, coro: _CoroutineLike[_T], *, name: str | None = None) -> Task[_T]: ...
172167
else:
173168
@abstractmethod
174-
def create_task(self, coro: Coroutine[Any, Any, _T] | Generator[Any, None, _T]) -> Task[_T]: ...
169+
def create_task(self, coro: _CoroutineLike[_T]) -> Task[_T]: ...
175170

176171
@abstractmethod
177172
def set_task_factory(self, factory: _TaskFactory | None) -> None: ...

mypy/typeshed/stdlib/asyncio/sslproto.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ class _SSLProtocolTransport(transports._FlowControlMixin, transports.Transport):
6666
_sendfile_compatible: ClassVar[constants._SendfileMode]
6767

6868
_loop: events.AbstractEventLoop
69-
_ssl_protocol: SSLProtocol
69+
if sys.version_info >= (3, 11):
70+
_ssl_protocol: SSLProtocol | None
71+
else:
72+
_ssl_protocol: SSLProtocol
7073
_closed: bool
7174
def __init__(self, loop: events.AbstractEventLoop, ssl_protocol: SSLProtocol) -> None: ...
7275
def get_extra_info(self, name: str, default: Any | None = None) -> dict[str, Any]: ...
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# This only exists in 3.11+. See VERSIONS.
22

3-
from collections.abc import Coroutine, Generator
43
from contextvars import Context
54
from types import TracebackType
6-
from typing import Any, TypeVar
5+
from typing import TypeVar
76
from typing_extensions import Self
87

8+
from . import _CoroutineLike
99
from .tasks import Task
1010

1111
__all__ = ["TaskGroup"]
@@ -15,6 +15,4 @@ _T = TypeVar("_T")
1515
class TaskGroup:
1616
async def __aenter__(self) -> Self: ...
1717
async def __aexit__(self, et: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None) -> None: ...
18-
def create_task(
19-
self, coro: Generator[Any, None, _T] | Coroutine[Any, Any, _T], *, name: str | None = None, context: Context | None = None
20-
) -> Task[_T]: ...
18+
def create_task(self, coro: _CoroutineLike[_T], *, name: str | None = None, context: Context | None = None) -> Task[_T]: ...

mypy/typeshed/stdlib/asyncio/tasks.pyi

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import concurrent.futures
22
import sys
3-
from collections.abc import Awaitable, Coroutine, Generator, Iterable, Iterator
3+
from collections.abc import Awaitable, Generator, Iterable, Iterator
44
from types import FrameType
55
from typing import Any, Generic, TextIO, TypeVar, overload
66
from typing_extensions import Literal, TypeAlias
77

8+
from . import _CoroutineLike
89
from .events import AbstractEventLoop
910
from .futures import Future
1011

@@ -312,15 +313,13 @@ class Task(Future[_T_co], Generic[_T_co]): # type: ignore[type-var] # pyright:
312313
def all_tasks(loop: AbstractEventLoop | None = None) -> set[Task[Any]]: ...
313314

314315
if sys.version_info >= (3, 11):
315-
def create_task(
316-
coro: Generator[Any, None, _T] | Coroutine[Any, Any, _T], *, name: str | None = None, context: Context | None = None
317-
) -> Task[_T]: ...
316+
def create_task(coro: _CoroutineLike[_T], *, name: str | None = None, context: Context | None = None) -> Task[_T]: ...
318317

319318
elif sys.version_info >= (3, 8):
320-
def create_task(coro: Generator[Any, None, _T] | Coroutine[Any, Any, _T], *, name: str | None = None) -> Task[_T]: ...
319+
def create_task(coro: _CoroutineLike[_T], *, name: str | None = None) -> Task[_T]: ...
321320

322321
else:
323-
def create_task(coro: Generator[Any, None, _T] | Coroutine[Any, Any, _T]) -> Task[_T]: ...
322+
def create_task(coro: _CoroutineLike[_T]) -> Task[_T]: ...
324323

325324
def current_task(loop: AbstractEventLoop | None = None) -> Task[Any] | None: ...
326325
def _enter_task(loop: AbstractEventLoop, task: Task[Any]) -> None: ...

mypy/typeshed/stdlib/ctypes/__init__.pyi

Lines changed: 20 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
import sys
22
from _ctypes import (
3+
POINTER as POINTER,
34
RTLD_GLOBAL as RTLD_GLOBAL,
45
RTLD_LOCAL as RTLD_LOCAL,
6+
ArgumentError as ArgumentError,
57
Array as Array,
8+
CFuncPtr as _CFuncPtr,
69
Structure as Structure,
710
Union as Union,
11+
_CanCastTo as _CanCastTo,
12+
_CArgObject as _CArgObject,
813
_CData as _CData,
914
_CDataMeta as _CDataMeta,
1015
_CField as _CField,
16+
_Pointer as _Pointer,
17+
_PointerLike as _PointerLike,
1118
_SimpleCData as _SimpleCData,
1219
_StructUnionBase as _StructUnionBase,
1320
_StructUnionMeta as _StructUnionMeta,
21+
addressof as addressof,
22+
alignment as alignment,
23+
byref as byref,
24+
get_errno as get_errno,
25+
pointer as pointer,
26+
resize as resize,
27+
set_errno as set_errno,
28+
sizeof as sizeof,
1429
)
15-
from collections.abc import Callable, Sequence
16-
from typing import Any, ClassVar, Generic, TypeVar, overload
30+
from typing import Any, ClassVar, Generic, TypeVar
1731
from typing_extensions import TypeAlias
1832

33+
if sys.platform == "win32":
34+
from _ctypes import FormatError as FormatError, get_last_error as get_last_error, set_last_error as set_last_error
35+
1936
if sys.version_info >= (3, 9):
2037
from types import GenericAlias
2138

2239
_T = TypeVar("_T")
2340
_DLLT = TypeVar("_DLLT", bound=CDLL)
24-
_CT = TypeVar("_CT", bound=_CData)
2541

2642
DEFAULT_MODE: int
2743

@@ -75,31 +91,11 @@ if sys.platform == "win32":
7591
pydll: LibraryLoader[PyDLL]
7692
pythonapi: PyDLL
7793

78-
class _CanCastTo(_CData): ...
79-
class _PointerLike(_CanCastTo): ...
80-
81-
_ECT: TypeAlias = Callable[[type[_CData] | None, _FuncPointer, tuple[_CData, ...]], _CData]
82-
_PF: TypeAlias = tuple[int] | tuple[int, str] | tuple[int, str, Any]
83-
84-
class _FuncPointer(_PointerLike, _CData):
85-
restype: type[_CData] | Callable[[int], Any] | None
86-
argtypes: Sequence[type[_CData]]
87-
errcheck: _ECT
88-
@overload
89-
def __init__(self, address: int) -> None: ...
90-
@overload
91-
def __init__(self, callable: Callable[..., Any]) -> None: ...
92-
@overload
93-
def __init__(self, func_spec: tuple[str | int, CDLL], paramflags: tuple[_PF, ...] = ...) -> None: ...
94-
@overload
95-
def __init__(self, vtlb_index: int, name: str, paramflags: tuple[_PF, ...] = ..., iid: _Pointer[c_int] = ...) -> None: ...
96-
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
94+
class _FuncPointer(_CFuncPtr): ...
9795

9896
class _NamedFuncPointer(_FuncPointer):
9997
__name__: str
10098

101-
class ArgumentError(Exception): ...
102-
10399
def CFUNCTYPE(
104100
restype: type[_CData] | None, *argtypes: type[_CData], use_errno: bool = ..., use_last_error: bool = ...
105101
) -> type[_FuncPointer]: ...
@@ -111,8 +107,6 @@ if sys.platform == "win32":
111107

112108
def PYFUNCTYPE(restype: type[_CData] | None, *argtypes: type[_CData]) -> type[_FuncPointer]: ...
113109

114-
class _CArgObject: ...
115-
116110
# Any type that can be implicitly converted to c_void_p when passed as a C function argument.
117111
# (bytes is not included here, see below.)
118112
_CVoidPLike: TypeAlias = _PointerLike | Array[Any] | _CArgObject | int
@@ -122,10 +116,6 @@ _CVoidPLike: TypeAlias = _PointerLike | Array[Any] | _CArgObject | int
122116
# when memmove(buf, b'foo', 4) was intended.
123117
_CVoidConstPLike: TypeAlias = _CVoidPLike | bytes
124118

125-
def addressof(obj: _CData) -> int: ...
126-
def alignment(obj_or_type: _CData | type[_CData]) -> int: ...
127-
def byref(obj: _CData, offset: int = ...) -> _CArgObject: ...
128-
129119
_CastT = TypeVar("_CastT", bound=_CanCastTo)
130120

131121
def cast(obj: _CData | _CArgObject | int, typ: type[_CastT]) -> _CastT: ...
@@ -138,39 +128,10 @@ def create_unicode_buffer(init: int | str, size: int | None = None) -> Array[c_w
138128
if sys.platform == "win32":
139129
def DllCanUnloadNow() -> int: ...
140130
def DllGetClassObject(rclsid: Any, riid: Any, ppv: Any) -> int: ... # TODO not documented
141-
def FormatError(code: int = ...) -> str: ...
142131
def GetLastError() -> int: ...
143132

144-
def get_errno() -> int: ...
145-
146-
if sys.platform == "win32":
147-
def get_last_error() -> int: ...
148-
149133
def memmove(dst: _CVoidPLike, src: _CVoidConstPLike, count: int) -> int: ...
150134
def memset(dst: _CVoidPLike, c: int, count: int) -> int: ...
151-
def POINTER(type: type[_CT]) -> type[_Pointer[_CT]]: ...
152-
153-
class _Pointer(Generic[_CT], _PointerLike, _CData):
154-
_type_: type[_CT]
155-
contents: _CT
156-
@overload
157-
def __init__(self) -> None: ...
158-
@overload
159-
def __init__(self, arg: _CT) -> None: ...
160-
@overload
161-
def __getitem__(self, __key: int) -> Any: ...
162-
@overload
163-
def __getitem__(self, __key: slice) -> list[Any]: ...
164-
def __setitem__(self, __key: int, __value: Any) -> None: ...
165-
166-
def pointer(__arg: _CT) -> _Pointer[_CT]: ...
167-
def resize(obj: _CData, size: int) -> None: ...
168-
def set_errno(value: int) -> int: ...
169-
170-
if sys.platform == "win32":
171-
def set_last_error(value: int) -> int: ...
172-
173-
def sizeof(obj_or_type: _CData | type[_CData]) -> int: ...
174135
def string_at(address: _CVoidConstPLike, size: int = -1) -> bytes: ...
175136

176137
if sys.platform == "win32":

0 commit comments

Comments
 (0)