Skip to content

Commit 9d59dcd

Browse files
Copilots3rius
andcommitted
Add docstrings to all type stubs in python/natsrpy
Co-authored-by: s3rius <18153319+s3rius@users.noreply.github.com> Agent-Logs-Url: https://github.com/taskiq-python/natsrpy/sessions/ea16012c-d53a-4a4b-b494-c9dec5f0ab4b
1 parent c00d269 commit 9d59dcd

File tree

8 files changed

+1093
-131
lines changed

8 files changed

+1093
-131
lines changed

python/natsrpy/_natsrpy_rs/__init__.pyi

Lines changed: 146 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,67 @@ class Message:
3434

3535
@final
3636
class IteratorSubscription:
37+
"""Async iterator subscription for receiving NATS messages.
38+
39+
Returned by :meth:`Nats.subscribe` when no callback is provided.
40+
Messages can be received using ``async for`` or by calling :meth:`next`
41+
directly.
42+
"""
43+
3744
def __aiter__(self) -> IteratorSubscription: ...
3845
async def __anext__(self) -> Message: ...
39-
async def next(self, timeout: float | timedelta | None = None) -> Message: ...
40-
async def unsubscribe(self, limit: int | None = None) -> None: ...
41-
async def drain(self) -> None: ...
46+
async def next(self, timeout: float | timedelta | None = None) -> Message:
47+
"""Receive the next message from the subscription.
48+
49+
:param timeout: maximum time to wait for a message in seconds
50+
or as a timedelta, defaults to None (wait indefinitely).
51+
:return: the next message.
52+
:raises StopAsyncIteration: when the subscription is drained or
53+
unsubscribed.
54+
"""
55+
56+
async def unsubscribe(self, limit: int | None = None) -> None:
57+
"""Unsubscribe from the subject.
58+
59+
:param limit: if set, automatically unsubscribe after receiving
60+
this many additional messages, defaults to None.
61+
"""
62+
63+
async def drain(self) -> None:
64+
"""Drain the subscription.
65+
66+
Unsubscribes and flushes any remaining messages before closing.
67+
"""
4268

4369
@final
4470
class CallbackSubscription:
45-
async def unsubscribe(self, limit: int | None = None) -> None: ...
46-
async def drain(self) -> None: ...
71+
"""Callback-based subscription for receiving NATS messages.
72+
73+
Returned by :meth:`Nats.subscribe` when a callback is provided.
74+
Messages are automatically delivered to the callback in a background task.
75+
"""
76+
77+
async def unsubscribe(self, limit: int | None = None) -> None:
78+
"""Unsubscribe from the subject.
79+
80+
:param limit: if set, automatically unsubscribe after receiving
81+
this many additional messages, defaults to None.
82+
"""
83+
84+
async def drain(self) -> None:
85+
"""Drain the subscription.
86+
87+
Unsubscribes and flushes any remaining messages before closing.
88+
"""
4789

