Fix mypy type errors in trip helpers

This commit is contained in:
Edward Betts 2026-01-15 22:53:10 +00:00
parent 8a0df48a73
commit ddfa77b152
3 changed files with 20 additions and 14 deletions

View file

@ -81,15 +81,16 @@ def get_busy_events(
return busy_events
def _parse_datetime_field(datetime_obj: datetime | date) -> tuple[datetime, date]:
def _parse_datetime_field(datetime_obj: datetime | date | str) -> tuple[datetime, date]:
"""Parse a datetime field that could be datetime object or string."""
if hasattr(datetime_obj, "date"):
if isinstance(datetime_obj, datetime):
return datetime_obj, datetime_obj.date()
elif isinstance(datetime_obj, str):
if isinstance(datetime_obj, date):
return datetime.combine(datetime_obj, datetime.min.time()), datetime_obj
if isinstance(datetime_obj, str):
dt = datetime.fromisoformat(datetime_obj.replace("Z", "+00:00"))
return dt, dt.date()
else:
raise ValueError(f"Invalid datetime format: {datetime_obj}")
raise ValueError(f"Invalid datetime format: {datetime_obj}")
def _get_accommodation_location(
@ -105,13 +106,15 @@ def _get_accommodation_location(
def _find_most_recent_travel_within_trip(
trip: Trip,
target_date: date,
) -> tuple[str | None, pycountry.db.Country] | None:
) -> tuple[str | None, pycountry.db.Country | None] | None:
"""Find the most recent travel location within a trip."""
uk_airports = {"LHR", "LGW", "STN", "LTN", "BRS", "BHX", "MAN", "EDI", "GLA"}
trip_most_recent_date = None
trip_most_recent_location = None
trip_most_recent_datetime = None
trip_most_recent_date: date | None = None
trip_most_recent_location: tuple[str | None, pycountry.db.Country | None] | None = (
None
)
trip_most_recent_datetime: datetime | None = None
# Check flights within trip period
for travel_item in trip.travel:
@ -286,6 +289,9 @@ def _get_trip_location_by_progression(
return (city, country)
# Multiple locations: use progression through the trip
if not trip.end:
city, country = locations[-1]
return (city, country)
trip_duration = (trip.end - trip.start).days + 1
days_into_trip = (target_date - trip.start).days
@ -305,9 +311,9 @@ def _find_most_recent_travel_before_date(
"""Find the most recent travel location before a given date."""
uk_airports = {"LHR", "LGW", "STN", "LTN", "BRS", "BHX", "MAN", "EDI", "GLA"}
most_recent_location = None
most_recent_date = None
most_recent_datetime = None
most_recent_location: tuple[str | None, pycountry.db.Country | None] | None = None
most_recent_date: date | None = None
most_recent_datetime: datetime | None = None
# Check all travel across all trips
for trip in trips:

View file

@ -100,7 +100,7 @@ def build_toastui_events(events: list[Event]) -> list[dict[str, typing.Any]]:
end_date = e.as_date
end_iso = end_date.isoformat()
item = {
item: dict[str, typing.Any] = {
"id": str(uuid.uuid4()),
"calendarId": calendar_id,
"title": e.title_with_emoji,

View file

@ -25,7 +25,7 @@ class Airline(typing.TypedDict, total=False):
def load_travel(travel_type: str, plural: str, data_dir: str) -> list[StrDict]:
"""Read flight and train journeys."""
items = typing.cast(list[StrDict], travel.parse_yaml(plural, data_dir))
items: list[StrDict] = travel.parse_yaml(plural, data_dir)
for item in items:
item["type"] = travel_type
return items