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:
parent
318dff8fc8
commit
e451f03d0f
8 changed files with 123 additions and 79 deletions
10
noxfile.py
10
noxfile.py
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
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`
|
||||
- "test": Run the test suite with `pytest`
|
||||
- "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)
|
||||
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.
|
||||
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
|
||||
# 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
|
||||
locations = session.posargs or SRC_LOCATIONS
|
||||
|
|
@ -166,6 +166,10 @@ def format_code(session: Session) -> None:
|
|||
session.run("isort", "--version")
|
||||
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)
|
||||
def lint_code(session: Session) -> None:
|
||||
|
|
|
|||
|
|
@ -140,13 +140,11 @@ class Customer(meta.Base):
|
|||
.all()
|
||||
)
|
||||
if len(restaurants) == 1: # type:ignore
|
||||
tooltip = (
|
||||
f"{restaurants[0].name} (#{restaurants[0].id})" # type:ignore
|
||||
)
|
||||
restaurant = restaurants[0] # type:ignore
|
||||
tooltip = f"{restaurant.name} (#{restaurant.id})"
|
||||
else:
|
||||
tooltip = "Restaurants " + ", ".join(
|
||||
f"#{restaurant.id}" for restaurant in restaurants # type:ignore
|
||||
)
|
||||
ids = ", ".join(f"#{r.id}" for r in restaurants) # type:ignore
|
||||
tooltip = f"Restaurants {ids}"
|
||||
|
||||
if order_counts:
|
||||
n_orders = (
|
||||
|
|
|
|||
|
|
@ -215,13 +215,11 @@ class Pixel(meta.Base):
|
|||
.all()
|
||||
)
|
||||
if len(restaurants) == 1: # type:ignore
|
||||
tooltip = (
|
||||
f"{restaurants[0].name} (#{restaurants[0].id})" # type:ignore
|
||||
)
|
||||
restaurant = restaurants[0] # type:ignore
|
||||
tooltip = f"{restaurant.name} (#{restaurant.id})"
|
||||
else:
|
||||
tooltip = "Restaurants " + ", ".join(
|
||||
f"#{restaurant.id}" for restaurant in restaurants # type:ignore
|
||||
)
|
||||
ids = ", ".join(f"#{r.id}" for r in restaurants) # type:ignore
|
||||
tooltip = f"Restaurants {ids}"
|
||||
|
||||
if order_counts:
|
||||
# Calculate the number of orders for ALL restaurants ...
|
||||
|
|
|
|||
|
|
@ -134,7 +134,6 @@ class HorizontalSMAModel(base.ForecastingModelABC):
|
|||
if (
|
||||
predictions[["actual", "prediction"]].isnull().any().any()
|
||||
): # pragma: no cover
|
||||
|
||||
raise RuntimeError("missing predictions in hsma model")
|
||||
if predict_at not in predictions.index: # pragma: no cover
|
||||
raise RuntimeError("missing prediction for `predict_at`")
|
||||
|
|
|
|||
|
|
@ -48,18 +48,22 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
|
|||
cancel_before_pickup = factory.Trait(
|
||||
cancel_=True,
|
||||
cancelled_at=factory.LazyAttribute(
|
||||
lambda obj: obj.dispatch_at
|
||||
+ utils.random_timespan(
|
||||
max_seconds=(obj.pickup_at - obj.dispatch_at).total_seconds(),
|
||||
lambda obj: (
|
||||
obj.dispatch_at
|
||||
+ utils.random_timespan(
|
||||
max_seconds=(obj.pickup_at - obj.dispatch_at).total_seconds(),
|
||||
)
|
||||
),
|
||||
),
|
||||
)
|
||||
cancel_after_pickup = factory.Trait(
|
||||
cancel_=True,
|
||||
cancelled_at=factory.LazyAttribute(
|
||||
lambda obj: obj.pickup_at
|
||||
+ utils.random_timespan(
|
||||
max_seconds=(obj.delivery_at - obj.pickup_at).total_seconds(),
|
||||
lambda obj: (
|
||||
obj.pickup_at
|
||||
+ 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.
|
||||
# Ad-hoc `Order`s are placed between 11.45 and 14.15.
|
||||
placed_at = factory.LazyFunction(
|
||||
lambda: dt.datetime(*test_config.DATE, 11, 45)
|
||||
+ utils.random_timespan(max_hours=2, max_minutes=30),
|
||||
lambda: (
|
||||
dt.datetime(*test_config.DATE, 11, 45)
|
||||
+ utils.random_timespan(max_hours=2, max_minutes=30)
|
||||
),
|
||||
)
|
||||
ad_hoc = True
|
||||
scheduled_delivery_at = None
|
||||
|
|
@ -96,15 +102,18 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
|
|||
# Restaurant-related attributes
|
||||
# restaurant -> set by the `make_order` fixture for better control
|
||||
restaurant_notified_at = factory.LazyAttribute(
|
||||
lambda obj: obj.placed_at
|
||||
+ utils.random_timespan(min_seconds=30, max_seconds=90),
|
||||
lambda obj: (
|
||||
obj.placed_at + utils.random_timespan(min_seconds=30, max_seconds=90)
|
||||
),
|
||||
)
|
||||
restaurant_notified_at_corrected = factory.LazyAttribute(
|
||||
lambda obj: False if obj.restaurant_notified_at else None,
|
||||
)
|
||||
restaurant_confirmed_at = factory.LazyAttribute(
|
||||
lambda obj: obj.restaurant_notified_at
|
||||
+ utils.random_timespan(min_seconds=30, max_seconds=150),
|
||||
lambda obj: (
|
||||
obj.restaurant_notified_at
|
||||
+ utils.random_timespan(min_seconds=30, max_seconds=150)
|
||||
),
|
||||
)
|
||||
restaurant_confirmed_at_corrected = factory.LazyAttribute(
|
||||
lambda obj: False if obj.restaurant_confirmed_at else None,
|
||||
|
|
@ -117,22 +126,26 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
|
|||
# Dispatch-related columns
|
||||
# courier -> set by the `make_order` fixture for better control
|
||||
dispatch_at = factory.LazyAttribute(
|
||||
lambda obj: obj.placed_at
|
||||
+ utils.random_timespan(min_seconds=600, max_seconds=1080),
|
||||
lambda obj: (
|
||||
obj.placed_at + utils.random_timespan(min_seconds=600, max_seconds=1080)
|
||||
),
|
||||
)
|
||||
dispatch_at_corrected = factory.LazyAttribute(
|
||||
lambda obj: False if obj.dispatch_at else None,
|
||||
)
|
||||
courier_notified_at = factory.LazyAttribute(
|
||||
lambda obj: obj.dispatch_at
|
||||
+ utils.random_timespan(min_seconds=100, max_seconds=140),
|
||||
lambda obj: (
|
||||
obj.dispatch_at + utils.random_timespan(min_seconds=100, max_seconds=140)
|
||||
),
|
||||
)
|
||||
courier_notified_at_corrected = factory.LazyAttribute(
|
||||
lambda obj: False if obj.courier_notified_at else None,
|
||||
)
|
||||
courier_accepted_at = factory.LazyAttribute(
|
||||
lambda obj: obj.courier_notified_at
|
||||
+ utils.random_timespan(min_seconds=15, max_seconds=45),
|
||||
lambda obj: (
|
||||
obj.courier_notified_at
|
||||
+ utils.random_timespan(min_seconds=15, max_seconds=45)
|
||||
),
|
||||
)
|
||||
courier_accepted_at_corrected = factory.LazyAttribute(
|
||||
lambda obj: False if obj.courier_accepted_at else None,
|
||||
|
|
@ -143,20 +156,25 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
|
|||
# Pickup-related attributes
|
||||
# pickup_address -> aligned with `restaurant.address` by the `make_order` fixture
|
||||
reached_pickup_at = factory.LazyAttribute(
|
||||
lambda obj: obj.courier_accepted_at
|
||||
+ utils.random_timespan(min_seconds=300, max_seconds=600),
|
||||
lambda obj: (
|
||||
obj.courier_accepted_at
|
||||
+ utils.random_timespan(min_seconds=300, max_seconds=600)
|
||||
),
|
||||
)
|
||||
pickup_at = factory.LazyAttribute(
|
||||
lambda obj: obj.reached_pickup_at
|
||||
+ utils.random_timespan(min_seconds=120, max_seconds=600),
|
||||
lambda obj: (
|
||||
obj.reached_pickup_at
|
||||
+ utils.random_timespan(min_seconds=120, max_seconds=600)
|
||||
),
|
||||
)
|
||||
pickup_at_corrected = factory.LazyAttribute(
|
||||
lambda obj: False if obj.pickup_at else None,
|
||||
)
|
||||
pickup_not_confirmed = False
|
||||
left_pickup_at = factory.LazyAttribute(
|
||||
lambda obj: obj.pickup_at
|
||||
+ utils.random_timespan(min_seconds=60, max_seconds=180),
|
||||
lambda obj: (
|
||||
obj.pickup_at + utils.random_timespan(min_seconds=60, max_seconds=180)
|
||||
),
|
||||
)
|
||||
left_pickup_at_corrected = factory.LazyAttribute(
|
||||
lambda obj: False if obj.left_pickup_at else None,
|
||||
|
|
@ -165,12 +183,15 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
|
|||
# Delivery-related attributes
|
||||
# delivery_address -> set by the `make_order` fixture as there is only one `city`
|
||||
reached_delivery_at = factory.LazyAttribute(
|
||||
lambda obj: obj.left_pickup_at
|
||||
+ utils.random_timespan(min_seconds=240, max_seconds=480),
|
||||
lambda obj: (
|
||||
obj.left_pickup_at + utils.random_timespan(min_seconds=240, max_seconds=480)
|
||||
),
|
||||
)
|
||||
delivery_at = factory.LazyAttribute(
|
||||
lambda obj: obj.reached_delivery_at
|
||||
+ utils.random_timespan(min_seconds=240, max_seconds=660),
|
||||
lambda obj: (
|
||||
obj.reached_delivery_at
|
||||
+ utils.random_timespan(min_seconds=240, max_seconds=660)
|
||||
),
|
||||
)
|
||||
delivery_at_corrected = factory.LazyAttribute(
|
||||
lambda obj: False if obj.delivery_at else None,
|
||||
|
|
@ -182,10 +203,12 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory):
|
|||
|
||||
# Statistical attributes -> calculate realistic stats
|
||||
logged_delivery_distance = factory.LazyAttribute(
|
||||
lambda obj: distance.great_circle(
|
||||
(obj.pickup_address.latitude, obj.pickup_address.longitude),
|
||||
(obj.delivery_address.latitude, obj.delivery_address.longitude),
|
||||
).meters,
|
||||
lambda obj: (
|
||||
distance.great_circle(
|
||||
(obj.pickup_address.latitude, obj.pickup_address.longitude),
|
||||
(obj.delivery_address.latitude, obj.delivery_address.longitude),
|
||||
).meters
|
||||
),
|
||||
)
|
||||
logged_avg_speed = factory.LazyAttribute(
|
||||
lambda obj: round(
|
||||
|
|
|
|||
|
|
@ -34,62 +34,81 @@ class ReplayedOrderFactory(alchemy.SQLAlchemyModelFactory):
|
|||
# Restaurant-related columns
|
||||
estimated_prep_duration = 1200
|
||||
restaurant_notified_at = factory.LazyAttribute(
|
||||
lambda obj: obj.placed_at
|
||||
+ utils.random_timespan(min_seconds=1, max_seconds=30),
|
||||
lambda obj: (
|
||||
obj.placed_at + utils.random_timespan(min_seconds=1, max_seconds=30)
|
||||
),
|
||||
)
|
||||
restaurant_confirmed_at = factory.LazyAttribute(
|
||||
lambda obj: obj.restaurant_notified_at
|
||||
+ utils.random_timespan(min_seconds=1, max_seconds=60),
|
||||
lambda obj: (
|
||||
obj.restaurant_notified_at
|
||||
+ utils.random_timespan(min_seconds=1, max_seconds=60)
|
||||
),
|
||||
)
|
||||
restaurant_ready_at = factory.LazyAttribute(
|
||||
lambda obj: obj.restaurant_confirmed_at
|
||||
+ dt.timedelta(seconds=obj.estimated_prep_duration)
|
||||
+ utils.random_timespan(min_seconds=300, max_seconds=300),
|
||||
lambda obj: (
|
||||
obj.restaurant_confirmed_at
|
||||
+ dt.timedelta(seconds=obj.estimated_prep_duration)
|
||||
+ utils.random_timespan(min_seconds=300, max_seconds=300)
|
||||
),
|
||||
)
|
||||
|
||||
# Dispatch-related columns
|
||||
dispatch_at = factory.LazyAttribute(
|
||||
lambda obj: obj.actual.placed_at
|
||||
+ utils.random_timespan(min_seconds=30, max_seconds=60),
|
||||
lambda obj: (
|
||||
obj.actual.placed_at + utils.random_timespan(min_seconds=30, max_seconds=60)
|
||||
),
|
||||
)
|
||||
first_estimated_delivery_at = factory.LazyAttribute(
|
||||
lambda obj: obj.restaurant_notified_at
|
||||
+ dt.timedelta(seconds=obj.estimated_prep_duration)
|
||||
+ dt.timedelta(minutes=10),
|
||||
lambda obj: (
|
||||
obj.restaurant_notified_at
|
||||
+ dt.timedelta(seconds=obj.estimated_prep_duration)
|
||||
+ dt.timedelta(minutes=10)
|
||||
),
|
||||
)
|
||||
# courier -> set by the `make_replay_order` fixture for better control
|
||||
courier_notified_at = factory.LazyAttribute(
|
||||
lambda obj: obj.dispatch_at
|
||||
+ utils.random_timespan(min_seconds=1, max_seconds=30),
|
||||
lambda obj: (
|
||||
obj.dispatch_at + utils.random_timespan(min_seconds=1, max_seconds=30)
|
||||
),
|
||||
)
|
||||
courier_accepted_at = factory.LazyAttribute(
|
||||
lambda obj: obj.courier_notified_at
|
||||
+ utils.random_timespan(min_seconds=1, max_seconds=60),
|
||||
lambda obj: (
|
||||
obj.courier_notified_at
|
||||
+ utils.random_timespan(min_seconds=1, max_seconds=60)
|
||||
),
|
||||
)
|
||||
utilization = None
|
||||
|
||||
# Pickup-related columns
|
||||
reached_pickup_at = factory.LazyAttribute(
|
||||
lambda obj: obj.restaurant_ready_at
|
||||
+ utils.random_timespan(min_seconds=1, max_seconds=60),
|
||||
lambda obj: (
|
||||
obj.restaurant_ready_at
|
||||
+ utils.random_timespan(min_seconds=1, max_seconds=60)
|
||||
),
|
||||
)
|
||||
pickup_at = factory.LazyAttribute(
|
||||
lambda obj: obj.reached_pickup_at
|
||||
+ utils.random_timespan(min_seconds=30, max_seconds=60),
|
||||
lambda obj: (
|
||||
obj.reached_pickup_at
|
||||
+ utils.random_timespan(min_seconds=30, max_seconds=60)
|
||||
),
|
||||
)
|
||||
left_pickup_at = factory.LazyAttribute(
|
||||
lambda obj: obj.pickup_at
|
||||
+ utils.random_timespan(min_seconds=30, max_seconds=60),
|
||||
lambda obj: (
|
||||
obj.pickup_at + utils.random_timespan(min_seconds=30, max_seconds=60)
|
||||
),
|
||||
)
|
||||
|
||||
# Delivery-related columns
|
||||
reached_delivery_at = factory.LazyAttribute(
|
||||
lambda obj: obj.left_pickup_at
|
||||
+ utils.random_timespan(min_minutes=5, max_minutes=10),
|
||||
lambda obj: (
|
||||
obj.left_pickup_at + utils.random_timespan(min_minutes=5, max_minutes=10)
|
||||
),
|
||||
)
|
||||
delivery_at = factory.LazyAttribute(
|
||||
lambda obj: obj.reached_delivery_at
|
||||
+ utils.random_timespan(min_seconds=30, max_seconds=60),
|
||||
lambda obj: (
|
||||
obj.reached_delivery_at
|
||||
+ utils.random_timespan(min_seconds=30, max_seconds=60)
|
||||
),
|
||||
)
|
||||
|
||||
@factory.post_generation
|
||||
|
|
|
|||
|
|
@ -45,12 +45,16 @@ class ScheduledOrderFactory(ad_hoc.AdHocOrderFactory):
|
|||
|
||||
# Restaurant-related attributes
|
||||
restaurant_notified_at = factory.LazyAttribute(
|
||||
lambda obj: obj.scheduled_delivery_at
|
||||
- utils.random_timespan(min_minutes=45, max_minutes=50),
|
||||
lambda obj: (
|
||||
obj.scheduled_delivery_at
|
||||
- utils.random_timespan(min_minutes=45, max_minutes=50)
|
||||
),
|
||||
)
|
||||
|
||||
# Dispatch-related attributes
|
||||
dispatch_at = factory.LazyAttribute(
|
||||
lambda obj: obj.scheduled_delivery_at
|
||||
- utils.random_timespan(min_minutes=40, max_minutes=45),
|
||||
lambda obj: (
|
||||
obj.scheduled_delivery_at
|
||||
- utils.random_timespan(min_minutes=40, max_minutes=45)
|
||||
),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
for _ in range(1_000):
|
||||
|
||||
# Ad-hoc `Order`s are far more common than pre-orders.
|
||||
scheduled = random.choice([True, False, False, False, False])
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue