Skip to content

Commit 4464188

Browse files
authored
[pika] Complete merging stubs (#16081)
1 parent 8f77d7c commit 4464188

12 files changed

Lines changed: 383 additions & 318 deletions

pyrightconfig.stricter.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@
7373
"stubs/parsimonious/parsimonious/nodes.pyi",
7474
"stubs/peewee",
7575
"stubs/pexpect",
76-
"stubs/pika",
76+
"stubs/pika/pika/adapters/twisted_connection.pyi",
77+
"stubs/pika/pika/adapters/utils/connection_workflow.pyi",
78+
"stubs/pika/pika/callback.pyi",
79+
"stubs/pika/pika/channel.pyi",
7780
"stubs/pony",
7881
"stubs/protobuf",
7982
"stubs/psutil/psutil/__init__.pyi",

stubs/pika/@tests/stubtest_allowlist.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# bytes alias for Python 3 compatibility
2+
pika.spec.str
3+
14
# Behind a TYPE_CHECKING guard at runtime.
25
pika.adapters.select_connection.SELECT_ERROR_T
36
pika.adapters.select_connection.POLLER_PARAMS

stubs/pika/pika/adapters/base_connection.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22
from _typeshed import Incomplete
3-
from collections.abc import Callable, Mapping, Sequence
3+
from collections.abc import Callable, Sequence
44
from logging import Logger
55
from typing import Final, Generic, Literal, TypeVar
66
from typing_extensions import Self
@@ -62,8 +62,8 @@ class _StreamingProtocolShim(AbstractStreamProtocol, Generic[_IOLoop]):
6262
connection_state: Literal[0, 1, 2, 3, 4, 5, 6] # one of the constants above
6363
params: Parameters
6464
callbacks: CallbackManager
65-
server_capabilities: Mapping[str, bool] | None
66-
server_properties: Mapping[str, Incomplete] | None
65+
server_capabilities: dict[str, bool] | None
66+
server_properties: dict[str, Incomplete] | None
6767
known_hosts: str | None
6868
def add_on_close_callback(self, callback: Callable[[Self, BaseException], object]) -> None: ...
6969
def add_on_connection_blocked_callback(self, callback: Callable[[Self, Method[SpecConnection.Blocked]], object]) -> None: ...

stubs/pika/pika/adapters/twisted_connection.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class _TwistedConnectionAdapter(Connection):
138138
self,
139139
parameters: Parameters | None,
140140
on_open_callback: Callable[[Connection], object] | None,
141-
on_open_error_callback: Callable[[Connection, Exception], object] | None,
141+
on_open_error_callback: Callable[[Connection, BaseException], object] | None,
142142
on_close_callback: Callable[[Connection, Exception], object] | None,
143143
custom_reactor: ReactorBase | None = None,
144144
) -> None: ...

stubs/pika/pika/callback.pyi

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
from collections.abc import Callable
1+
from _typeshed import Incomplete
2+
from collections.abc import Callable, Mapping
23
from logging import Logger
3-
from typing import Literal, TypeAlias
4+
from typing import Any, Final, Literal, ParamSpec, TypeAlias, TypeVar
45

56
from .amqp_object import AMQPObject
67

8+
_P = ParamSpec("_P")
9+
_R = TypeVar("_R")
710
AMQPValue: TypeAlias = type[AMQPObject] | AMQPObject | int | str
811

912
LOGGER: Logger
1013

1114
def name_or_value(value: AMQPValue) -> str: ...
12-
def sanitize_prefix(function): ...
13-
def check_for_prefix_and_key(function): ...
15+
def sanitize_prefix(function: Callable[_P, _R]) -> Callable[_P, _R]: ...
16+
def check_for_prefix_and_key(function: Callable[_P, _R]) -> Callable[_P, _R | Literal[False]]: ...
1417

