Skip to content

Commit 9ad7970

Browse files
authored
fix: cap fastapi below 0.140 and adopt ruff 0.16.0 (#153)
fastapi 0.140.0 made Dependant a slotted dataclass with no `model` field. faststream's FastAPI integration assigns `dependant.model`, so every FastStream-in-FastAPI route raised AttributeError (ag2ai/faststream#2959). faststream declares no fastapi dependency of its own, so the `fastapi` extra here is the only place the pairing can be constrained; cap it in both the extra and the dev group. ruff 0.16.0 stabilized CPY001 out of preview and started flagging ISC004 and an unused BLE001 noqa. Ignore CPY001 repo-wide, matching modern-di; take the two autofixes. 0.16.0 also formats Python code blocks inside Markdown; reformat the 17 affected files.
1 parent 13dbd9b commit 9ad7970

20 files changed

Lines changed: 178 additions & 121 deletions

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ engine = create_async_engine("postgresql+asyncpg://localhost/app")
8282
broker = OutboxBroker(engine, outbox_table=outbox_table)
8383
app = FastStream(broker)
8484

85+
8586
@broker.subscriber("orders", max_workers=4)
8687
async def handle(order_id: int) -> None:
8788
print(f"order {order_id}")
8889

90+
8991
# Producer side — share the caller's open transaction:
9092
session_factory = async_sessionmaker(engine, expire_on_commit=False)
9193
async with session_factory() as session, session.begin():

‎docs/introduction/how-it-works.md‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ transaction — the row must commit with the caller's domain writes:
3636

3737
```python
3838
async with session_factory() as session, session.begin():
39-
session.add(order) # domain write
39+
session.add(order) # domain write
4040
await broker.publish(order.id, queue="orders", session=session)
4141
# session.begin() commits both atomically on exit
4242
```
@@ -127,8 +127,7 @@ immune to worker / DB clock skew.
127127
When the invariant fires, the broker emits a WARNING with structured fields:
128128

129129
```python
130-
extra={"event": "lease_lost", "phase": "terminal" | "retry",
131-
"row_id": ..., "queue": ..., "deliveries_count": ...}
130+
extra = {"event": "lease_lost", "phase": "terminal" | "retry", "row_id": ..., "queue": ..., "deliveries_count": ...}
132131
```
133132

134133
Recurring `event=lease_lost` records mean `lease_ttl_seconds < handler P99`

‎docs/operations/alembic.md‎

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,41 @@ against an empty schema):
1616

1717
```python
1818
# ### commands auto generated by Alembic - please adjust! ###
19-
op.create_table('outbox',
20-
sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
21-
sa.Column('queue', sa.String(length=255), nullable=False),
22-
sa.Column('payload', sa.LargeBinary(), nullable=False),
23-
sa.Column('headers', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
24-
sa.Column('attempts_count', sa.BigInteger(), server_default='0', nullable=False),
25-
sa.Column('deliveries_count', sa.BigInteger(), server_default='0', nullable=False),
26-
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
27-
sa.Column('next_attempt_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
28-
sa.Column('first_attempt_at', sa.DateTime(timezone=True), nullable=True),
29-
sa.Column('last_attempt_at', sa.DateTime(timezone=True), nullable=True),
30-
sa.Column('acquired_at', sa.DateTime(timezone=True), nullable=True),
31-
sa.Column('acquired_token', sa.Uuid(), nullable=True),
32-
sa.Column('timer_id', sa.String(length=255), nullable=True),
33-
sa.CheckConstraint('(acquired_token IS NULL) = (acquired_at IS NULL)', name='outbox_lease_ck'),
34-
sa.PrimaryKeyConstraint('id')
19+
op.create_table(
20+
"outbox",
21+
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
22+
sa.Column("queue", sa.String(length=255), nullable=False),
23+
sa.Column("payload", sa.LargeBinary(), nullable=False),
24+
sa.Column("headers", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
25+
sa.Column("attempts_count", sa.BigInteger(), server_default="0", nullable=False),
26+
sa.Column("deliveries_count", sa.BigInteger(), server_default="0", nullable=False),
27+
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
28+
sa.Column("next_attempt_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
29+
sa.Column("first_attempt_at", sa.DateTime(timezone=True), nullable=True),
30+
sa.Column("last_attempt_at", sa.DateTime(timezone=True), nullable=True),
31+
sa.Column("acquired_at", sa.DateTime(timezone=True), nullable=True),
32+
sa.Column("acquired_token", sa.Uuid(), nullable=True),
33+
sa.Column("timer_id", sa.String(length=255), nullable=True),
34+
sa.CheckConstraint("(acquired_token IS NULL) = (acquired_at IS NULL)", name="outbox_lease_ck"),
35+
sa.PrimaryKeyConstraint("id"),
36+
)
37+
op.create_index(
38+
"outbox_lease_idx",
39+
"outbox",
40+
["queue", "acquired_at"],
41+
unique=False,
42+
postgresql_where=sa.text("acquired_token IS NOT NULL"),
43+
)
44+
op.create_index(
45+
"outbox_pending_idx",
46+
"outbox",
47+
["queue", "next_attempt_at"],
48+
unique=False,
49+
postgresql_where=sa.text("acquired_token IS NULL"),
50+
)
51+
op.create_index(
52+
"outbox_timer_id_uq", "outbox", ["queue", "timer_id"], unique=True, postgresql_where=sa.text("timer_id IS NOT NULL")
3553
)
36-
op.create_index('outbox_lease_idx', 'outbox', ['queue', 'acquired_at'], unique=False,
37-
postgresql_where=sa.text('acquired_token IS NOT NULL'))
38-
op.create_index('outbox_pending_idx', 'outbox', ['queue', 'next_attempt_at'], unique=False,
39-
postgresql_where=sa.text('acquired_token IS NULL'))
40-
op.create_index('outbox_timer_id_uq', 'outbox', ['queue', 'timer_id'], unique=True,
41-
postgresql_where=sa.text('timer_id IS NOT NULL'))
4254
# ### end Alembic commands ###
4355
```
4456

@@ -89,21 +101,22 @@ The captured `upgrade()` body:
89101

90102
```python
91103
# ### commands auto generated by Alembic - please adjust! ###
92-
op.create_table('outbox_dlq',
93-
sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
94-
sa.Column('original_id', sa.BigInteger(), nullable=False),
95-
sa.Column('queue', sa.String(length=255), nullable=False),
96-
sa.Column('payload', sa.LargeBinary(), nullable=False),
97-
sa.Column('headers', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
98-
sa.Column('deliveries_count', sa.BigInteger(), nullable=False),
99-
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
100-
sa.Column('failed_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
101-
sa.Column('failure_reason', sa.String(length=64), nullable=False),
102-
sa.Column('last_exception', sa.String(), nullable=True),
103-
sa.Column('timer_id', sa.String(length=255), nullable=True),
104-
sa.PrimaryKeyConstraint('id')
104+
op.create_table(
105+
"outbox_dlq",
106+
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
107+
sa.Column("original_id", sa.BigInteger(), nullable=False),
108+
sa.Column("queue", sa.String(length=255), nullable=False),
109+
sa.Column("payload", sa.LargeBinary(), nullable=False),
110+
sa.Column("headers", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
111+
sa.Column("deliveries_count", sa.BigInteger(), nullable=False),
112+
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
113+
sa.Column("failed_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
114+
sa.Column("failure_reason", sa.String(length=64), nullable=False),
115+
sa.Column("last_exception", sa.String(), nullable=True),
116+
sa.Column("timer_id", sa.String(length=255), nullable=True),
117+
sa.PrimaryKeyConstraint("id"),
105118
)
106-
op.create_index('outbox_dlq_queue_failed_idx', 'outbox_dlq', ['queue', 'failed_at'], unique=False)
119+
op.create_index("outbox_dlq_queue_failed_idx", "outbox_dlq", ["queue", "failed_at"], unique=False)
107120
# ### end Alembic commands ###
108121
```
109122

@@ -268,11 +281,11 @@ pointer to this section. Re-running autogenerate produces an empty
268281
```python
269282
# Drop first ONLY if the constraint exists with a wrong predicate; skip the
270283
# drop if it is absent entirely.
271-
op.drop_constraint('outbox_lease_ck', 'outbox', type_='check')
284+
op.drop_constraint("outbox_lease_ck", "outbox", type_="check")
272285
op.create_check_constraint(
273-
'outbox_lease_ck',
274-
'outbox',
275-
'(acquired_token IS NULL) = (acquired_at IS NULL)',
286+
"outbox_lease_ck",
287+
"outbox",
288+
"(acquired_token IS NULL) = (acquired_at IS NULL)",
276289
)
277290
```
278291

@@ -288,13 +301,13 @@ three indexes and their expected shape:
288301
| `outbox_timer_id_uq` | `queue, timer_id` | yes | `timer_id IS NOT NULL` |
289302

290303
```python
291-
op.drop_index('outbox_timer_id_uq', table_name='outbox')
304+
op.drop_index("outbox_timer_id_uq", table_name="outbox")
292305
op.create_index(
293-
'outbox_timer_id_uq',
294-
'outbox',
295-
['queue', 'timer_id'],
306+
"outbox_timer_id_uq",
307+
"outbox",
308+
["queue", "timer_id"],
296309
unique=True,
297-
postgresql_where=sa.text('timer_id IS NOT NULL'),
310+
postgresql_where=sa.text("timer_id IS NOT NULL"),
298311
)
299312
```
300313

@@ -339,7 +352,7 @@ faster, no vacuum debt.
339352
# ### Convert outbox_dlq to range-partitioned by failed_at ###
340353

341354
# Rename the existing table out of the way.
342-
op.rename_table('outbox_dlq', 'outbox_dlq_old')
355+
op.rename_table("outbox_dlq", "outbox_dlq_old")
343356

344357
# Create the partitioned parent. The PRIMARY KEY must include the
345358
# partition key, so we extend it to (id, failed_at).
@@ -381,7 +394,7 @@ op.execute("""
381394
WHERE failed_at >= '2026-05-01';
382395
""")
383396

