From f02f6d014c19fce8d6a9f57a83ceeb65f92d7878 Mon Sep 17 00:00:00 2001 From: Michael Sakaluk Date: Sun, 10 Mar 2024 07:26:00 -0400 Subject: [PATCH] FIX: Gracefully handle Ergast date parse errors (#546) Fixes #545, #547 (cherry picked from commit dd33132916b283c435e05db0055db121ce7bfa3a) --- fastf1/ergast/structure.py | 9 +++++++-- fastf1/tests/test_ergast.py | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/fastf1/ergast/structure.py b/fastf1/ergast/structure.py index 9dbf455cb..f8afee4df 100644 --- a/fastf1/ergast/structure.py +++ b/fastf1/ergast/structure.py @@ -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]: diff --git a/fastf1/tests/test_ergast.py b/fastf1/tests/test_ergast.py index 94e277ac3..1b2f2514a 100644 --- a/fastf1/tests/test_ergast.py +++ b/fastf1/tests/test_ergast.py @@ -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", (