Skip to content

Commit

Permalink
Merge pull request stravalib#440 from enadeau/fix-field-conversion
Browse files Browse the repository at this point in the history
fix type for timedelta element on models
  • Loading branch information
enadeau authored Sep 13, 2023
2 parents 82f3c29 + 1537a76 commit 8900f05
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Fix: forgot to update model CI build to follow new src layout (@lwasser, #438)
- Fix: Type annotation on field that are of type timedelta are now correct (@enadeau, #440)

## v1.5

Expand Down
9 changes: 0 additions & 9 deletions src/stravalib/field_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging
from collections.abc import Sequence
from datetime import timedelta
from functools import wraps
from typing import Callable, TypeVar

Expand Down Expand Up @@ -55,14 +54,6 @@ def enum_values(enums: Sequence[ActivityType | SportType]) -> list[str | None]:
return [enum_value(e) for e in enums]


@optional_input
def time_interval(seconds: int) -> timedelta:
"""
Replaces legacy TimeIntervalAttribute
"""
return timedelta(seconds=seconds)


@optional_input
def timezone(
tz: str,
Expand Down
35 changes: 16 additions & 19 deletions src/stravalib/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from __future__ import annotations

import logging
from datetime import date, datetime
from datetime import date, datetime, timedelta
from functools import wraps
from typing import (
TYPE_CHECKING,
Expand All @@ -36,7 +36,6 @@
from stravalib.field_conversions import (
enum_value,
enum_values,
time_interval,
timezone,
)
from stravalib.strava_model import (
Expand Down Expand Up @@ -492,11 +491,11 @@ class ActivityTotals(
elapsed time, moving time, distance and elevation gain."""

_field_conversions = {
"elapsed_time": time_interval,
"moving_time": time_interval,
"distance": uh.meters,
"elevation_gain": uh.meters,
}
elapsed_time: Optional[timedelta] = None # type: ignore[assignment]
moving_time: Optional[timedelta] = None # type: ignore[assignment]


class AthleteStats(
Expand Down Expand Up @@ -785,13 +784,13 @@ class ActivityLap(
device_watts: Optional[bool] = None

_field_conversions = {
"elapsed_time": time_interval,
"moving_time": time_interval,
"distance": uh.meters,
"total_elevation_gain": uh.meters,
"average_speed": uh.meters_per_second,
"max_speed": uh.meters_per_second,
}
elapsed_time: Optional[timedelta] = None # type: ignore[assignment]
moving_time: Optional[timedelta] = None # type: ignore[assignment]

_naive_local = validator("start_date_local", allow_reuse=True)(
naive_datetime
Expand Down Expand Up @@ -819,13 +818,13 @@ class Split(
average_grade_adjusted_speed: Optional[float] = None

_field_conversions = {
"elapsed_time": time_interval,
"moving_time": time_interval,
"distance": uh.meters,
"elevation_difference": uh.meters,
"average_speed": uh.meters_per_second,
"average_grade_adjusted_speed": uh.meters_per_second,
}
elapsed_time: Optional[timedelta] = None # type: ignore[assignment]
moving_time: Optional[timedelta] = None # type: ignore[assignment]


class SegmentExplorerResult(
Expand Down Expand Up @@ -882,14 +881,13 @@ class AthleteSegmentStats(

# Undocumented attributes:
effort_count: Optional[int] = None
pr_elapsed_time: Optional[int] = None
pr_elapsed_time: Optional[timedelta] = None
pr_date: Optional[date] = None

_field_conversions = {
"elapsed_time": time_interval,
"pr_elapsed_time": time_interval,
"distance": uh.meters,
}
elapsed_time: Optional[timedelta] = None # type: ignore[assignment]

_naive_local = validator("start_date_local", allow_reuse=True)(
naive_datetime
Expand All @@ -908,16 +906,16 @@ class AthletePrEffort(
is_kom: Optional[bool] = None

_field_conversions = {
"pr_elapsed_time": time_interval,
"distance": uh.meters,
}
pr_elapsed_time: Optional[timedelta] = None # type: ignore[assignment]

_naive_local = validator("start_date_local", allow_reuse=True)(
naive_datetime
)

@property
def elapsed_time(self) -> Optional[int]:
def elapsed_time(self) -> Optional[timedelta]:
# For backward compatibility
return self.pr_elapsed_time

Expand Down Expand Up @@ -946,7 +944,7 @@ class Segment(
start_longitude: Optional[float] = None
end_longitude: Optional[float] = None
starred: Optional[bool] = None
pr_time: Optional[int] = None
pr_time: Optional[timedelta] = None
starred_date: Optional[datetime] = None
elevation_profile: Optional[str] = None

Expand All @@ -955,7 +953,6 @@ class Segment(
"elevation_high": uh.meters,
"elevation_low": uh.meters,
"total_elevation_gain": uh.meters,
"pr_time": time_interval,
}

_latlng_check = validator(
Expand Down Expand Up @@ -1002,10 +999,10 @@ class BaseEffort(
athlete: Optional[Athlete] = None

_field_conversions = {
"moving_time": time_interval,
"elapsed_time": time_interval,
"distance": uh.meters,
}
moving_time: Optional[timedelta] = None # type: ignore[assignment]
elapsed_time: Optional[timedelta] = None # type: ignore[assignment]

_naive_local = validator("start_date_local", allow_reuse=True)(
naive_datetime
Expand Down Expand Up @@ -1086,8 +1083,6 @@ class Activity(
perceived_exertion: Optional[int] = None

_field_conversions = {
"moving_time": time_interval,
"elapsed_time": time_interval,
"timezone": timezone,
"distance": uh.meters,
"total_elevation_gain": uh.meters,
Expand All @@ -1096,6 +1091,8 @@ class Activity(
"type": enum_value,
"sport_type": enum_value,
}
moving_time: Optional[timedelta] = None # type: ignore[assignment]
elapsed_time: Optional[timedelta] = None # type: ignore[assignment]

_latlng_check = validator(
"start_latlng", "end_latlng", allow_reuse=True, pre=True
Expand Down

0 comments on commit 8900f05

Please sign in to comment.