1518
class CallbackManager:
16-
CALLS: str
17-
ARGUMENTS: str
18-
DUPLICATE_WARNING: str
19-
CALLBACK: str
20-
ONE_SHOT: str
21-
ONLY_CALLER: str
19+
CALLS: Final = "calls"
20+
ARGUMENTS: Final = "arguments"
21+
DUPLICATE_WARNING: Final = 'Duplicate callback found for "%s:%s"'
22+
CALLBACK: Final = "callback"
23+
ONE_SHOT: Final = "one_shot"
24+
ONLY_CALLER: Final = "only"
2225
def __init__(self) -> None: ...
2326
def add(
2427
self,
@@ -28,13 +31,24 @@ class CallbackManager:
2831
callback: Callable[..., object],
2932
one_shot: bool = True,
3033
only_caller: object | None = None,
31-
arguments=None,
34+
arguments: Mapping[str, Incomplete] | None = None,
3235
) -> tuple[str | int, str | object]: ...
3336
def clear(self) -> None: ...
3437
def cleanup(self, prefix: str | int) -> bool: ...
35-
def pending(self, prefix: str | int, key: str | object) -> int | None: ...
36-
def process(self, prefix: str | int, key: AMQPValue, caller, *args, **keywords) -> bool: ...
38+
def pending(self, prefix: str | int, key: AMQPValue) -> int | None: ...
39+
def process(
40+
self,
41+
prefix: str | int,
42+
key: AMQPValue,
43+
caller,
44+
*args: Any, # Arguments depends on callbacks stored on self._stack
45+
**keywords: Any,
46+
) -> bool: ...
3747
def remove(
38-
self, prefix: str | int, key: AMQPValue, callback_value: Callable[..., object] | None = None, arguments=None
39-
) -> Literal[True]: ...
40-
def remove_all(self, prefix: str | int, key: AMQPValue) -> None: ...
48+
self,
49+
prefix: str | int,
50+
key: AMQPValue,
51+
callback_value: Callable[..., object] | None = None,
52+
arguments: Mapping[str, Incomplete] | None = None,
53+
) -> bool: ...
54+
def remove_all(self, prefix: str | int, key: AMQPValue) -> Literal[False] | None: ...

stubs/pika/pika/channel.pyi

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
from _typeshed import Incomplete
2-
from collections.abc import Callable
2+
from collections.abc import Callable, Iterable, Mapping
33
from logging import Logger
4-
from typing import Any, Final
4+
from typing import Final, TypeVar
55
from typing_extensions import Self
66

7+
from . import amqp_object
78
from .callback import CallbackManager
89
from .connection import Connection
9-
from .data import _ArgumentMapping
1010
from .exchange_type import ExchangeType
1111
from .frame import Body, Header, Method
1212
from .spec import Basic, BasicProperties, Confirm, Exchange, Queue, Tx
1313

14+
_Method = TypeVar("_Method", bound=amqp_object.Method)
15+
1416
LOGGER: Logger
15-
MAX_CHANNELS: Final[int]
17+
MAX_CHANNELS: Final = 65535
1618

1719
class Channel:
1820
CLOSED: Final = 0
@@ -27,11 +29,11 @@ class Channel:
2729