4890
@final
4991
class Nats:
92+
"""NATS client.
93+
94+
Provides publish/subscribe messaging, request-reply, and JetStream
95+
access over a connection to one or more NATS servers.
96+
"""
97+
5098
def __new__(
5199
cls,
52100
/,
@@ -60,9 +108,45 @@ class Nats:
60108
max_reconnects: int | None = None,
61109
connection_timeout: float | timedelta = ..., # 5 sec
62110
request_timeout: float | timedelta = ..., # 10 sec
63-
) -> Self: ...
64-
async def startup(self) -> None: ...
65-
async def shutdown(self) -> None: ...
111+
) -> Self:
112+
"""Create a new NATS client instance.
113+
114+
The client is not connected until :meth:`startup` is called.
115+
116+
:param addrs: list of NATS server URLs, defaults to
117+
``["nats://localhost:4222"]``.
118+
:param user_and_pass: username and password tuple for authentication.
119+
:param nkey: NKey seed for authentication.
120+
:param token: token string for authentication.
121+
:param custom_inbox_prefix: custom prefix for auto-generated inbox
122+
subjects.
123+
:param read_buffer_capacity: size of the read buffer in bytes,
124+
defaults to 65535.
125+
:param sender_capacity: capacity of the internal send channel,
126+
defaults to 128.
127+
:param max_reconnects: maximum number of reconnection attempts,
128+
None means unlimited.
129+
:param connection_timeout: timeout for establishing a connection
130+
in seconds or as a timedelta, defaults to 5 seconds.
131+
:param request_timeout: default timeout for request-reply operations
132+
in seconds or as a timedelta, defaults to 10 seconds.
133+
"""
134+
135+
async def startup(self) -> None:
136+
"""Connect to the NATS server.
137+
138+
Establishes the connection using the parameters provided at
139+
construction time. Must be called before any publish, subscribe,
140+
or JetStream operations.
141+
"""
142+
143+
async def shutdown(self) -> None:
144+
"""Close the NATS connection.
145+
146+
Drains all subscriptions and flushes pending data before
147+
disconnecting.
148+
"""
149+
66150
async def publish(
67151
self,
68152
subject: str,
@@ -71,7 +155,18 @@ class Nats:
71155
headers: dict[str, Any] | None = None,
72156
reply: str | None = None,
73157
err_on_disconnect: bool = False,
74-
) -> None: ...
158+
) -> None:
159+
"""Publish a message to a subject.
160+
161+
:param subject: subject to publish the message to.
162+
:param payload: message payload.
163+
:param headers: optional NATS headers dictionary.
164+
:param reply: optional reply-to subject for the request-reply
165+
pattern.
166+
:param err_on_disconnect: when True, raise an error if the client
167+
is disconnected, defaults to False.
168+
"""
169+
75170
async def request(
76171
self,
77172
subject: str,
@@ -80,9 +175,30 @@ class Nats:
80175
headers: dict[str, Any] | None = None,
81176
inbox: str | None = None,
82177
timeout: float | timedelta | None = None,
83-
) -> None: ...
84-
async def drain(self) -> None: ...
85-
async def flush(self) -> None: ...
178+
) -> None:
179+
"""Send a request and discard the response.
180+
181+
:param subject: subject to send the request to.
182+
:param payload: request payload.
183+
:param headers: optional NATS headers dictionary.
184+
:param inbox: custom inbox subject for the reply, auto-generated
185+
if None.
186+
:param timeout: maximum time to wait for a response in seconds
187+
or as a timedelta, defaults to the client request_timeout.
188+
"""
189+
190+
async def drain(self) -> None:
191+
"""Drain the connection.
192+
193+
Gracefully closes all subscriptions and flushes pending messages.
194+
"""
195+
196+
async def flush(self) -> None:
197+
"""Flush the connection.
198+
199+
Waits until all pending messages have been sent to the server.
200+
"""
201+
86202
@overload
87203
async def subscribe(
88204
self,
@@ -105,7 +221,24 @@ class Nats:
105221
concurrency_limit: int | None = None,
106222
max_ack_inflight: int | None = None,
107223
backpressure_on_inflight: bool | None = None,
108-
) -> js.JetStream: ...
224+
) -> js.JetStream:
225+
"""Create a JetStream context.
226+
227+
:param domain: JetStream domain to use.
228+
:param api_prefix: custom JetStream API prefix, cannot be used
229+
together with *domain*.
230+
:param timeout: default request timeout for JetStream operations
231+
in seconds or as a timedelta.
232+
:param ack_timeout: acknowledgement timeout for consumers in seconds
233+
or as a timedelta.
234+
:param concurrency_limit: maximum number of concurrent JetStream
235+
operations.
236+
:param max_ack_inflight: maximum number of unacknowledged messages
237+
in flight.
238+
:param backpressure_on_inflight: when True, apply backpressure when
239+
the in-flight limit is reached.
240+
:return: a JetStream context.
241+
"""
109242

110243
__all__ = [
111244
"CallbackSubscription",

python/natsrpy/_natsrpy_rs/exceptions.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
class NatsrpyBaseError(Exception): ...
2-
class NatsrpySessionError(NatsrpyBaseError): ...
3-
class NatsrpyPublishError(NatsrpyBaseError): ...
1+
class NatsrpyBaseError(Exception):
2+
"""Base exception for all natsrpy errors."""
3+
4+
class NatsrpySessionError(NatsrpyBaseError):
5+
"""Raised on connection or session-level errors."""
6+
7+
class NatsrpyPublishError(NatsrpyBaseError):
8+
"""Raised when a publish operation fails."""
49

510
__all__ = [
611
"NatsrpyBaseError",

0 commit comments

Comments
 (0)