Make mypy as strict as practical #13

Merged
alexander merged 32 commits from stricter-mypy into develop 2026-12-01 14:26:52 +01:00
8 changed files with 20 additions and 13 deletions
Showing only changes of commit 0d2aab4211 - Show all commits

Use t.Self for methods returning their own class

Alexander Hess 2026-12-01 11:02:12 +01:00
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -179,7 +179,7 @@ class Address(meta.Base):
"""
return self.location.y
def clear_map(self) -> "Address": # pragma: no cover
def clear_map(self) -> t.Self: # pragma: no cover
"""Shortcut to the `.city.clear_map()` method.
Returns:

View file

@ -3,6 +3,7 @@
import functools
import itertools
import json
import typing as t
import folium
import googlemaps as gm
@ -124,7 +125,7 @@ class Path(meta.Base):
cls,
*addresses: db.Address,
google_maps: bool = False,
) -> list["Path"]:
) -> list[t.Self]:
"""Calculate pair-wise paths for `Address` objects.
This is the main constructor method for the class.
@ -151,9 +152,9 @@ class Path(meta.Base):
# If there is no `Path` object in the database ...
path = (
db.session.query(db.Path)
.filter(db.Path.first_address == first)
.filter(db.Path.second_address == second)
db.session.query(cls)
.filter(cls.first_address == first)
.filter(cls.second_address == second)
.first()
)
# ... create a new one.
@ -181,7 +182,7 @@ class Path(meta.Base):
return paths
@classmethod
def from_order(cls, order: "db.Order", *, google_maps: bool = False) -> "Path":
def from_order(cls, order: "db.Order", *, google_maps: bool = False) -> t.Self:
"""Calculate the path for an `Order` object.
The path goes from the `Order.pickup_address` to the `Order.delivery_address`.

View file

@ -1,6 +1,7 @@
"""Provide the ORM's `City` model."""
import functools
import typing as t
import folium
import sqlalchemy as sa
@ -127,7 +128,7 @@ class City(meta.Base):
"""
return self.northeast.northing - self.southwest.northing
def clear_map(self) -> "City": # pragma: no cover
def clear_map(self) -> t.Self: # pragma: no cover
"""Create a new `folium.Map` object aligned with the city's viewport.
The map is available via the `.map` property. Note that it is mutable

View file

@ -1,5 +1,7 @@
"""Provide the ORM's `Customer` model."""
import typing as t
import folium
import sqlalchemy as sa
from sqlalchemy import orm
@ -31,7 +33,7 @@ class Customer(meta.Base):
back_populates="customer",
)
def clear_map(self) -> "Customer": # pragma: no cover
def clear_map(self) -> t.Self: # pragma: no cover
"""Shortcut to the `...city.clear_map()` method.
Returns:

View file

@ -2,6 +2,7 @@
import datetime as dt
import math
import typing as t
import pandas as pd
import sqlalchemy as sa
@ -194,7 +195,7 @@ class Forecast(meta.Base):
train_horizon: int,
model: str,
data: pd.DataFrame,
) -> list["db.Forecast"]:
) -> list[t.Self]:
"""Convert results from the forecasting `*Model`s into `Forecast` objects.
This is an alternative constructor method.

View file

@ -71,7 +71,7 @@ class Grid(meta.Base):
return round((self.side_length**2) / 1_000_000, 1)
@classmethod
def gridify(cls, city: db.City, side_length: int) -> "db.Grid":
def gridify(cls, city: db.City, side_length: int) -> t.Self:
"""Create a fully populated `Grid` for a `city`.
The `Grid` contains only `Pixel`s that have at least one
@ -126,7 +126,7 @@ class Grid(meta.Base):
return grid
def clear_map(self) -> "Grid": # pragma: no cover
def clear_map(self) -> t.Self: # pragma: no cover
"""Shortcut to the `.city.clear_map()` method.
Returns:

View file

@ -1,6 +1,7 @@
"""Provide the ORM's `Pixel` model."""
import functools
import typing as t
import folium
import sqlalchemy as sa
@ -149,7 +150,7 @@ class Pixel(meta.Base):
.all()
)
def clear_map(self) -> "Pixel": # pragma: no cover
def clear_map(self) -> t.Self: # pragma: no cover
"""Shortcut to the `.city.clear_map()` method.
Returns:

View file

@ -1,6 +1,7 @@
"""Provide the ORM's `Restaurant` model."""
import datetime as dt
import typing as t
import folium
import sqlalchemy as sa
@ -76,7 +77,7 @@ class Restaurant(meta.Base):
"""Non-literal text representation."""
return f"<{self.__class__.__name__}({self.name})>"
def clear_map(self) -> "Restaurant": # pragma: no cover
def clear_map(self) -> t.Self: # pragma: no cover
"""Shortcut to the `.address.city.clear_map()` method.
Returns: