Skip to content

Commit 125e65b

Browse files
committed
Stub fixes.
1 parent 393efcd commit 125e65b

8 files changed

Lines changed: 268 additions & 36 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ email = "s3riussan@gmail.com"
2525
[dependency-groups]
2626
dev = [
2727
"anyio>=4,<5",
28+
"mypy>=1.19.1,<2",
2829
"pytest>=9,<10",
2930
"pytest-xdist>=3,<4",
3031
]

python/natsrpy/_natsrpy_rs/__init__.pyi

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
from collections.abc import Awaitable, Callable
22
from datetime import timedelta
3-
from typing import Any, overload
3+
from typing import Any, final, overload
44

55
from natsrpy._natsrpy_rs.js import JetStream
66
from natsrpy._natsrpy_rs.message import Message
7+
from typing_extensions import Self
78

9+
@final
810
class IteratorSubscription:
911
def __aiter__(self) -> IteratorSubscription: ...
1012
async def __anext__(self) -> Message: ...
1113
async def next(self, timeout: float | timedelta | None = None) -> Message: ...
1214
async def unsubscribe(self, limit: int | None = None) -> None: ...
1315
async def drain(self) -> None: ...
1416

17+
@final
1518
class CallbackSubscription:
1619
async def unsubscribe(self, limit: int | None = None) -> None: ...
1720
async def drain(self) -> None: ...
1821

