Use t.Self for methods returning their own class

This commit is contained in:
Alexander Hess 2026-12-01 11:02:12 +01:00
commit 0d2aab4211
Signed by: alexander
GPG key ID: B46EDB4DFC26805D
8 changed files with 20 additions and 13 deletions

View file

@ -179,7 +179,7 @@ class Address(meta.Base):
""" """
return self.location.y 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. """Shortcut to the `.city.clear_map()` method.
Returns: Returns:

View file

@ -3,6 +3,7 @@
import functools import functools
import itertools import itertools
import json import json
import typing as t
import folium import folium
import googlemaps as gm import googlemaps as gm
@ -124,7 +125,7 @@ class Path(meta.Base):
cls, cls,
*addresses: db.Address, *addresses: db.Address,
google_maps: bool = False, google_maps: bool = False,
) -> list["Path"]: ) -> list[t.Self]:
"""Calculate pair-wise paths for `Address` objects. """Calculate pair-wise paths for `Address` objects.
This is the main constructor method for the class. 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 ... # If there is no `Path` object in the database ...
path = ( path = (
db.session.query(db.Path) db.session.query(cls)
.filter(db.Path.first_address == first) .filter(cls.first_address == first)
.filter(db.Path.second_address == second) .filter(cls.second_address == second)
.first() .first()
) )
# ... create a new one. # ... create a new one.
@ -181,7 +182,7 @@ class Path(meta.Base):
return paths return paths
@classmethod @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. """Calculate the path for an `Order` object.
The path goes from the `Order.pickup_address` to the `Order.delivery_address`. 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.""" """Provide the ORM's `City` model."""
import functools import functools
import typing as t
import folium import folium
import sqlalchemy as sa import sqlalchemy as sa
@ -127,7 +128,7 @@ class City(meta.Base):
""" """
return self.northeast.northing - self.southwest.northing 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. """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 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.""" """Provide the ORM's `Customer` model."""
import typing as t
import folium import folium
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy import orm from sqlalchemy import orm
@ -31,7 +33,7 @@ class Customer(meta.Base):
back_populates="customer", 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. """Shortcut to the `...city.clear_map()` method.
Returns: Returns:

View file

@ -2,6 +2,7 @@
import datetime as dt import datetime as dt
import math import math
import typing as t
import pandas as pd import pandas as pd
import sqlalchemy as sa import sqlalchemy as sa
@ -194,7 +195,7 @@ class Forecast(meta.Base):
train_horizon: int, train_horizon: int,
model: str, model: str,
data: pd.DataFrame, data: pd.DataFrame,
) -> list["db.Forecast"]: ) -> list[t.Self]:
"""Convert results from the forecasting `*Model`s into `Forecast` objects. """Convert results from the forecasting `*Model`s into `Forecast` objects.
This is an alternative constructor method. 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) return round((self.side_length**2) / 1_000_000, 1)
@classmethod @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`. """Create a fully populated `Grid` for a `city`.
The `Grid` contains only `Pixel`s that have at least one The `Grid` contains only `Pixel`s that have at least one
@ -126,7 +126,7 @@ class Grid(meta.Base):
return grid 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. """Shortcut to the `.city.clear_map()` method.
Returns: Returns:

View file

@ -1,6 +1,7 @@
"""Provide the ORM's `Pixel` model.""" """Provide the ORM's `Pixel` model."""
import functools import functools
import typing as t
import folium import folium
import sqlalchemy as sa import sqlalchemy as sa
@ -149,7 +150,7 @@ class Pixel(meta.Base):
.all() .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. """Shortcut to the `.city.clear_map()` method.
Returns: Returns:

View file

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