Skip to content

Commit 43ffb49

Browse files
Sync typeshed (#16665)
Source commit: python/typeshed@b6740d0
1 parent 5e77c3e commit 43ffb49

35 files changed

+464
-279
lines changed

mypy/typeshed/stdlib/VERSIONS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ imaplib: 2.7-
150150
imghdr: 2.7-
151151
imp: 2.7-3.11
152152
importlib: 2.7-
153+
importlib._abc: 3.10-
153154
importlib.metadata: 3.8-
154155
importlib.metadata._meta: 3.10-
155156
importlib.readers: 3.10-

mypy/typeshed/stdlib/_ctypes.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class _CDataMeta(type):
5151
# By default mypy complains about the following two methods, because strictly speaking cls
5252
# might not be a Type[_CT]. However this can never actually happen, because the only class that
5353
# uses _CDataMeta as its metaclass is _CData. So it's safe to ignore the errors here.
54-
def __mul__(cls: type[_CT], other: int) -> type[Array[_CT]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
55-
def __rmul__(cls: type[_CT], other: int) -> type[Array[_CT]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
54+
def __mul__(cls: type[_CT], other: int) -> type[Array[_CT]]: ... # type: ignore[misc]
55+
def __rmul__(cls: type[_CT], other: int) -> type[Array[_CT]]: ... # type: ignore[misc]
5656

5757
class _CData(metaclass=_CDataMeta):
5858
_b_base_: int

mypy/typeshed/stdlib/_operator.pyi

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import sys
22
from _typeshed import SupportsGetItem
33
from collections.abc import Callable, Container, Iterable, MutableMapping, MutableSequence, Sequence
44
from typing import Any, AnyStr, Generic, Protocol, SupportsAbs, TypeVar, overload
5-
from typing_extensions import ParamSpec, SupportsIndex, TypeAlias, final
5+
from typing_extensions import ParamSpec, SupportsIndex, TypeAlias, TypeVarTuple, Unpack, final
66

77
_R = TypeVar("_R")
88
_T = TypeVar("_T")
99
_T_co = TypeVar("_T_co", covariant=True)
10+
_T1 = TypeVar("_T1")
11+
_T2 = TypeVar("_T2")
1012
_K = TypeVar("_K")
1113
_V = TypeVar("_V")
1214
_P = ParamSpec("_P")
15+
_Ts = TypeVarTuple("_Ts")
1316

1417
# The following protocols return "Any" instead of bool, since the comparison
1518
# operators can be overloaded to return an arbitrary object. For example,
@@ -105,22 +108,10 @@ class attrgetter(Generic[_T_co]):
105108

106109
@final
107110
class itemgetter(Generic[_T_co]):
108-
# mypy lacks support for PEP 646 https://github.com/python/mypy/issues/12280
109-
# So we have to define all of these overloads to simulate unpacking the arguments
110111
@overload
111-
def __new__(cls, item: _T_co) -> itemgetter[_T_co]: ...
112+
def __new__(cls, __item: _T) -> itemgetter[_T]: ...
112113
@overload
113-
def __new__(cls, item: _T_co, __item2: _T_co) -> itemgetter[tuple[_T_co, _T_co]]: ...
114-
@overload
115-
def __new__(cls, item: _T_co, __item2: _T_co, __item3: _T_co) -> itemgetter[tuple[_T_co, _T_co, _T_co]]: ...
116-
@overload
117-
def __new__(
118-
cls, item: _T_co, __item2: _T_co, __item3: _T_co, __item4: _T_co
119-
) -> itemgetter[tuple[_T_co, _T_co, _T_co, _T_co]]: ...
120-
@overload
121-
def __new__(
122-
cls, item: _T_co, __item2: _T_co, __item3: _T_co, __item4: _T_co, *items: _T_co
123-
) -> itemgetter[tuple[_T_co, ...]]: ...
114+
def __new__(cls, __item1: _T1, __item2: _T2, *items: Unpack[_Ts]) -> itemgetter[tuple[_T1, _T2, Unpack[_Ts]]]: ...
124115
# __key: _KT_contra in SupportsGetItem seems to be causing variance issues, ie:
125116
# TypeVar "_KT_contra@SupportsGetItem" is contravariant
126117
# "tuple[int, int]" is incompatible with protocol "SupportsIndex"

mypy/typeshed/stdlib/_typeshed/__init__.pyi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,12 @@ class DataclassInstance(Protocol):
320320
# Anything that can be passed to the int/float constructors
321321
ConvertibleToInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc
322322
ConvertibleToFloat: TypeAlias = str | ReadableBuffer | SupportsFloat | SupportsIndex
323+
324+
# A few classes updated from Foo(str, Enum) to Foo(StrEnum). This is a convenience so these
325+
# can be accurate on all python versions without getting too wordy
326+
if sys.version_info >= (3, 11):
327+
from enum import StrEnum as StrEnum
328+
else:
329+
from enum import Enum
330+
331+
class StrEnum(str, Enum): ...

mypy/typeshed/stdlib/asyncio/base_events.pyi

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ from collections.abc import Callable, Iterable, Sequence
1111
from contextvars import Context
1212
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
1313
from typing import IO, Any, TypeVar, overload
14-
from typing_extensions import Literal, TypeAlias
14+
from typing_extensions import Literal, TypeAlias, TypeVarTuple, Unpack
1515

1616
if sys.version_info >= (3, 9):
1717
__all__ = ("BaseEventLoop", "Server")
1818
else:
1919
__all__ = ("BaseEventLoop",)
2020

2121
_T = TypeVar("_T")
22+
_Ts = TypeVarTuple("_Ts")
2223
_ProtocolT = TypeVar("_ProtocolT", bound=BaseProtocol)
2324
_Context: TypeAlias = dict[str, Any]
2425
_ExceptionHandler: TypeAlias = Callable[[AbstractEventLoop, _Context], object]
@@ -71,12 +72,14 @@ class BaseEventLoop(AbstractEventLoop):
7172
def close(self) -> None: ...
7273
async def shutdown_asyncgens(self) -> None: ...
7374
# Methods scheduling callbacks. All these return Handles.
74-
def call_soon(self, callback: Callable[..., object], *args: Any, context: Context | None = None) -> Handle: ...
75+
def call_soon(
76+
self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
77+
) -> Handle: ...
7578
def call_later(
76-
self, delay: float, callback: Callable[..., object], *args: Any, context: Context | None = None
79+
self, delay: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
7780
) -> TimerHandle: ...
7881
def call_at(
79-
self, when: float, callback: Callable[..., object], *args: Any, context: Context | None = None
82+
self, when: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
8083
) -> TimerHandle: ...
8184
def time(self) -> float: ...
8285
# Future methods
@@ -92,8 +95,10 @@ class BaseEventLoop(AbstractEventLoop):
9295
def set_task_factory(self, factory: _TaskFactory | None) -> None: ...
9396
def get_task_factory(self) -> _TaskFactory | None: ...
9497
# Methods for interacting with threads
95-
def call_soon_threadsafe(self, callback: Callable[..., object], *args: Any, context: Context | None = None) -> Handle: ...
96-
def run_in_executor(self, executor: Any, func: Callable[..., _T], *args: Any) -> Future[_T]: ...
98+
def call_soon_threadsafe(
99+
self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
100+
) -> Handle: ...
101+
def run_in_executor(self, executor: Any, func: Callable[[Unpack[_Ts]], _T], *args: Unpack[_Ts]) -> Future[_T]: ...
97102
def set_default_executor(self, executor: Any) -> None: ...
98103
# Network I/O methods returning Futures.
99104
async def getaddrinfo(
@@ -441,9 +446,9 @@ class BaseEventLoop(AbstractEventLoop):
441446
errors: None = None,
442447
**kwargs: Any,
443448
) -> tuple[SubprocessTransport, _ProtocolT]: ...
444-
def add_reader(self, fd: FileDescriptorLike, callback: Callable[..., Any], *args: Any) -> None: ...
449+
def add_reader(self, fd: FileDescriptorLike, callback: Callable[[Unpack[_Ts]], Any], *args: Unpack[_Ts]) -> None: ...
445450
def remove_reader(self, fd: FileDescriptorLike) -> bool: ...
446-
def add_writer(self, fd: FileDescriptorLike, callback: Callable[..., Any], *args: Any) -> None: ...
451+
def add_writer(self, fd: FileDescriptorLike, callback: Callable[[Unpack[_Ts]], Any], *args: Unpack[_Ts]) -> None: ...
447452
def remove_writer(self, fd: FileDescriptorLike) -> bool: ...
448453
# The sock_* methods (and probably some others) are not actually implemented on
449454
# BaseEventLoop, only on subclasses. We list them here for now for convenience.
@@ -457,7 +462,7 @@ class BaseEventLoop(AbstractEventLoop):
457462
async def sock_recvfrom_into(self, sock: socket, buf: WriteableBuffer, nbytes: int = 0) -> tuple[int, _RetAddress]: ...
458463
async def sock_sendto(self, sock: socket, data: ReadableBuffer, address: _Address) -> int: ...
459464
# Signal handling.
460-
def add_signal_handler(self, sig: int, callback: Callable[..., Any], *args: Any) -> None: ...
465+
def add_signal_handler(self, sig: int, callback: Callable[[Unpack[_Ts]], Any], *args: Unpack[_Ts]) -> None: ...
461466
def remove_signal_handler(self, sig: int) -> bool: ...
462467
# Error handlers.
463468
def set_exception_handler(self, handler: _ExceptionHandler | None) -> None: ...

mypy/typeshed/stdlib/asyncio/events.pyi

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from collections.abc import Callable, Coroutine, Generator, Sequence
66
from contextvars import Context
77
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
88
from typing import IO, Any, Protocol, TypeVar, overload
9-
from typing_extensions import Literal, Self, TypeAlias, deprecated
9+
from typing_extensions import Literal, Self, TypeAlias, TypeVarTuple, Unpack, deprecated
1010

1111
from . import _AwaitableLike, _CoroutineLike
1212
from .base_events import Server
@@ -56,6 +56,7 @@ else:
5656
)
5757

5858
_T = TypeVar("_T")
59+
_Ts = TypeVarTuple("_Ts")
5960
_ProtocolT = TypeVar("_ProtocolT", bound=BaseProtocol)
6061
_Context: TypeAlias = dict[str, Any]
6162
_ExceptionHandler: TypeAlias = Callable[[AbstractEventLoop, _Context], object]
@@ -131,22 +132,24 @@ class AbstractEventLoop:
131132
# Methods scheduling callbacks. All these return Handles.
132133
if sys.version_info >= (3, 9): # "context" added in 3.9.10/3.10.2
133134
@abstractmethod
134-
def call_soon(self, callback: Callable[..., object], *args: Any, context: Context | None = None) -> Handle: ...
135+
def call_soon(
136+
self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
137+
) -> Handle: ...
135138
@abstractmethod
136139
def call_later(
137-
self, delay: float, callback: Callable[..., object], *args: Any, context: Context | None = None
140+
self, delay: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
138141
) -> TimerHandle: ...
139142
@abstractmethod
140143
def call_at(
141-
self, when: float, callback: Callable[..., object], *args: Any, context: Context | None = None
144+
self, when: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
142145
) -> TimerHandle: ...
143146
else:
144147
@abstractmethod
145-
def call_soon(self, callback: Callable[..., object], *args: Any) -> Handle: ...
148+
def call_soon(self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> Handle: ...
146149
@abstractmethod
147-
def call_later(self, delay: float, callback: Callable[..., object], *args: Any) -> TimerHandle: ...
150+
def call_later(self, delay: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> TimerHandle: ...
148151
@abstractmethod
149-
def call_at(self, when: float, callback: Callable[..., object], *args: Any) -> TimerHandle: ...
152+
def call_at(self, when: float, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> TimerHandle: ...
150153

151154
@abstractmethod
152155
def time(self) -> float: ...
@@ -173,13 +176,15 @@ class AbstractEventLoop:
173176
# Methods for interacting with threads
174177
if sys.version_info >= (3, 9): # "context" added in 3.9.10/3.10.2
175178
@abstractmethod
176-
def call_soon_threadsafe(self, callback: Callable[..., object], *args: Any, context: Context | None = None) -> Handle: ...
179+
def call_soon_threadsafe(
180+
self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts], context: Context | None = None
181+
) -> Handle: ...
177182
else:
178183
@abstractmethod
179-
def call_soon_threadsafe(self, callback: Callable[..., object], *args: Any) -> Handle: ...
184+
def call_soon_threadsafe(self, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> Handle: ...
180185

181186
@abstractmethod
182-
def run_in_executor(self, executor: Any, func: Callable[..., _T], *args: Any) -> Future[_T]: ...
187+
def run_in_executor(self, executor: Any, func: Callable[[Unpack[_Ts]], _T], *args: Unpack[_Ts]) -> Future[_T]: ...
183188
@abstractmethod
184189
def set_default_executor(self, executor: Any) -> None: ...
185190
# Network I/O methods returning Futures.
@@ -542,11 +547,11 @@ class AbstractEventLoop:
542547
**kwargs: Any,
543548
) -> tuple[SubprocessTransport, _ProtocolT]: ...
544549
@abstractmethod
545-
def add_reader(self, fd: FileDescriptorLike, callback: Callable[..., Any], *args: Any) -> None: ...
550+
def add_reader(self, fd: FileDescriptorLike, callback: Callable[[Unpack[_Ts]], Any], *args: Unpack[_Ts]) -> None: ...
546551
@abstractmethod
547552
def remove_reader(self, fd: FileDescriptorLike) -> bool: ...
548553
@abstractmethod
549-
def add_writer(self, fd: FileDescriptorLike, callback: Callable[..., Any], *args: Any) -> None: ...
554+
def add_writer(self, fd: FileDescriptorLike, callback: Callable[[Unpack[_Ts]], Any], *args: Unpack[_Ts]) -> None: ...
550555
@abstractmethod
551556
def remove_writer(self, fd: FileDescriptorLike) -> bool: ...
552557
# Completion based I/O methods returning Futures prior to 3.7
@@ -569,7 +574,7 @@ class AbstractEventLoop:
569574
async def sock_sendto(self, sock: socket, data: ReadableBuffer, address: _Address) -> int: ...
570575
# Signal handling.
571576
@abstractmethod
572-
def add_signal_handler(self, sig: int, callback: Callable[..., object], *args: Any) -> None: ...
577+
def add_signal_handler(self, sig: int, callback: Callable[[Unpack[_Ts]], object], *args: Unpack[_Ts]) -> None: ...
573578
@abstractmethod
574579
def remove_signal_handler(self, sig: int) -> bool: ...
575580
# Error handlers.

mypy/typeshed/stdlib/asyncio/exceptions.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ else:
2121
)
2222

2323
class CancelledError(BaseException): ...
24-
class TimeoutError(Exception): ...
24+
25+
if sys.version_info >= (3, 11):
26+
from builtins import TimeoutError as TimeoutError
27+
else:
28+
class TimeoutError(Exception): ...
29+
2530
class InvalidStateError(Exception): ...
2631
class SendfileNotAvailableError(RuntimeError): ...
2732

mypy/typeshed/stdlib/asyncio/locks.pyi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ from typing_extensions import Literal, Self
1010
from .events import AbstractEventLoop
1111
from .futures import Future
1212

13-
if sys.version_info >= (3, 11):
13+
if sys.version_info >= (3, 10):
1414
from .mixins import _LoopBoundMixin
15+
else:
16+
_LoopBoundMixin = object
1517

1618
if sys.version_info >= (3, 11):
1719
__all__ = ("Lock", "Event", "Condition", "Semaphore", "BoundedSemaphore", "Barrier")
@@ -44,7 +46,7 @@ else:
4446
self, exc_type: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None
4547
) -> None: ...
4648

47-
class Lock(_ContextManagerMixin):
49+
class Lock(_ContextManagerMixin, _LoopBoundMixin):
4850
if sys.version_info >= (3, 10):
4951
def __init__(self) -> None: ...
5052
else:
@@ -54,7 +56,7 @@ class Lock(_ContextManagerMixin):
5456
async def acquire(self) -> Literal[True]: ...
5557
def release(self) -> None: ...
5658

57-
class Event:
59+
class Event(_LoopBoundMixin):
5860
if sys.version_info >= (3, 10):
5961
def __init__(self) -> None: ...
6062
else:
@@ -65,7 +67,7 @@ class Event:
6567
def clear(self) -> None: ...
6668
async def wait(self) -> Literal[True]: ...
6769

68-
class Condition(_ContextManagerMixin):
70+
class Condition(_ContextManagerMixin, _LoopBoundMixin):
6971
if sys.version_info >= (3, 10):
7072
def __init__(self, lock: Lock | None = None) -> None: ...
7173
else:
@@ -79,7 +81,7 @@ class Condition(_ContextManagerMixin):
7981
def notify(self, n: int = 1) -> None: ...
8082
def notify_all(self) -> None: ...
8183

82-
class Semaphore(_ContextManagerMixin):
84+
class Semaphore(_ContextManagerMixin, _LoopBoundMixin):
8385
_value: int
8486
_waiters: deque[Future[Any]]
8587
if sys.version_info >= (3, 10):

mypy/typeshed/stdlib/asyncio/queues.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ from typing import Any, Generic, TypeVar
55
if sys.version_info >= (3, 9):
66
from types import GenericAlias
77

8+
if sys.version_info >= (3, 10):
9+
from .mixins import _LoopBoundMixin
10+
else:
11+
_LoopBoundMixin = object
12+
813
__all__ = ("Queue", "PriorityQueue", "LifoQueue", "QueueFull", "QueueEmpty")
914

1015
class QueueEmpty(Exception): ...
1116
class QueueFull(Exception): ...
1217

1318
_T = TypeVar("_T")
1419

15-
class Queue(Generic[_T]):
20+
# If Generic[_T] is last and _LoopBoundMixin is object, pyright is unhappy.
21+
# We can remove the noqa pragma when dropping 3.9 support.
22+
class Queue(Generic[_T], _LoopBoundMixin): # noqa: Y059
1623
if sys.version_info >= (3, 10):
1724
def __init__(self, maxsize: int = 0) -> None: ...
1825
else:

0 commit comments

Comments
 (0)