22+
@final
1923
class Nats:
20-
def __init__(
21-
self,
24+
def __new__(
25+
cls,
2226
/,
2327
addrs: list[str] = ["nats://localhost:4222"],
2428
user_and_pass: tuple[str, str] | None = None,
@@ -30,7 +34,7 @@ class Nats:
3034
max_reconnects: int | None = None,
3135
connection_timeout: float | timedelta = ...,
3236
request_timeout: float | timedelta = ...,
33-
) -> None: ...
37+
) -> Self: ...
3438
async def startup(self) -> None: ...
3539
async def shutdown(self) -> None: ...
3640
async def publish(

python/natsrpy/_natsrpy_rs/js/consumers.pyi

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import timedelta
22

33
from natsrpy._natsrpy_rs.js import JetStreamMessage
4+
from typing_extensions import Self
45

56
class DeliverPolicy:
67
ALL: DeliverPolicy
@@ -55,8 +56,8 @@ class PullConsumerConfig:
5556
priority_groups: list[str]
5657
pause_until: int | None
5758

58-
def __init__(
59-
self,
59+
def __new__(
60+
cls,
6061
name: str | None = None,
6162
durable_name: str | None = None,
6263
description: str | None = None,
@@ -85,7 +86,7 @@ class PullConsumerConfig:
8586
priority_policy: PriorityPolicy | None = None,
8687
priority_groups: list[str] | None = None,
8788
pause_until: int | None = None,
88-
) -> None: ...
89+
) -> Self: ...
8990

9091
class PushConsumerConfig:
9192
deliver_subject: str
@@ -116,8 +117,8 @@ class PushConsumerConfig:
116117
inactive_threshold: timedelta
117118
pause_until: int | None
118119

119-
def __init__(
120-
self,
120+
def __new__(
121+
cls,
121122
deliver_subject: str,
122123
name: str | None = None,
123124
durable_name: str | None = None,
@@ -145,7 +146,7 @@ class PushConsumerConfig:
145146
backoff: list[timedelta] | None = None,
146147
inactive_threshold: timedelta | None = None,
147148
pause_until: int | None = None,
148-
) -> None: ...
149+
) -> Self: ...
149150

150151
class MessagesIterator:
151152
def __aiter__(self) -> MessagesIterator: ...

python/natsrpy/_natsrpy_rs/js/kv.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from natsrpy._natsrpy_rs.js.stream import Placement, Republish, Source, StorageType
2+
from typing_extensions import Self
23

34
class KVConfig:
45
"""
@@ -23,8 +24,8 @@ class KVConfig:
2324
placement: Placement | None
2425
limit_markers: float | None
2526

26-
def __init__(
27-
self,
27+
def __new__(
28+
cls,
2829
bucket: str,
2930
description: str | None = None,
3031
max_value_size: int | None = None,
@@ -40,7 +41,7 @@ class KVConfig:
4041
compression: bool | None = None,
4142
placement: Placement | None = None,
4243
limit_markers: float | None = None,
43-
) -> None: ...
44+
) -> Self: ...
4445

4546
class KeyValue:
4647
@property

python/natsrpy/_natsrpy_rs/js/object_store.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import timedelta
22

3-
from typing_extensions import Writer
3+
from typing_extensions import Self, Writer
44

55
from .stream import Placement, StorageType
66

@@ -14,8 +14,8 @@ class ObjectStoreConfig:
1414
compression: bool
1515
placement: Placement | None
1616

17-
def __init__(
18-
self,
17+
def __new__(
18+
cls,
1919
bucket: str,
2020
description: str | None = None,
2121
max_age: float | timedelta | None = None,
@@ -24,7 +24,7 @@ class ObjectStoreConfig:
2424
num_replicas: int | None = None,
2525
compression: bool | None = None,
2626
placement: Placement | None = None,
27-
) -> None: ...
27+
) -> Self: ...
2828

2929
class ObjectStore:
3030
async def get(

python/natsrpy/_natsrpy_rs/js/stream.pyi

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from datetime import datetime, timedelta
22
from typing import Any
33

4+
from typing_extensions import Self
5+
46
from .managers import ConsumersManager
57

68
class StorageType:
@@ -28,27 +30,31 @@ class ConsumerLimits:
2830
inactive_threshold: timedelta
2931
max_ack_pending: int
3032

31-
def __init__(self, inactive_threshold: timedelta, max_ack_pending: int) -> None: ...
33+
def __new__(
34+
cls,
35+
inactive_threshold: timedelta,
36+
max_ack_pending: int,
37+
) -> Self: ...
3238

3339
class External:
3440
api_prefix: str
3541
delivery_prefix: str | None
3642

37-
def __init__(
38-
self,
43+
def __new__(
44+
cls,
3945
api_prefix: str,
4046
delivery_prefix: str | None = None,
41-
) -> None: ...
47+
) -> Self: ...
4248

4349
class SubjectTransform:
4450
source: str
4551
destination: str
4652

47-
def __init__(
48-
self,
53+
def __new__(
54+
cls,
4955
source: str,
5056
destination: str,
51-
) -> None: ...
57+
) -> Self: ...
5258

5359
class Source:
5460
name: str
@@ -59,38 +65,38 @@ class Source:
5965
domain: str | None = None
6066
subject_transforms: SubjectTransform | None = None
6167

62-
def __init__(
63-
self,
68+
def __new__(
69+
cls,
6470
name: str,
6571
filter_subject: str | None = None,
6672
external: External | None = None,
6773
start_sequence: int | None = None,
6874
start_time: int | None = None,
6975
domain: str | None = None,
7076
subject_transforms: SubjectTransform | None = None,
71-
) -> None: ...
77+
) -> Self: ...
7278

7379
class Placement:
7480
cluster: str | None
7581
tags: list[str] | None
7682

77-
def __init__(
78-
self,
83+
def __new__(
84+
cls,
7985
cluster: str | None = None,
8086
tags: list[str] | None = None,
81-
) -> None: ...
87+
) -> Self: ...
8288

8389
class Republish:
8490
source: str
8591
destination: str
8692
headers_only: bool
8793

88-
def __init__(
89-
self,
94+
def __new__(
95+
cls,
9096
source: str,
9197
destination: str,
9298
headers_only: bool,
93-
) -> None: ...
99+
) -> Self: ...
94100

95101
class StreamConfig:
96102
name: str
@@ -133,8 +139,8 @@ class StreamConfig:
133139
allow_message_schedules: bool | None
134140
allow_message_counter: bool | None
135141

136-
def __init__(
137-
self,
142+
def __new__(
143+
cls,
138144
name: str,
139145
subjects: list[str],
140146
max_bytes: int | None = None,
@@ -174,7 +180,7 @@ class StreamConfig:
174180
allow_atomic_publish: bool | None = None,
175181
allow_message_schedules: bool | None = None,
176182
allow_message_counter: bool | None = None,
177-
) -> None: ...
183+
) -> Self: ...
178184

179185
class StreamMessage:
180186
subject: str

test.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import asyncio
2+
import logging
3+
from datetime import timedelta
4+
5+
from natsrpy import Nats, Message
6+
from natsrpy.js import PullConsumerConfig, StreamConfig
7+
8+
logging.basicConfig(level=logging.DEBUG)
9+
10+
11+
async def do_things(nats: Nats) -> None:
12+
"""Do things."""
13+
js = await nats.jetstream()
14+
stream = await js.streams.create_or_update(
15+
StreamConfig(
16+
subjects=["test.one", "test.two"],
17+
name="test-stream",
18+
),
19+
)
20+
await js.publish("test.one", b"Hello lib!")
21+
22+
consumer = await stream.consumers.create(
23+
PullConsumerConfig(
24+
name="test-cons",
25+
durable_name="test-cons",
26+
),
27+
)
28+
while True:
29+
for message in await consumer.fetch():
30+
print(
31+
message.subject,
32+
message.reply,
33+
message.payload,
34+
message.headers,
35+
message.domain,
36+
message.acc_hash,
37+
message.stream,
38+
message.consumer,
39+
message.stream_sequence,
40+
message.consumer_sequence,
41+
message.delivered,
42+
message.pending,
43+
message.published,
44+
message.token,
45+
sep="||",
46+
)
47+
await message.ack()
48+
49+
await asyncio.Future()
50+
51+
52+
async def main() -> None:
53+
"""We do logic here."""
54+
nats = Nats(
55+
addrs=["nats://localhost:4222"],
56+
connection_timeout=timedelta(seconds=1),
57+
request_timeout=timedelta(seconds=3),
58+
)
59+
await nats.startup()
60+
61+
try:
62+
await do_things(nats)
63+
finally:
64+
await nats.shutdown()
65+
66+
67+
if __name__ == "__main__":
68+
asyncio.run(main())

0 commit comments

Comments
 (0)