From 1cc5dcff5469982a833b49baceb2706e4d4b0169 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 30 Nov 2026 11:39:49 +0100 Subject: [PATCH] Avoid queries that may return `None` The last `grid` of the loop already belongs to the `city`, and `.scalar()` returns the maximum or `None` without indexing a row --- src/velofy/console/forecasts.py | 4 ++-- src/velofy/console/gridify.py | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/velofy/console/forecasts.py b/src/velofy/console/forecasts.py index 75864e5..3a5ca60 100644 --- a/src/velofy/console/forecasts.py +++ b/src/velofy/console/forecasts.py @@ -112,8 +112,8 @@ def tactical_heuristic( # noqa:C901,D417,DOC001 .filter(db.Grid.side_length == side_length) .filter(db.Forecast.time_step == time_step) .filter(db.Forecast.train_horizon == train_horizon) - .first() - )[0] + .scalar() + ) # ... or start `train_horizon` weeks after the first `Order` # if no `Forecast`s are in the database yet. if last_predict_at is None: diff --git a/src/velofy/console/gridify.py b/src/velofy/console/gridify.py index 5107fd4..ea72bc5 100644 --- a/src/velofy/console/gridify.py +++ b/src/velofy/console/gridify.py @@ -35,8 +35,7 @@ def gridify() -> None: # pragma: no cover note:b1f68d24 click.echo(f" -> created {len(grid.pixels)} pixels") # Because the number of assigned addresses is the same across - # different `side_length`s, we can take any `grid` from the `city`. - grid = db.session.query(db.Grid).filter_by(city=city).first() + # different `side_length`s, we can take the last `grid` from the `city`. n_assigned = ( db.session.query(db.AddressPixelAssociation) .filter(db.AddressPixelAssociation.grid_id == grid.id)