Add ruff to the "format" session

`ruff format` adds parentheses around multi-line lambda bodies
and removes blank lines after block openers,
both of which `black` accepts.

Lines with `# type:ignore` are shortened
because `black` counts pragma comments toward the line length
and `ruff` does not.
This commit is contained in:
Alexander Hess 2026-11-28 04:43:49 +01:00
commit e451f03d0f
Signed by: alexander
GPG key ID: B46EDB4DFC26805D
8 changed files with 123 additions and 79 deletions

View file

@ -2,7 +2,7 @@
By default, `uv run nox` runs the sessions that assure code quality: By default, `uv run nox` runs the sessions that assure code quality:
- "format": Format the source files with `autoflake`, `black`, and `isort` - "format": Format the source files with `autoflake`, `black`, `isort`, and `ruff`
- "lint": Lint the source files with `flake8`, `isort`, and `ruff` - "lint": Lint the source files with `flake8`, `isort`, and `ruff`
- "test": Run the test suite with `pytest` - "test": Run the test suite with `pytest`
- "doctest": Check if code snippets in the docstrings run - "doctest": Check if code snippets in the docstrings run
@ -141,7 +141,7 @@ os.makedirs(tempfile.tempdir, exist_ok=True)
@nox.session(name="format", python=MAIN_PYTHON) @nox.session(name="format", python=MAIN_PYTHON)
def format_code(session: Session) -> None: def format_code(session: Session) -> None:
"""Format source files with `autoflake`, `black`, and `isort`. """Format source files with `autoflake`, `black`, `isort`, and `ruff`.
If no extra arguments are provided, all source files are formatted. If no extra arguments are provided, all source files are formatted.
Otherwise, they are interpreted as paths the formatters work on recursively. Otherwise, they are interpreted as paths the formatters work on recursively.
@ -150,7 +150,7 @@ def format_code(session: Session) -> None:
# The formatters do not need `velofy` installed # The formatters do not need `velofy` installed
# because they work on the source files directly # because they work on the source files directly
install_pinned(session, "autoflake", "black", "isort") install_pinned(session, "autoflake", "black", "isort", "ruff")
# Interpret extra arguments as `locations` of source files # Interpret extra arguments as `locations` of source files
locations = session.posargs or SRC_LOCATIONS locations = session.posargs or SRC_LOCATIONS
@ -166,6 +166,10 @@ def format_code(session: Session) -> None:
session.run("isort", "--version") session.run("isort", "--version")
session.run("isort", *locations) session.run("isort", *locations)
session.run("ruff", "--version")
session.run("ruff", "check", "--fix-only", *locations)
session.run("ruff", "format", *locations)
@nox.session(name="lint", python=MAIN_PYTHON) @nox.session(name="lint", python=MAIN_PYTHON)
def lint_code(session: Session) -> None: def lint_code(session: Session) -> None:

View file

@ -140,13 +140,11 @@ class Customer(meta.Base):
.all() .all()
) )
if len(restaurants) == 1: # type:ignore if len(restaurants) == 1: # type:ignore
tooltip = ( restaurant = restaurants[0] # type:ignore
f"{restaurants[0].name} (#{restaurants[0].id})" # type:ignore tooltip = f"{restaurant.name} (#{restaurant.id})"
)
else: else:
tooltip = "Restaurants " + ", ".join( ids = ", ".join(f"#{r.id}" for r in restaurants) # type:ignore
f"#{restaurant.id}" for restaurant in restaurants # type:ignore tooltip = f"Restaurants {ids}"
)
if order_counts: if order_counts:
n_orders = ( n_orders = (

View file

@ -215,13 +215,11 @@ class Pixel(meta.Base):
.all() .all()
) )
if len(restaurants) == 1: # type:ignore if len(restaurants) == 1: # type:ignore
tooltip = ( restaurant = restaurants[0] # type:ignore
f"{restaurants[0].name} (#{restaurants[0].id})" # type:ignore tooltip = f"{restaurant.name} (#{restaurant.id})"
)
else: else:
tooltip = "Restaurants " + ", ".join( ids = ", ".join(f"#{r.id}" for r in restaurants) # type:ignore
f"#{restaurant.id}" for restaurant in restaurants # type:ignore tooltip = f"Restaurants {ids}"
)
if order_counts: if order_counts:
# Calculate the number of orders for ALL restaurants ... # Calculate the number of orders for ALL restaurants ...