384-
op.drop_table('outbox_dlq_old')
397+
op.drop_table("outbox_dlq_old")
385398
```
386399

387400
`validate_schema()` continues to work against the partitioned table —

‎docs/usage/dlq.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,10 @@ transform (or drop) the stored text:
129129

130130
```python
131131
# Store only the exception class — no message, no payload.
132-
broker = OutboxBroker(engine, outbox_table=t, dlq_table=dlq,
133-
last_exception_renderer=lambda exc: type(exc).__name__)
132+
broker = OutboxBroker(engine, outbox_table=t, dlq_table=dlq, last_exception_renderer=lambda exc: type(exc).__name__)
134133

135134
# Or drop the detail entirely:
136-
broker = OutboxBroker(engine, outbox_table=t, dlq_table=dlq,
137-
last_exception_renderer=lambda exc: None)
135+
broker = OutboxBroker(engine, outbox_table=t, dlq_table=dlq, last_exception_renderer=lambda exc: None)
138136
```
139137

140138
The renderer runs per terminal failure; its output is still length-capped

‎docs/usage/fastapi.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ router = OutboxRouter(engine, outbox_table=outbox_table)
6363
async def handle(
6464
body: dict,
6565
session: AsyncSession = Depends(get_session),
66-
) -> None:
67-
... # domain writes on `session` commit with any chained outbox publishes
66+
) -> None: ... # domain writes on `session` commit with any chained outbox publishes
6867

6968

7069
@router.post("/orders")
@@ -116,8 +115,7 @@ async def handle(
116115
msg: OutboxMessage,
117116
broker: OutboxBroker,
118117
session: AsyncSession = Depends(get_session),
119-
) -> None:
120-
...
118+
) -> None: ...
121119
```
122120

123121
They resolve via FastStream's `Context()` paths but go through FastAPI's

‎docs/usage/messaging-service.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ class OutboxEventProducer:
101101

102102
async def send_chat_event(self, event: ChatEvent) -> None:
103103
await self.outbox_broker.publish(
104-
event, queue="chat-events", session=self.session,
104+
event,
105+
queue="chat-events",
106+
session=self.session,
105107
)
106108
```
107109

@@ -250,7 +252,11 @@ from faststream_outbox import TestOutboxBroker
250252

251253

252254
async def test_create_message_relays_event_to_kafka(
253-
outbox_broker, kafka_broker, create_message_use_case, command, kafka_publisher,
255+
outbox_broker,
256+
kafka_broker,
257+
create_message_use_case,
258+
command,
259+
kafka_publisher,
254260
) -> None:
255261
async with TestOutboxBroker(outbox_broker), TestKafkaBroker(kafka_broker):
256262
await create_message_use_case(command)

‎docs/usage/publisher.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ raises `NotImplementedError`.
2222

2323
```python
2424
async with session_factory() as session, session.begin():
25-
session.add(order) # domain write
25+
session.add(order) # domain write
2626
await broker.publish(
2727
{"order_id": order.id},
2828
queue="orders",
@@ -97,9 +97,9 @@ orders_pub = broker.publisher("orders", headers={"source": "checkout"})
9797

9898

9999
async def checkout(order: Order, session: AsyncSession) -> None:
100-
session.add(order) # domain write
100+
session.add(order) # domain write
101101
await orders_pub.publish({"order_id": order.id}, session=session)
102-
await session.commit() # row + domain commit together
102+
await session.commit() # row + domain commit together
103103
```
104104

105105
Per-call `headers` are merged with the publisher's static headers

‎docs/usage/relay.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ are **not** forwarded to the foreign publish. Two ways to override:
131131
from faststream.response import Response
132132
from faststream_outbox.annotations import OutboxMessage
133133

134+
134135
@publisher_kafka
135136
@broker_outbox.subscriber("outbox_queue")
136137
async def relay(body: dict, msg: OutboxMessage) -> Response:

‎docs/usage/subscriber.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ reclaim of *actually* stuck rows everywhere. Instead, segregate slow work
124124
onto its own subscriber with a longer TTL:
125125

126126
```python
127-
@broker.subscriber("slow_q", lease_ttl_seconds=600) # 10 minutes
127+
@broker.subscriber("slow_q", lease_ttl_seconds=600) # 10 minutes
128128
async def heavy_job(msg): ...
129129

130130

@@ -235,7 +235,7 @@ async def handle(msg: OutboxMessage, body: dict) -> None:
235235
await write_audit(body)
236236
await msg.ack()
237237
except TransientError:
238-
await msg.nack() # retry
238+
await msg.nack() # retry
239239
except PermanentError:
240240
await msg.reject() # terminal delete
241241
```

‎docs/usage/testing.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ tb = TestOutboxBroker(broker, run_loops=True)
124124
async with tb:
125125
tb.feed("orders", json.dumps({"order_id": 1}).encode())
126126
# the real fetch + worker loops pick the row up asynchronously
127-
async with asyncio.timeout(1.0): # fail fast instead of hanging forever
127+
async with asyncio.timeout(1.0): # fail fast instead of hanging forever
128128
while not received:
129129
await asyncio.sleep(0.01)
130130

0 commit comments

Comments
 (0)