2830
def __init__(self, connection: Connection, channel_number: int, on_open_callback: Callable[[Self], object]) -> None: ...
2931
def __int__(self) -> int: ...
30-
def add_callback(self, callback, replies, one_shot: bool = True) -> None: ...
31-
def add_on_cancel_callback(self, callback) -> None: ...
32-
def add_on_close_callback(self, callback) -> None: ...
33-
def add_on_flow_callback(self, callback) -> None: ...
34-
def add_on_return_callback(self, callback) -> None: ...
32+
def add_callback(self, callback: Callable[..., object], replies: Iterable[Incomplete], one_shot: bool = True) -> None: ...
33+
def add_on_cancel_callback(self, callback: Callable[[Method[Basic.Cancel]], object]) -> None: ...
34+
def add_on_close_callback(self, callback: Callable[[Channel, Exception], object]) -> None: ...
35+
def add_on_flow_callback(self, callback: Callable[[bool], object]) -> None: ...
36+
def add_on_return_callback(self, callback: Callable[[Channel, Basic.Return, BasicProperties, bytes], object]) -> None: ...
3537
def basic_ack(self, delivery_tag: int = 0, multiple: bool = False) -> None: ...
3638
def basic_cancel(
3739
self, consumer_tag: str = "", callback: Callable[[Method[Basic.CancelOk]], object] | None = None
@@ -43,7 +45,7 @@ class Channel:
4345
auto_ack: bool = False,
4446
exclusive: bool = False,
4547
consumer_tag: str | None = None,
46-
arguments: _ArgumentMapping | None = None,
48+
arguments: Mapping[str, Incomplete] | None = None,
4749
callback: Callable[[Method[Basic.ConsumeOk]], object] | None = None,
4850
) -> str: ...
4951
def basic_get(
@@ -82,7 +84,7 @@ class Channel:
8284
destination: str,
8385
source: str,
8486
routing_key: str = "",
85-
arguments: _ArgumentMapping | None = None,
87+
arguments: Mapping[str, Incomplete] | None = None,
8688
callback: Callable[[Method[Exchange.BindOk]], object] | None = None,
8789
) -> None: ...
8890
def exchange_declare(
@@ -93,7 +95,7 @@ class Channel:
9395
durable: bool = False,
9496
auto_delete: bool = False,
9597
internal: bool = False,
96-
arguments: _ArgumentMapping | None = None,
98+
arguments: Mapping[str, Incomplete] | None = None,
9799
callback: Callable[[Method[Exchange.DeclareOk]], object] | None = None,
98100
) -> None: ...
99101
def exchange_delete(
@@ -107,7 +109,7 @@ class Channel:
107109
destination: str | None = None,
108110
source: str | None = None,
109111
routing_key: str = "",
110-
arguments: _ArgumentMapping | None = None,
112+
arguments: Mapping[str, Incomplete] | None = None,
111113
callback: Callable[[Method[Exchange.UnbindOk]], object] | None = None,
112114
) -> None: ...
113115
def flow(self, active: bool, callback: Callable[[bool], object] | None = None) -> None: ...
@@ -125,7 +127,7 @@ class Channel:
125127
queue: str,
126128
exchange: str,
127129
routing_key: str | None = None,
128-
arguments: _ArgumentMapping | None = None,
130+
arguments: Mapping[str, Incomplete] | None = None,
129131
callback: Callable[[Method[Queue.BindOk]], object] | None = None,
130132
) -> None: ...
131133
def queue_declare(
@@ -135,7 +137,7 @@ class Channel:
135137
durable: bool = False,
136138
exclusive: bool = False,
137139
auto_delete: bool = False,
138-
arguments: _ArgumentMapping | None = None,
140+
arguments: Mapping[str, Incomplete] | None = None,
139141
callback: Callable[[Method[Queue.DeclareOk]], object] | None = None,
140142
) -> None: ...
141143
def queue_delete(
@@ -151,7 +153,7 @@ class Channel:
151153
queue: str,
152154
exchange: str | None = None,
153155
routing_key: str | None = None,
154-
arguments: _ArgumentMapping | None = None,
156+
arguments: Mapping[str, Incomplete] | None = None,
155157
callback: Callable[[Method[Queue.UnbindOk]], object] | None = None,
156158
): ...
157159
def tx_commit(self, callback: Callable[[Method[Tx.CommitOk]], object] | None = None) -> None: ...
@@ -160,4 +162,4 @@ class Channel:
160162

161163
class ContentFrameAssembler:
162164
def __init__(self) -> None: ...
163-
def process(self, frame_value: Method[Any] | Header | Body) -> tuple[Incomplete, Incomplete, bytes] | None: ...
165+
def process(self, frame_value: Method[_Method] | Header | Body) -> tuple[Method[_Method], Header, bytes] | None: ...

stubs/pika/pika/connection.pyi

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import ssl
33
from _typeshed import Incomplete
44
from collections.abc import Callable
55
from logging import Logger
6-
from typing import Final
6+
from typing import Final, Literal
77
from typing_extensions import Self
88

