Skip to content

Commit aea4ebe

Browse files
asg017claude
andcommitted
Parse task config JSON in _row_to_task so the dict annotation is true
CronTask.config was annotated `dict` but held the raw JSON string straight from SQLite, so every consumer had to defend itself: _execute_task and _task_to_response each carried an `isinstance(..., str)` branch, and tests asserted through json.loads(task.config). The annotation being a lie also meant ty could not check any subscripting of task.config. _row_to_task already coerces `enabled` to bool precisely so the dataclass reflects reality; config now gets the same treatment. The parse is safe unguarded: the column is TEXT NOT NULL DEFAULT '{}' and both write paths (upsert_task, update_task) json.dumps the value, so no row holds NULL. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 61db518 commit aea4ebe

4 files changed

Lines changed: 9 additions & 12 deletions

File tree

‎datasette_cron/internal_db.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ def _row_to_task(self, row) -> CronTask:
124124
# SQLite stores enabled as INTEGER 0/1; coerce to bool at the
125125
# boundary so the dataclass annotation reflects reality.
126126
d["enabled"] = bool(d["enabled"])
127+
# Likewise config: the column is TEXT NOT NULL DEFAULT '{}' and every
128+
# write goes through upsert_task/update_task, which json.dumps it -- so
129+
# parse it back here and CronTask.config really is the dict it claims.
130+
d["config"] = json.loads(d["config"])
127131
return CronTask(**d)
128132

129133
def _row_to_run(self, row) -> CronRun:

‎datasette_cron/routes/api.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
from dataclasses import asdict
32
from typing import Annotated
43

@@ -54,13 +53,9 @@ class EnableRequest(BaseModel):
5453

5554

5655
def _task_to_response(task: CronTask) -> dict:
57-
config = task.config
58-
if isinstance(config, str):
59-
config = json.loads(config)
60-
6156
return TaskResponse(
6257
**task_to_summary(task).model_dump(),
63-
config=config,
58+
config=task.config,
6459
schedule_config=task.schedule_config,
6560
overlap_policy=task.overlap_policy,
6661
retry_max=task.retry_max,

‎datasette_cron/scheduler.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,7 @@ async def _execute_task(
443443
now: datetime | None = None,
444444
) -> None:
445445
name = task.name
446-
config = (
447-
json.loads(task.config) if isinstance(task.config, str) else task.config
448-
)
446+
config = task.config
449447
max_attempts = task.retry_max + 1
450448
backoff_strategy = task.retry_backoff
451449
plugin, _, _ = task.handler.partition(":")

‎tests/test_cron.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async def noop(datasette, config):
5050
assert task.name == "my-task"
5151
assert task.handler == "test:noop"
5252
assert task.schedule_type == "interval"
53-
assert json.loads(task.config) == {"key": "value"}
53+
assert task.config == {"key": "value"}
5454
assert task.enabled is True
5555
assert task.next_run_at is not None
5656
assert task.overlap_policy == "skip"
@@ -669,7 +669,7 @@ async def test_internal_db_update_task_config_serialized():
669669

670670
await idb.update_task("cfg-task", config={"x": 42})
671671
task = await idb.get_task("cfg-task")
672-
assert json.loads(task.config) == {"x": 42}
672+
assert task.config == {"x": 42}
673673

674674
await scheduler.shutdown()
675675

@@ -957,7 +957,7 @@ async def noop(datasette, config):
957957

958958
await scheduler.update_task("upd-task", config={"new": True}, overlap="cancel")
959959
task = await scheduler.internal_db.get_task("upd-task")
960-
assert json.loads(task.config) == {"new": True}
960+
assert task.config == {"new": True}
961961
assert task.overlap_policy == "cancel"
962962

963963
await scheduler.shutdown()

0 commit comments

Comments
 (0)