From ba35014a06c920bae10d689530d60ff4fe3575c8 Mon Sep 17 00:00:00 2001 From: Richard Kacerek Date: Wed, 7 Oct 2026 00:23:54 +0200 Subject: [PATCH] fix: raise ParseError for out-of-range times Co-Authored-By: Claude Sonnet 5.5 --- tests/test_timetable.py | 18 ++++++++++++++++++ timetable.py | 6 +++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/test_timetable.py b/tests/test_timetable.py index 78ba6d8..74bb162 100644 --- a/tests/test_timetable.py +++ b/tests/test_timetable.py @@ -103,3 +103,21 @@ def test_cell_count_mismatch_is_a_parse_error(): ) with pytest.raises(ParseError, match="cells"): parse_page(html, "4", "X") + + +def test_invalid_hour_is_a_parse_error(): + html = HEADER.format( + ths=WORKDAY_TH, + rows='2510 ', + ) + with pytest.raises(ParseError, match="invalid time"): + parse_page(html, "4", "X") + + +def test_invalid_minute_is_a_parse_error(): + html = HEADER.format( + ths=WORKDAY_TH, + rows='0499 ', + ) + with pytest.raises(ParseError, match="invalid time"): + parse_page(html, "4", "X") diff --git a/timetable.py b/timetable.py index aff0470..d4907cd 100644 --- a/timetable.py +++ b/timetable.py @@ -93,7 +93,11 @@ def parse_page(html: str, line: str, stop: str) -> Timetable: raise ParseError(f"hour {hour}: {len(cells)} cells for {len(specs)} columns") for column, cell in zip(per_column, cells): for minute, mark in _MINUTE_RE.findall(cell): - column.append(Departure(line, stop, time(int(hour), int(minute)), mark or None)) + try: + departure_time = time(int(hour), int(minute)) + except ValueError: + raise ParseError(f"hour {hour}: invalid time {hour}:{minute}") + column.append(Departure(line, stop, departure_time, mark or None)) if not any(per_column): raise ParseError("timetable contains no departures")