99
from .callback import CallbackManager
1010
from .channel import Channel
1111
from .compat import AbstractBase
12-
from .credentials import _Credentials
12+
from .credentials import PlainCredentials, _Credentials
1313
from .frame import Method
1414
from .spec import Connection as SpecConnection
1515

16-
PRODUCT: str
16+
PRODUCT: Final = "Pika Python Client Library"
1717
LOGGER: Logger
1818

1919
class Parameters:
@@ -35,26 +35,26 @@ class Parameters:
3535
"_virtual_host",
3636
"_tcp_options",
3737
)
38-
DEFAULT_USERNAME: str
39-
DEFAULT_PASSWORD: str
40-
DEFAULT_BLOCKED_CONNECTION_TIMEOUT: None
41-
DEFAULT_CHANNEL_MAX: int
42-
DEFAULT_CLIENT_PROPERTIES: None
43-
DEFAULT_CREDENTIALS: Incomplete
44-
DEFAULT_CONNECTION_ATTEMPTS: int
45-
DEFAULT_FRAME_MAX: int
38+
DEFAULT_USERNAME: Final = "guest"
39+
DEFAULT_PASSWORD: Final = "guest"
40+
DEFAULT_BLOCKED_CONNECTION_TIMEOUT: Final = None
41+
DEFAULT_CHANNEL_MAX: Final = 65535
42+
DEFAULT_CLIENT_PROPERTIES: Final = None
43+
DEFAULT_CREDENTIALS: Final[PlainCredentials]
44+
DEFAULT_CONNECTION_ATTEMPTS: Final = 1
45+
DEFAULT_FRAME_MAX: Final = 131072
4646
DEFAULT_HEARTBEAT_TIMEOUT: None
47-
DEFAULT_HOST: str
48-
DEFAULT_LOCALE: str
49-
DEFAULT_PORT: int
50-
DEFAULT_RETRY_DELAY: float
51-
DEFAULT_SOCKET_TIMEOUT: float
52-
DEFAULT_STACK_TIMEOUT: float
53-
DEFAULT_SSL: bool
54-
DEFAULT_SSL_OPTIONS: None
55-
DEFAULT_SSL_PORT: int
56-
DEFAULT_VIRTUAL_HOST: str
57-
DEFAULT_TCP_OPTIONS: None
47+
DEFAULT_HOST: Final = "localhost"
48+
DEFAULT_LOCALE: Final = "en_US"
49+
DEFAULT_PORT: Final = 5672
50+
DEFAULT_RETRY_DELAY: Final = 2.0
51+
DEFAULT_SOCKET_TIMEOUT: Final = 10.0
52+
DEFAULT_STACK_TIMEOUT: Final = 15.0
53+
DEFAULT_SSL: Final = False
54+
DEFAULT_SSL_OPTIONS: Final = None
55+
DEFAULT_SSL_PORT: Final = 5671
56+
DEFAULT_VIRTUAL_HOST: Final = "/"
57+
DEFAULT_TCP_OPTIONS: Final = None
5858
def __init__(self) -> None: ...
5959
def __eq__(self, other: object) -> bool: ...
6060
def __ne__(self, other: object) -> bool: ...
@@ -135,13 +135,12 @@ class Parameters:
135135
def virtual_host(self, value: str) -> None: ...
136136

137137
@property
138-
def tcp_options(self) -> dict[Incomplete, Incomplete] | None: ...
138+
def tcp_options(self) -> dict[str, Incomplete] | None: ...
139139
@tcp_options.setter
140-
def tcp_options(self, value: dict[Incomplete, Incomplete] | None) -> None: ...
140+
def tcp_options(self, value: dict[str, Incomplete] | None) -> None: ...
141141

142142
class ConnectionParameters(Parameters):
143143
__slots__ = ()
144-
145144
def __init__(
146145
self,
147146
host: str = ...,
@@ -158,8 +157,8 @@ class ConnectionParameters(Parameters):
158157
stack_timeout: float | None = ...,
159158
locale: str = ...,
160159
blocked_connection_timeout: float | None = ...,
161-
client_properties: dict[Incomplete, Incomplete] | None = ...,
162-
tcp_options: dict[Incomplete, Incomplete] | None = ...,
160+
client_properties: dict[str, Incomplete] | None = ...,
161+
tcp_options: dict[str, Incomplete] | None = ...,
163162
) -> None: ...
164163

