Skip to content

Commit 71b2b28

Browse files
committed
fix(cleanup-task): ruff/mypy strict-Blocker beheben
- asyncio.TimeoutError → TimeoutError (built-in, py312) in stop() und _loop() - logger.warning-Zeile auf 4 Zeilen aufgeteilt (> 100 Zeichen) - unbenutztes `import asyncio` aus test_cleanup_task.py entfernt - -> None Return-Type auf alle async def test_*-Funktionen ergänzt - Single-Use-Hinweis in CleanupTask-Docstring ergänzt Refs #93
1 parent 3c7a27d commit 71b2b28

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

backend/app/services/cleanup_task.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class CleanupTask:
2525
2626
Der Loop führt beim Start sofort einen ersten Run durch, danach in ``interval``-Abständen.
2727
Exceptions in _run_once werden geloggt und der Loop läuft weiter (fail-soft).
28+
29+
Single-Use: nach stop() kann die Instanz nicht restartet werden.
30+
Lifespan startet eine neue Instanz beim App-Startup.
2831
"""
2932

3033
def __init__(
@@ -56,9 +59,12 @@ async def stop(self, timeout_s: float = 5.0) -> None:
5659
if self._task is not None:
5760
try:
5861
await asyncio.wait_for(self._task, timeout=timeout_s)
59-
except asyncio.TimeoutError:
62+
except TimeoutError:
6063
self._task.cancel()
61-
logger.warning("CleanupTask hat sich nicht in %ss beendet, Task gecancelled", timeout_s)
64+
logger.warning(
65+
"CleanupTask hat sich nicht in %ss beendet, Task gecancelled",
66+
timeout_s,
67+
)
6268
self._task = None
6369

6470
async def _loop(self) -> None:
@@ -70,7 +76,7 @@ async def _loop(self) -> None:
7076
self._stopping.wait(),
7177
timeout=self._interval.total_seconds(),
7278
)
73-
except asyncio.TimeoutError:
79+
except TimeoutError:
7480
await self._run_once()
7581

7682
async def _run_once(self) -> None:

backend/tests/unit/services/test_cleanup_task.py

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

33
from __future__ import annotations
44

5-
import asyncio
65
from datetime import timedelta
76
from unittest.mock import AsyncMock
87

@@ -11,14 +10,14 @@
1110

1211

1312
@pytest.mark.asyncio
14-
async def test_cleanup_validates_retention_days():
13+
async def test_cleanup_validates_retention_days() -> None:
1514
store = AsyncMock()
1615
with pytest.raises(ValueError, match="retention_days must be >= 1"):
1716
CleanupTask(store=store, retention_days=0)
1817

1918

2019
@pytest.mark.asyncio
21-
async def test_cleanup_initial_run_on_start():
20+
async def test_cleanup_initial_run_on_start() -> None:
2221
store = AsyncMock()
2322
store.evict_terminal_older_than.return_value = 3
2423
task = CleanupTask(store=store, retention_days=30, interval=timedelta(seconds=99))
@@ -33,7 +32,7 @@ async def test_cleanup_initial_run_on_start():
3332

3433

3534
@pytest.mark.asyncio
36-
async def test_cleanup_fail_soft_on_exception():
35+
async def test_cleanup_fail_soft_on_exception() -> None:
3736
store = AsyncMock()
3837
store.evict_terminal_older_than.side_effect = RuntimeError("boom")
3938
task = CleanupTask(store=store, retention_days=30, interval=timedelta(seconds=99))

0 commit comments

Comments
 (0)