Skip to content

Commit

Permalink
fix(api): support Python 3.10 parsing
Browse files Browse the repository at this point in the history
support Python 3.10+ by fixing datetime parsing from known ISO 8601 format
with 'Z' offset character for UTC

see https://docs.python.org/3.11/library/datetime.html#datetime.datetime.fromisoformat
  • Loading branch information
thekaveman committed Mar 19, 2024
1 parent e866872 commit 7bc81b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
30 changes: 19 additions & 11 deletions littlepay/api/groups.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import InitVar, dataclass
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Generator

Expand Down Expand Up @@ -29,21 +29,29 @@ def csv_header() -> str:
class GroupFundingSourceResponse:
id: str
participant_id: str
concession_expiry: InitVar[datetime | None] = None
concession_created_at: InitVar[datetime | None] = None
concession_updated_at: InitVar[datetime | None] = None
concession_expiry: datetime | None = None
concession_created_at: datetime | None = None
concession_updated_at: datetime | None = None

def __post_init__(self, concession_expiry, concession_created_at, concession_updated_at):
if concession_expiry:
self.concession_expiry = datetime.fromisoformat(concession_expiry)
def __post_init__(self):
"""Parses any date parameters into Python datetime objects.
Includes a workaround for Python 3.10 where datetime.fromisoformat() can only parse the format output
by datetime.isoformat(), i.e. without a trailing 'Z' offset character and with UTC offset expressed
as +/-HH:mm
https://docs.python.org/3.11/library/datetime.html#datetime.datetime.fromisoformat
"""
if self.concession_expiry:
self.concession_expiry = datetime.fromisoformat(self.concession_expiry.replace("Z", "+00:00", 1))
else:
self.concession_expiry = None
if concession_created_at:
self.concession_created_at = datetime.fromisoformat(concession_created_at)
if self.concession_created_at:
self.concession_created_at = datetime.fromisoformat(self.concession_created_at.replace("Z", "+00:00", 1))
else:
self.concession_created_at = None
if concession_updated_at:
self.concession_updated_at = datetime.fromisoformat(concession_updated_at)
if self.concession_updated_at:
self.concession_updated_at = datetime.fromisoformat(self.concession_updated_at.replace("Z", "+00:00", 1))
else:
self.concession_updated_at = None

Expand Down
3 changes: 3 additions & 0 deletions tests/api/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ def test_GroupFundingSourceResponse_with_dates():
assert response.id == "id"
assert response.participant_id == "participant_id"
assert response.concession_expiry == expected_date
assert response.concession_expiry.tzinfo == timezone.utc
assert response.concession_created_at == expected_date
assert response.concession_created_at.tzinfo == timezone.utc
assert response.concession_updated_at == expected_date
assert response.concession_updated_at.tzinfo == timezone.utc


def test_GroupsMixin_concession_groups_endpoint(url):
Expand Down

0 comments on commit 7bc81b9

Please sign in to comment.