Skip to content

Commit 5d4298c

Browse files
authored
[pika] Complete adapters.utils (#16061)
1 parent f497b0b commit 5d4298c

5 files changed

Lines changed: 223 additions & 96 deletions

File tree

stubs/pika/pika/adapters/asyncio_connection.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class _AsyncioIOServicesAdapter(
4646
def call_later(self, delay: float, callback: Callable[[], object]) -> _TimerHandle: ...
4747
def getaddrinfo(
4848
self,
49-
host: str,
50-
port: int,
51-
on_done: Callable[..., object],
49+
host: str | bytes | None,
50+
port: str | bytes | int | None,
51+
on_done: Callable[[BaseConnection | BaseException], object], # type: ignore[override]
5252
family: int = 0,
5353
socktype: int = 0,
5454
proto: int = 0,
Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from _typeshed import Incomplete
2+
from collections.abc import Callable, Iterable, Sequence
23

34
import pika.compat
5+
import pika.connection
6+
from pika.adapters.utils.nbio_interface import AbstractIOServices, AbstractStreamProtocol
47

58
class AMQPConnectorException(Exception): ...
69
class AMQPConnectorStackTimeout(AMQPConnectorException): ...
710
class AMQPConnectorAborted(AMQPConnectorException): ...
811
class AMQPConnectorWrongState(AMQPConnectorException): ...
912

1013
class AMQPConnectorPhaseErrorBase(AMQPConnectorException):
11-
exception: Incomplete
12-
def __init__(self, exception, *args) -> None: ...
14+
exception: BaseException
15+
def __init__(self, exception: BaseException, *args: object) -> None: ...
1316

1417
class AMQPConnectorSocketConnectError(AMQPConnectorPhaseErrorBase): ...
1518
class AMQPConnectorTransportSetupError(AMQPConnectorPhaseErrorBase): ...
@@ -18,20 +21,41 @@ class AMQPConnectionWorkflowAborted(AMQPConnectorException): ...
1821
class AMQPConnectionWorkflowWrongState(AMQPConnectorException): ...
1922

2023
class AMQPConnectionWorkflowFailed(AMQPConnectorException):
21-
exceptions: Incomplete
22-
def __init__(self, exceptions, *args) -> None: ...
24+
exceptions: tuple[BaseException, ...]
25+
def __init__(self, exceptions: Iterable[BaseException], *args: object) -> None: ...
2326

2427
class AMQPConnector:
25-
def __init__(self, conn_factory, nbio) -> None: ...
26-
def start(self, addr_record, conn_params, on_done) -> None: ...
28+
def __init__(
29+
self, conn_factory: Callable[[pika.connection.Parameters], AbstractStreamProtocol], nbio: AbstractIOServices
30+
) -> None: ...
31+
def start(
32+
self,
33+
addr_record: tuple[ # tuple taken result of socket.getaddrinfo
34+
int, int, int, str, tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes]
35+
],
36+
conn_params: pika.connection.Parameters,
37+
on_done: Callable[[pika.connection.Connection | BaseException], None],
38+
) -> None: ...
2739
def abort(self) -> None: ...
2840

2941
class AbstractAMQPConnectionWorkflow(pika.compat.AbstractBase):
30-
def start(self, connection_configs, connector_factory, native_loop, on_done) -> None: ...
42+
def start(
43+
self,
44+
connection_configs: Sequence[pika.connection.Parameters],
45+
connector_factory: Callable[..., Incomplete],
46+
native_loop,
47+
on_done: Callable[[pika.connection.Connection | AMQPConnectorException], None],
48+
) -> None: ...
3149
def abort(self) -> None: ...
3250

3351
class AMQPConnectionWorkflow(AbstractAMQPConnectionWorkflow):
3452
def __init__(self, _until_first_amqp_attempt: bool = False) -> None: ...
35-
def set_io_services(self, nbio) -> None: ...
36-
def start(self, connection_configs, connector_factory, native_loop, on_done) -> None: ...
53+
def set_io_services(self, nbio: AbstractIOServices) -> None: ...
54+
def start(
55+
self,
56+
connection_configs: Sequence[pika.connection.Parameters],
57+
connector_factory: Callable[..., Incomplete],
58+
native_loop,
59+
on_done: Callable[[pika.connection.Connection | AMQPConnectorException], None],
60+
) -> None: ...
3761
def abort(self) -> None: ...
Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,89 @@
11
import abc
2+
from collections.abc import Callable
3+
from socket import socket
4+
from ssl import SSLContext, SSLSocket
5+
from typing import Any
26

3-
from pika.adapters.utils.nbio_interface import AbstractIOReference, AbstractStreamTransport
7+
from pika.adapters.utils.nbio_interface import (
8+
AbstractFileDescriptorServices,
9+
AbstractIOReference,
10+
AbstractIOServices,
11+
AbstractStreamProtocol,
12+
AbstractStreamTransport,
13+
)
14+
from pika.adapters.utils.selector_ioloop_adapter import _SupportsCancel
415

5-
def check_callback_arg(callback, name) -> None: ...
6-
def check_fd_arg(fd) -> None: ...
16+
def check_callback_arg(callback: Callable[..., Any], name: str) -> None: ...
17+
def check_fd_arg(fd: int) -> None: ...
718

819
class SocketConnectionMixin:
9-
def connect_socket(self, sock, resolved_addr, on_done): ...
20+
def connect_socket(
21+
self, sock: socket, resolved_addr: tuple[str, int], on_done: Callable[[BaseException | None], None]
22+
) -> _AsyncServiceAsyncHandle: ...
1023

1124
class StreamingConnectionMixin:
12-
def create_streaming_connection(self, protocol_factory, sock, on_done, ssl_context=None, server_hostname=None): ...
25+
def create_streaming_connection(
26+
self,
27+
protocol_factory: Callable[[], AbstractStreamProtocol],
28+
sock: socket,
29+
on_done: Callable[[tuple[AbstractStreamTransport, AbstractStreamProtocol] | BaseException], None],
30+
ssl_context: SSLContext | None = None,
31+
server_hostname: str | None = None,
32+
) -> AbstractIOReference: ...
1333

1434
class _AsyncServiceAsyncHandle(AbstractIOReference):
15-
def __init__(self, subject) -> None: ...
16-
def cancel(self): ...
35+
def __init__(self, subject: _SupportsCancel) -> None: ...
36+
def cancel(self) -> bool: ...
1737

1838
class _AsyncSocketConnector:
19-
def __init__(self, nbio, sock, resolved_addr, on_done) -> None: ...
20-
def start(self): ...
21-
def cancel(self): ...
39+
def __init__(
40+
self,
41+
nbio: AbstractIOServices | AbstractFileDescriptorServices,
42+
sock: socket,
43+
resolved_addr: tuple[str, int],
44+
on_done: Callable[[BaseException | None], None],
45+
) -> None: ...
46+
def start(self) -> AbstractIOReference: ...
47+
def cancel(self) -> bool: ...
2248

2349
class _AsyncStreamConnector:
24-
def __init__(self, nbio, protocol_factory, sock, ssl_context, server_hostname, on_done) -> None: ...
25-
def start(self): ...
26-
def cancel(self): ...
50+
def __init__(
51+
self,
52+
nbio: AbstractIOServices | AbstractFileDescriptorServices,
53+
protocol_factory: Callable[[], AbstractStreamProtocol],
54+
sock: socket,
55+
ssl_context: SSLContext,
56+
server_hostname: str | None,
57+
on_done: Callable[[tuple[AbstractStreamTransport, AbstractStreamProtocol] | BaseException], None],
58+
) -> None: ...
59+
def start(self) -> AbstractIOReference: ...
60+
def cancel(self) -> bool: ...
2761

2862
class _AsyncTransportBase(AbstractStreamTransport, metaclass=abc.ABCMeta):
2963
class RxEndOfFile(OSError):
3064
def __init__(self) -> None: ...
3165

32-
def __init__(self, sock, protocol, nbio) -> None: ...
66+
def __init__(
67+
self,
68+
sock: socket | SSLSocket,
69+
protocol: AbstractStreamProtocol,
70+
nbio: AbstractIOServices | AbstractFileDescriptorServices,
71+
) -> None: ...
3372
def abort(self) -> None: ...
34-
def get_protocol(self): ...
35-
def get_write_buffer_size(self): ...
73+
def get_protocol(self) -> AbstractStreamProtocol: ...
74+
def get_write_buffer_size(self) -> int: ...
3675

3776
class _AsyncPlaintextTransport(_AsyncTransportBase):
38-
def __init__(self, sock, protocol, nbio) -> None: ...
39-
def write(self, data) -> None: ...
77+
def __init__(
78+
self,
79+
sock: socket | SSLSocket,
80+
protocol: AbstractStreamProtocol,
81+
nbio: AbstractIOServices | AbstractFileDescriptorServices,
82+
) -> None: ...
83+
def write(self, data: bytes) -> None: ...
4084

4185
class _AsyncSSLTransport(_AsyncTransportBase):
42-
def __init__(self, sock, protocol, nbio) -> None: ...
43-
def write(self, data) -> None: ...
86+
def __init__(
87+
self, sock: SSLSocket, protocol: AbstractStreamProtocol, nbio: AbstractIOServices | AbstractFileDescriptorServices
88+
) -> None: ...
89+
def write(self, data: bytes) -> None: ...
Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,85 @@
11
import abc
2+
from collections.abc import Callable
3+
from socket import socket
4+
from ssl import SSLContext
25

36
import pika.compat
47

58
class AbstractIOServices(pika.compat.AbstractBase, metaclass=abc.ABCMeta):
69
@abc.abstractmethod
7-
def get_native_ioloop(self): ...
10+
def get_native_ioloop(self) -> object: ...
811
@abc.abstractmethod
9-
def close(self): ...
12+
def close(self) -> None: ...
1013
@abc.abstractmethod
11-
def run(self): ...
14+
def run(self) -> None: ...
1215
@abc.abstractmethod
13-
def stop(self): ...
16+
def stop(self) -> None: ...
1417
@abc.abstractmethod
15-
def add_callback_threadsafe(self, callback): ...
18+
def add_callback_threadsafe(self, callback: Callable[..., None]) -> None: ...
1619
@abc.abstractmethod
17-
def call_later(self, delay, callback): ...
20+
def call_later(self, delay: float, callback: Callable[..., None]) -> AbstractTimerReference: ...
1821
@abc.abstractmethod
19-
def getaddrinfo(self, host, port, on_done, family: int = 0, socktype: int = 0, proto: int = 0, flags: int = 0): ...
22+
def getaddrinfo(
23+
self,
24+
host: str | bytes | None,
25+
port: str | bytes | int | None,
26+
on_done: Callable[ # list is result of socket.getaddrinfo
27+
[list[tuple[int, int, int, str, tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes]]] | BaseException],
28+
None,
29+
],
30+
family: int = 0,
31+
socktype: int = 0,
32+
proto: int = 0,
33+
flags: int = 0,
34+
) -> AbstractIOReference: ...
2035
@abc.abstractmethod
21-
def connect_socket(self, sock, resolved_addr, on_done): ...
36+
def connect_socket(
37+
self, sock: socket, resolved_addr: tuple[str, int], on_done: Callable[[BaseException | None], None]
38+
) -> AbstractIOReference: ...
2239
@abc.abstractmethod
23-
def create_streaming_connection(self, protocol_factory, sock, on_done, ssl_context=None, server_hostname=None): ...
40+
def create_streaming_connection(
41+
self,
42+
protocol_factory: Callable[[], AbstractStreamProtocol],
43+
sock: socket,
44+
on_done: Callable[[tuple[AbstractStreamTransport, AbstractStreamProtocol] | BaseException], None],
45+
ssl_context: SSLContext | None = None,
46+
server_hostname: str | None = None,
47+
) -> AbstractIOReference: ...
2448

25-
class AbstractFileDescriptorServices(pika.compat.AbstractBase, metaclass=abc.ABCMeta):
49+
class AbstractFileDescriptorServices(pika.compat.AbstractBase):
2650
@abc.abstractmethod
27-
def set_reader(self, fd, on_readable): ...
51+
def set_reader(self, fd: int, on_readable: Callable[[], None]) -> None: ...
2852
@abc.abstractmethod
29-
def remove_reader(self, fd): ...
53+
def remove_reader(self, fd: int) -> bool: ...
3054
@abc.abstractmethod
31-
def set_writer(self, fd, on_writable): ...
55+
def set_writer(self, fd: int, on_writable: Callable[[], None]) -> None: ...
3256
@abc.abstractmethod
33-
def remove_writer(self, fd): ...
57+
def remove_writer(self, fd: int) -> bool: ...
3458

35-
class AbstractTimerReference(pika.compat.AbstractBase, metaclass=abc.ABCMeta):
59+
class AbstractTimerReference(pika.compat.AbstractBase):
3660
@abc.abstractmethod
37-
def cancel(self): ...
61+
def cancel(self) -> None: ...
3862

39-
class AbstractIOReference(pika.compat.AbstractBase, metaclass=abc.ABCMeta):
63+
class AbstractIOReference(pika.compat.AbstractBase):
4064
@abc.abstractmethod
41-
def cancel(self): ...
65+
def cancel(self) -> bool: ...
4266

43-
class AbstractStreamProtocol(pika.compat.AbstractBase, metaclass=abc.ABCMeta):
67+
class AbstractStreamProtocol(pika.compat.AbstractBase):
4468
@abc.abstractmethod
45-
def connection_made(self, transport): ...
69+
def connection_made(self, transport: AbstractStreamTransport) -> None: ...
4670
@abc.abstractmethod
47-
def connection_lost(self, error): ...
71+
def connection_lost(self, error: BaseException | None) -> None: ...
4872
@abc.abstractmethod
49-
def eof_received(self): ...
73+
def eof_received(self) -> bool | None: ...
5074
@abc.abstractmethod
51-
def data_received(self, data): ...
75+
def data_received(self, data: bytes) -> None: ...
5276

53-
class AbstractStreamTransport(pika.compat.AbstractBase, metaclass=abc.ABCMeta):
77+
class AbstractStreamTransport(pika.compat.AbstractBase):
5478
@abc.abstractmethod
55-
def abort(self): ...
79+
def abort(self) -> None: ...
5680
@abc.abstractmethod
57-
def get_protocol(self): ...
81+
def get_protocol(self) -> AbstractStreamProtocol: ...
5882
@abc.abstractmethod
59-
def write(self, data): ...
83+
def write(self, data: bytes) -> None: ...
6084
@abc.abstractmethod
61-
def get_write_buffer_size(self): ...
85+
def get_write_buffer_size(self) -> int: ...

0 commit comments

Comments
 (0)