Skip to content

Commit

Permalink
FIX: Gracefully handle Ergast date parse errors (#546)
Browse files Browse the repository at this point in the history
Fixes #545, #547

(cherry picked from commit dd33132)
  • Loading branch information
mdsakalu authored and theOehrly committed Mar 10, 2024
1 parent b42caf6 commit f02f6d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions fastf1/ergast/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
# matches [hh:][mm:]ss[.micros][Z | +-hh:mm] timestring


def date_from_ergast(d_str) -> datetime.datetime:
def date_from_ergast(d_str) -> Optional[datetime.datetime]:
"""Create a ``datetime.datetime`` object from a date stamp formatted
like 'YYYY-MM-DD'."""
return datetime.datetime.strptime(d_str, "%Y-%m-%d")
try:
return datetime.datetime.strptime(d_str, "%Y-%m-%d")
except ValueError:
logger.debug(f"Failed to parse date stamp '{d_str}' in Ergast"
f"response.")
return None


def time_from_ergast(t_str) -> Optional[datetime.time]:
Expand Down
13 changes: 13 additions & 0 deletions fastf1/tests/test_ergast.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ def test_date_from_ergast():
assert date_from_ergast('2022-10-25') == datetime.datetime(2022, 10, 25)


@pytest.mark.parametrize(
"date_string",
(
"",
"huh?"
)
)
def test_date_from_ergast_errors(date_string, caplog):
caplog.set_level(logging.DEBUG)
assert date_from_ergast(date_string) is None
assert "Failed to parse" in caplog.text


@pytest.mark.parametrize(
"time_string, expected",
(
Expand Down

0 comments on commit f02f6d0

Please sign in to comment.