165164
class URLParameters(Parameters):
@@ -173,22 +172,22 @@ class SSLOptions:
173172
def __init__(self, context: ssl.SSLContext, server_hostname: str | None = None) -> None: ...
174173

175174
class Connection(AbstractBase, metaclass=abc.ABCMeta):
176-
ON_CONNECTION_CLOSED: Final[str]
177-
ON_CONNECTION_ERROR: Final[str]
178-
ON_CONNECTION_OPEN_OK: Final[str]
179-
CONNECTION_CLOSED: Final[int]
180-
CONNECTION_INIT: Final[int]
181-
CONNECTION_PROTOCOL: Final[int]
182-
CONNECTION_START: Final[int]
183-
CONNECTION_TUNE: Final[int]
184-
CONNECTION_OPEN: Final[int]
185-
CONNECTION_CLOSING: Final[int]
186-
connection_state: int # one of the constants above
175+
ON_CONNECTION_CLOSED: Final = "_on_connection_closed"
176+
ON_CONNECTION_ERROR: Final = "_on_connection_error"
177+
ON_CONNECTION_OPEN_OK: Final = "_on_connection_open_ok"
178+
CONNECTION_CLOSED: Final = 0
179+
CONNECTION_INIT: Final = 1
180+
CONNECTION_PROTOCOL: Final = 2
181+
CONNECTION_START: Final = 3
182+
CONNECTION_TUNE: Final = 4
183+
CONNECTION_OPEN: Final = 5
184+
CONNECTION_CLOSING: Final = 6
185+
connection_state: Literal[0, 1, 2, 3, 4, 5, 6] # one of the constants above
187186
params: Parameters
188187
callbacks: CallbackManager
189-
server_capabilities: Incomplete
190-
server_properties: Incomplete
191-
known_hosts: Incomplete
188+
server_capabilities: dict[str, bool] | None
189+
server_properties: dict[str, Incomplete] | None
190+
known_hosts: str | None
192191
def __init__(
193192
self,
194193
parameters: Parameters | None = None,
@@ -209,7 +208,12 @@ class Connection(AbstractBase, metaclass=abc.ABCMeta):
209208
def channel(
210209
self, channel_number: int | None = None, on_open_callback: Callable[[Channel], object] | None = None
211210
) -> Channel: ...
212-
def update_secret(self, new_secret, reason, callback=None) -> None: ...
211+
def update_secret(
212+
self,
213+
new_secret: str | bytes,
214+
reason: str | bytes,
215+
callback: Callable[[Method[SpecConnection.UpdateSecretOk]], object] | None = None,
216+
) -> None: ...
213217
def close(self, reply_code: int = 200, reply_text: str = "Normal shutdown") -> None: ...
214218
@property
215219
def is_closed(self) -> bool: ...

stubs/pika/pika/data.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ _Value: TypeAlias = str | bytes | bool | int | Decimal | datetime | _ArgumentMap
77
_ArgumentMapping: TypeAlias = Mapping[str, _Value]
88

99
def encode_short_string(pieces: list[bytes], value: str | bytes) -> int: ...
10-
def decode_short_string(encoded: bytes, offset: int) -> tuple[str, int]: ...
10+
def decode_short_string(encoded: bytes, offset: int) -> tuple[str | bytes, int]: ...
1111
def encode_table(pieces: list[bytes], table: _ArgumentMapping) -> int: ...
1212
def encode_value(pieces: list[bytes], value: _Value) -> int: ...
13-
def decode_table(encoded: bytes, offset: int) -> tuple[dict[str, _Value], int]: ...
13+
def decode_table(encoded: bytes, offset: int) -> tuple[dict[str | bytes, _Value], int]: ...
1414
def decode_value(encoded: bytes, offset: int) -> tuple[_Value, int]: ...

0 commit comments

Comments
 (0)