Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle Ergast date parse errors #546

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading