Skip to content

Commit 785da1a

Browse files
anconguiancongui
andauthored
chore: clean up ruff findings from #23 / #24 / #25 (#26)
* chore: clean up ruff findings introduced by #23 / #24 / #25 The Lint check failed on all three merged PRs because the new files tripped ``F401`` (unused ``typing.Any``), ``SIM105`` (replace ``try``/``except``/``pass`` with ``contextlib.suppress``), ``UP041`` (replace ``asyncio.TimeoutError`` with builtin ``TimeoutError``), ``I001`` (import ordering), and ``F841`` (unused local). The other CI jobs (Unit tests, SDK Python, SDK Java, Typecheck, Docling) were all green on each PR; the merges weren't gated on Lint. This is the follow-up sweep so ``ruff check`` is clean on ``main``. 11 errors fixed (8 auto-fixed by ``ruff --fix``, 3 manual). * chore: apply ``ruff format`` to the same files The Lint job runs both ``ruff check`` and ``ruff format --check``. The previous commit cleared the ``check`` half; this one runs ``ruff format`` over the 8 files in the same change set so the formatter half passes too. No behaviour change. --------- Co-authored-by: ancongui <andres.contreras@soon.es>
1 parent 50ba95e commit 785da1a

11 files changed

Lines changed: 21 additions & 59 deletions

src/flydocs/core/services/jobs/cancel_job_handler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,4 @@ async def do_handle(self, command: CancelJobCommand) -> JobStatusResponse | None
6060
job = await self._repository.get(command.job_id)
6161
if job is None:
6262
return None
63-
raise JobNotCancellable(
64-
f"Job {job.id!r} cannot be cancelled in status {job.status}"
65-
)
63+
raise JobNotCancellable(f"Job {job.id!r} cannot be cancelled in status {job.status}")

src/flydocs/core/services/workers/bbox_reaper.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
from __future__ import annotations
1919

2020
import asyncio
21+
import contextlib
2122
import logging
2223
import socket
23-
from typing import Any
2424

2525
from pyfly.eda import EventPublisher
2626

@@ -65,13 +65,11 @@ async def run_forever(self) -> None:
6565
await self._sweep()
6666
except Exception: # noqa: BLE001
6767
logger.exception("BboxReaper sweep failed; will retry next interval")
68-
try:
68+
with contextlib.suppress(TimeoutError):
6969
await asyncio.wait_for(
7070
self._stop.wait(),
7171
timeout=max(1, self._settings.reaper_sweep_interval_s),
7272
)
73-
except asyncio.TimeoutError:
74-
pass
7573

7674
def stop(self) -> None:
7775
self._stop.set()

src/flydocs/core/services/workers/job_reaper.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
from __future__ import annotations
2727

2828
import asyncio
29+
import contextlib
2930
import logging
3031
import socket
31-
from typing import Any
3232

3333
from pyfly.eda import EventPublisher
3434

@@ -70,13 +70,11 @@ async def run_forever(self) -> None:
7070
await self._sweep()
7171
except Exception: # noqa: BLE001
7272
logger.exception("JobReaper sweep failed; will retry next interval")
73-
try:
73+
with contextlib.suppress(TimeoutError):
7474
await asyncio.wait_for(
7575
self._stop.wait(),
7676
timeout=max(1, self._settings.reaper_sweep_interval_s),
7777
)
78-
except asyncio.TimeoutError:
79-
pass
8078

8179
def stop(self) -> None:
8280
self._stop.set()

src/flydocs/core/services/workers/job_worker.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ async def _process(self, job_id: str) -> None:
151151
# (or stale-RUNNING) job. ``None`` means another worker beat us
152152
# to it or the job was cancelled between our ``get`` and this
153153
# claim -- both are silent no-ops.
154-
claimed = await self._repository.mark_running(
155-
job.id, lease_seconds=self._settings.job_run_lease_s
156-
)
154+
claimed = await self._repository.mark_running(job.id, lease_seconds=self._settings.job_run_lease_s)
157155
if claimed is None:
158156
logger.info(
159157
"Job %s could not be claimed -- already owned by another worker or "
@@ -206,20 +204,15 @@ async def _process(self, job_id: str) -> None:
206204
# the bboxes change between PARTIAL_SUCCEEDED and SUCCEEDED.
207205
terminal_status = JobStatus.PARTIAL_SUCCEEDED if wants_bbox_refine else JobStatus.SUCCEEDED
208206
if wants_bbox_refine:
209-
finalised = await self._repository.mark_partial_succeeded(
210-
job.id, result=result_payload
211-
)
207+
finalised = await self._repository.mark_partial_succeeded(job.id, result=result_payload)
212208
else:
213-
finalised = await self._repository.mark_succeeded(
214-
job.id, result=result_payload
215-
)
209+
finalised = await self._repository.mark_succeeded(job.id, result=result_payload)
216210
if finalised is None:
217211
# Another worker (or the bbox leg) already advanced the
218212
# row past RUNNING. Our work is duplicate -- don't fire
219213
# the webhook a second time, don't republish.
220214
logger.info(
221-
"Job %s already finalised by another worker -- "
222-
"discarding our duplicate result",
215+
"Job %s already finalised by another worker -- discarding our duplicate result",
223216
job.id,
224217
)
225218
return
@@ -283,9 +276,7 @@ async def _process(self, job_id: str) -> None:
283276
)
284277

285278
if terminal:
286-
failed = await self._repository.mark_failed(
287-
job.id, code=error_code, message=str(exc)
288-
)
279+
failed = await self._repository.mark_failed(job.id, code=error_code, message=str(exc))
289280
if failed is None:
290281
logger.info(
291282
"Job %s no longer in RUNNING -- another worker handled the "
@@ -323,8 +314,7 @@ async def _process(self, job_id: str) -> None:
323314
requeued = await self._repository.requeue_for_retry(job.id)
324315
if requeued is None:
325316
logger.info(
326-
"Job %s not requeueable (status changed under us) -- "
327-
"skipping retry publish",
317+
"Job %s not requeueable (status changed under us) -- skipping retry publish",
328318
job.id,
329319
)
330320
else:

src/flydocs/models/repositories/extraction_job_repository.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ async def find_stale_queued(
169169
func.coalesce(
170170
ExtractionJob.started_at,
171171
ExtractionJob.created_at,
172-
) < cutoff,
172+
)
173+
< cutoff,
173174
)
174175
.limit(limit)
175176
)

tests/integration/test_eda_advisory_lock.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import os
1515

1616
import pytest
17-
1817
from pyfly.eda.adapters.postgres import PostgresEventBus
1918

2019
_PG_URL = os.environ.get("FLYDOCS_TEST_PG_URL")
@@ -72,7 +71,6 @@ async def handler_b(envelope) -> None:
7271
await bus_b.start()
7372
try:
7473
# Publish 20 events; each must be delivered exactly once.
75-
published_ids: list[str] = []
7674
for i in range(20):
7775
# Use bus_a as the producer; events land in the shared outbox.
7876
await bus_a.publish(

tests/integration/test_postgres_concurrency.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ async def test_postgres_atomic_claim_single_winner(pg_repo: ExtractionJobReposit
6969
seeded = await _seed(pg_repo)
7070

7171
n = 8
72-
results = await asyncio.gather(
73-
*(pg_repo.mark_running(seeded.id, lease_seconds=300) for _ in range(n))
74-
)
72+
results = await asyncio.gather(*(pg_repo.mark_running(seeded.id, lease_seconds=300) for _ in range(n)))
7573
winners = [r for r in results if r is not None]
7674
assert len(winners) == 1, f"expected 1 winner, got {len(winners)} (lost-update?!)"
7775
assert winners[0].attempts == 1

tests/integration/test_reaper_postgres.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import os
2424
from datetime import UTC, datetime, timedelta
25-
from typing import Any
2625
from unittest.mock import AsyncMock, MagicMock
2726

2827
import pytest
@@ -128,9 +127,7 @@ async def test_job_reaper_revives_all_three_job_orphan_classes(
128127

129128
await reaper._sweep()
130129

131-
published_ids = [
132-
c.kwargs["payload"]["job_id"] for c in publisher.publish.await_args_list
133-
]
130+
published_ids = [c.kwargs["payload"]["job_id"] for c in publisher.publish.await_args_list]
134131
assert submit_orphan.id in published_ids
135132
assert crashed_runner.id in published_ids
136133
assert retry_orphan.id in published_ids
@@ -176,9 +173,7 @@ async def test_bbox_reaper_revives_both_bbox_orphan_classes(
176173

177174
await reaper._sweep()
178175

179-
published_ids = [
180-
c.kwargs["payload"]["job_id"] for c in publisher.publish.await_args_list
181-
]
176+
published_ids = [c.kwargs["payload"]["job_id"] for c in publisher.publish.await_args_list]
182177
assert publish_orphan.id in published_ids
183178
assert crashed_bbox.id in published_ids
184179
assert fresh.id not in published_ids

tests/unit/test_reapers.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import asyncio
1414
from datetime import UTC, datetime, timedelta
15-
from typing import Any
1615
from unittest.mock import AsyncMock, MagicMock
1716

1817
import pytest
@@ -224,10 +223,7 @@ async def test_job_reaper_republishes_stale_running_and_queued() -> None:
224223

225224
await reaper._sweep()
226225

227-
published_ids = [
228-
call.kwargs["payload"]["job_id"]
229-
for call in publisher.publish.await_args_list
230-
]
226+
published_ids = [call.kwargs["payload"]["job_id"] for call in publisher.publish.await_args_list]
231227
assert stale_running.id in published_ids
232228
assert orphan_queued.id in published_ids
233229
assert fresh_running.id not in published_ids
@@ -272,10 +268,7 @@ async def test_bbox_reaper_republishes_stale_refining_and_pending() -> None:
272268

273269
await reaper._sweep()
274270

275-
published_ids = [
276-
call.kwargs["payload"]["job_id"]
277-
for call in publisher.publish.await_args_list
278-
]
271+
published_ids = [call.kwargs["payload"]["job_id"] for call in publisher.publish.await_args_list]
279272
assert stale_refining.id in published_ids
280273
assert pending_orphan.id in published_ids
281274
assert fresh_refining.id not in published_ids

tests/unit/test_submit_job_handler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ async def _get_by_key(key: str):
202202
],
203203
docs=[_doc_spec()],
204204
)
205-
response = await handler.do_handle(
206-
SubmitJobCommand(request=request, idempotency_key="dupe-key")
207-
)
205+
response = await handler.do_handle(SubmitJobCommand(request=request, idempotency_key="dupe-key"))
208206

209207
assert response.job_id == "winner-job-id"
210208
assert response.status is JobStatus.QUEUED

0 commit comments

Comments
 (0)