View file

@ -134,7 +134,6 @@ class HorizontalSMAModel(base.ForecastingModelABC):
if ( if (
predictions[["actual", "prediction"]].isnull().any().any() predictions[["actual", "prediction"]].isnull().any().any()
): # pragma: no cover ): # pragma: no cover
raise RuntimeError("missing predictions in hsma model") raise RuntimeError("missing predictions in hsma model")
if predict_at not in predictions.index: # pragma: no cover if predict_at not in predictions.index: # pragma: no cover
raise RuntimeError("missing prediction for `predict_at`") raise RuntimeError("missing prediction for `predict_at`")

View file

@ -48,18 +48,22 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
cancel_before_pickup = factory.Trait( cancel_before_pickup = factory.Trait(
cancel_=True, cancel_=True,
cancelled_at=factory.LazyAttribute( cancelled_at=factory.LazyAttribute(
lambda obj: obj.dispatch_at lambda obj: (
+ utils.random_timespan( obj.dispatch_at
max_seconds=(obj.pickup_at - obj.dispatch_at).total_seconds(), + utils.random_timespan(
max_seconds=(obj.pickup_at - obj.dispatch_at).total_seconds(),
)
), ),
), ),
) )
cancel_after_pickup = factory.Trait( cancel_after_pickup = factory.Trait(
cancel_=True, cancel_=True,
cancelled_at=factory.LazyAttribute( cancelled_at=factory.LazyAttribute(
lambda obj: obj.pickup_at lambda obj: (
+ utils.random_timespan( obj.pickup_at
max_seconds=(obj.delivery_at - obj.pickup_at).total_seconds(), + utils.random_timespan(
max_seconds=(obj.delivery_at - obj.pickup_at).total_seconds(),
)
), ),
), ),
) )
@ -71,8 +75,10 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
# Attributes regarding the specialization of an `Order`: ad-hoc or scheduled. # Attributes regarding the specialization of an `Order`: ad-hoc or scheduled.
# Ad-hoc `Order`s are placed between 11.45 and 14.15. # Ad-hoc `Order`s are placed between 11.45 and 14.15.
placed_at = factory.LazyFunction( placed_at = factory.LazyFunction(
lambda: dt.datetime(*test_config.DATE, 11, 45) lambda: (
+ utils.random_timespan(max_hours=2, max_minutes=30), dt.datetime(*test_config.DATE, 11, 45)
+ utils.random_timespan(max_hours=2, max_minutes=30)
),
) )
ad_hoc = True ad_hoc = True
scheduled_delivery_at = None scheduled_delivery_at = None
@ -96,15 +102,18 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
# Restaurant-related attributes # Restaurant-related attributes
# restaurant -> set by the `make_order` fixture for better control # restaurant -> set by the `make_order` fixture for better control
restaurant_notified_at = factory.LazyAttribute( restaurant_notified_at = factory.LazyAttribute(
lambda obj: obj.placed_at lambda obj: (
+ utils.random_timespan(min_seconds=30, max_seconds=90), obj.placed_at + utils.random_timespan(min_seconds=30, max_seconds=90)
),
) )
restaurant_notified_at_corrected = factory.LazyAttribute( restaurant_notified_at_corrected = factory.LazyAttribute(
lambda obj: False if obj.restaurant_notified_at else None, lambda obj: False if obj.restaurant_notified_at else None,
) )
restaurant_confirmed_at = factory.LazyAttribute( restaurant_confirmed_at = factory.LazyAttribute(
lambda obj: obj.restaurant_notified_at lambda obj: (
+ utils.random_timespan(min_seconds=30, max_seconds=150), obj.restaurant_notified_at
+ utils.random_timespan(min_seconds=30, max_seconds=150)
),
) )
restaurant_confirmed_at_corrected = factory.LazyAttribute( restaurant_confirmed_at_corrected = factory.LazyAttribute(
lambda obj: False if obj.restaurant_confirmed_at else None, lambda obj: False if obj.restaurant_confirmed_at else None,
@ -117,22 +126,26 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
# Dispatch-related columns # Dispatch-related columns
# courier -> set by the `make_order` fixture for better control # courier -> set by the `make_order` fixture for better control
dispatch_at = factory.LazyAttribute( dispatch_at = factory.LazyAttribute(
lambda obj: obj.placed_at lambda obj: (
+ utils.random_timespan(min_seconds=600, max_seconds=1080), obj.placed_at + utils.random_timespan(min_seconds=600, max_seconds=1080)
),
) )
dispatch_at_corrected = factory.LazyAttribute( dispatch_at_corrected = factory.LazyAttribute(
lambda obj: False if obj.dispatch_at else None, lambda obj: False if obj.dispatch_at else None,
) )
courier_notified_at = factory.LazyAttribute( courier_notified_at = factory.LazyAttribute(
lambda obj: obj.dispatch_at lambda obj: (
+ utils.random_timespan(min_seconds=100, max_seconds=140), obj.dispatch_at + utils.random_timespan(min_seconds=100, max_seconds=140)
),
) )
courier_notified_at_corrected = factory.LazyAttribute( courier_notified_at_corrected = factory.LazyAttribute(
lambda obj: False if obj.courier_notified_at else None, lambda obj: False if obj.courier_notified_at else None,
) )
courier_accepted_at = factory.LazyAttribute( courier_accepted_at = factory.LazyAttribute(
lambda obj: obj.courier_notified_at lambda obj: (
+ utils.random_timespan(min_seconds=15, max_seconds=45), obj.courier_notified_at
+ utils.random_timespan(min_seconds=15, max_seconds=45)
),
) )
courier_accepted_at_corrected = factory.LazyAttribute( courier_accepted_at_corrected = factory.LazyAttribute(
lambda obj: False if obj.courier_accepted_at else None, lambda obj: False if obj.courier_accepted_at else None,
@ -143,20 +156,25 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
# Pickup-related attributes # Pickup-related attributes
# pickup_address -> aligned with `restaurant.address` by the `make_order` fixture # pickup_address -> aligned with `restaurant.address` by the `make_order` fixture
reached_pickup_at = factory.LazyAttribute( reached_pickup_at = factory.LazyAttribute(
lambda obj: obj.courier_accepted_at lambda obj: (
+ utils.random_timespan(min_seconds=300, max_seconds=600), obj.courier_accepted_at
+ utils.random_timespan(min_seconds=300, max_seconds=600)
),
) )
pickup_at = factory.LazyAttribute( pickup_at = factory.LazyAttribute(
lambda obj: obj.reached_pickup_at lambda obj: (
+ utils.random_timespan(min_seconds=120, max_seconds=600), obj.reached_pickup_at
+ utils.random_timespan(min_seconds=120, max_seconds=600)
),
) )
pickup_at_corrected = factory.LazyAttribute( pickup_at_corrected = factory.LazyAttribute(
lambda obj: False if obj.pickup_at else None, lambda obj: False if obj.pickup_at else None,
) )
pickup_not_confirmed = False pickup_not_confirmed = False
left_pickup_at = factory.LazyAttribute( left_pickup_at = factory.LazyAttribute(
lambda obj: obj.pickup_at lambda obj: (
+ utils.random_timespan(min_seconds=60, max_seconds=180), obj.pickup_at + utils.random_timespan(min_seconds=60, max_seconds=180)
),
) )
left_pickup_at_corrected = factory.LazyAttribute( left_pickup_at_corrected = factory.LazyAttribute(
lambda obj: False if obj.left_pickup_at else None, lambda obj: False if obj.left_pickup_at else None,
@ -165,12 +183,15 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
# Delivery-related attributes # Delivery-related attributes
# delivery_address -> set by the `make_order` fixture as there is only one `city` # delivery_address -> set by the `make_order` fixture as there is only one `city`
reached_delivery_at = factory.LazyAttribute( reached_delivery_at = factory.LazyAttribute(
lambda obj: obj.left_pickup_at lambda obj: (
+ utils.random_timespan(min_seconds=240, max_seconds=480), obj.left_pickup_at + utils.random_timespan(min_seconds=240, max_seconds=480)
),
) )
delivery_at = factory.LazyAttribute( delivery_at = factory.LazyAttribute(
lambda obj: obj.reached_delivery_at lambda obj: (
+ utils.random_timespan(min_seconds=240, max_seconds=660), obj.reached_delivery_at
+ utils.random_timespan(min_seconds=240, max_seconds=660)
),
) )
delivery_at_corrected = factory.LazyAttribute( delivery_at_corrected = factory.LazyAttribute(
lambda obj: False if obj.delivery_at else None, lambda obj: False if obj.delivery_at else None,
@ -182,10 +203,12 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
# Statistical attributes -> calculate realistic stats # Statistical attributes -> calculate realistic stats
logged_delivery_distance = factory.LazyAttribute( logged_delivery_distance = factory.LazyAttribute(
lambda obj: distance.great_circle( lambda obj: (
(obj.pickup_address.latitude, obj.pickup_address.longitude), distance.great_circle(
(obj.delivery_address.latitude, obj.delivery_address.longitude), (obj.pickup_address.latitude, obj.pickup_address.longitude),
).meters, (obj.delivery_address.latitude, obj.delivery_address.longitude),
).meters
),
) )
logged_avg_speed = factory.LazyAttribute( logged_avg_speed = factory.LazyAttribute(
lambda obj: round( lambda obj: round(

View file

@ -34,62 +34,81 @@ class ReplayedOrderFactory(alchemy.SQLAlchemyModelFactory):
# Restaurant-related columns # Restaurant-related columns
estimated_prep_duration = 1200 estimated_prep_duration = 1200
restaurant_notified_at = factory.LazyAttribute( restaurant_notified_at = factory.LazyAttribute(
lambda obj: obj.placed_at lambda obj: (
+ utils.random_timespan(min_seconds=1, max_seconds=30), obj.placed_at + utils.random_timespan(min_seconds=1, max_seconds=30)
),
) )
restaurant_confirmed_at = factory.LazyAttribute( restaurant_confirmed_at = factory.LazyAttribute(
lambda obj: obj.restaurant_notified_at lambda obj: (
+ utils.random_timespan(min_seconds=1, max_seconds=60), obj.restaurant_notified_at
+ utils.random_timespan(min_seconds=1, max_seconds=60)
),
) )
restaurant_ready_at = factory.LazyAttribute( restaurant_ready_at = factory.LazyAttribute(
lambda obj: obj.restaurant_confirmed_at lambda obj: (
+ dt.timedelta(seconds=obj.estimated_prep_duration) obj.restaurant_confirmed_at
+ utils.random_timespan(min_seconds=300, max_seconds=300), + dt.timedelta(seconds=obj.estimated_prep_duration)
+ utils.random_timespan(min_seconds=300, max_seconds=300)
),
) )
# Dispatch-related columns # Dispatch-related columns
dispatch_at = factory.LazyAttribute( dispatch_at = factory.LazyAttribute(
lambda obj: obj.actual.placed_at lambda obj: (
+ utils.random_timespan(min_seconds=30, max_seconds=60), obj.actual.placed_at + utils.random_timespan(min_seconds=30, max_seconds=60)
),
) )
first_estimated_delivery_at = factory.LazyAttribute( first_estimated_delivery_at = factory.LazyAttribute(
lambda obj: obj.restaurant_notified_at lambda obj: (
+ dt.timedelta(seconds=obj.estimated_prep_duration) obj.restaurant_notified_at
+ dt.timedelta(minutes=10), + dt.timedelta(seconds=obj.estimated_prep_duration)
+ dt.timedelta(minutes=10)
),
) )
# courier -> set by the `make_replay_order` fixture for better control # courier -> set by the `make_replay_order` fixture for better control
courier_notified_at = factory.LazyAttribute( courier_notified_at = factory.LazyAttribute(
lambda obj: obj.dispatch_at lambda obj: (
+ utils.random_timespan(min_seconds=1, max_seconds=30), obj.dispatch_at + utils.random_timespan(min_seconds=1, max_seconds=30)
),
) )
courier_accepted_at = factory.LazyAttribute( courier_accepted_at = factory.LazyAttribute(
lambda obj: obj.courier_notified_at lambda obj: (
+ utils.random_timespan(min_seconds=1, max_seconds=60), obj.courier_notified_at
+ utils.random_timespan(min_seconds=1, max_seconds=60)
),
) )
utilization = None utilization = None
# Pickup-related columns # Pickup-related columns
reached_pickup_at = factory.LazyAttribute( reached_pickup_at = factory.LazyAttribute(
lambda obj: obj.restaurant_ready_at lambda obj: (
+ utils.random_timespan(min_seconds=1, max_seconds=60), obj.restaurant_ready_at
+ utils.random_timespan(min_seconds=1, max_seconds=60)
),
) )
pickup_at = factory.LazyAttribute( pickup_at = factory.LazyAttribute(
lambda obj: obj.reached_pickup_at lambda obj: (
+ utils.random_timespan(min_seconds=30, max_seconds=60), obj.reached_pickup_at
+ utils.random_timespan(min_seconds=30, max_seconds=60)
),
) )
left_pickup_at = factory.LazyAttribute( left_pickup_at = factory.LazyAttribute(
lambda obj: obj.pickup_at lambda obj: (
+ utils.random_timespan(min_seconds=30, max_seconds=60), obj.pickup_at + utils.random_timespan(min_seconds=30, max_seconds=60)
),
) )
# Delivery-related columns # Delivery-related columns
reached_delivery_at = factory.LazyAttribute( reached_delivery_at = factory.LazyAttribute(
lambda obj: obj.left_pickup_at lambda obj: (
+ utils.random_timespan(min_minutes=5, max_minutes=10), obj.left_pickup_at + utils.random_timespan(min_minutes=5, max_minutes=10)
),
) )
delivery_at = factory.LazyAttribute( delivery_at = factory.LazyAttribute(
lambda obj: obj.reached_delivery_at lambda obj: (
+ utils.random_timespan(min_seconds=30, max_seconds=60), obj.reached_delivery_at
+ utils.random_timespan(min_seconds=30, max_seconds=60)
),
) )
@factory.post_generation @factory.post_generation

View file

@ -45,12 +45,16 @@ class ScheduledOrderFactory(ad_hoc.AdHocOrderFactory):
# Restaurant-related attributes # Restaurant-related attributes
restaurant_notified_at = factory.LazyAttribute( restaurant_notified_at = factory.LazyAttribute(
lambda obj: obj.scheduled_delivery_at lambda obj: (
- utils.random_timespan(min_minutes=45, max_minutes=50), obj.scheduled_delivery_at
- utils.random_timespan(min_minutes=45, max_minutes=50)
),
) )
# Dispatch-related attributes # Dispatch-related attributes
dispatch_at = factory.LazyAttribute( dispatch_at = factory.LazyAttribute(
lambda obj: obj.scheduled_delivery_at lambda obj: (
- utils.random_timespan(min_minutes=40, max_minutes=45), obj.scheduled_delivery_at
- utils.random_timespan(min_minutes=40, max_minutes=45)
),
) )

View file

@ -1097,7 +1097,6 @@ def test_make_random_orders( # noqa:C901
""" """
# Generate a large number of `Order`s to obtain a large variance of data. # Generate a large number of `Order`s to obtain a large variance of data.
for _ in range(1_000): for _ in range(1_000):
# Ad-hoc `Order`s are far more common than pre-orders. # Ad-hoc `Order`s are far more common than pre-orders.
scheduled = random.choice([True, False, False, False, False]) scheduled = random.choice([True, False, False, False, False])