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
This commit is contained in:
Alexander Hess 2026-11-30 11:39:49 +01:00
commit 1cc5dcff54
Signed by: alexander
GPG key ID: B46EDB4DFC26805D
2 changed files with 3 additions and 4 deletions

View file

@ -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:

View file

@ -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)