Skip to content

Commit 4d1e177

Browse files
authored
Merge branch 'main' into docs/fix-intervalunit-docstring
2 parents 2b751b5 + 839c5b2 commit 4d1e177

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

sqlmesh/utils/date.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,13 @@ def to_datetime(
194194
try:
195195
dt = datetime.strptime(str(value), DATE_INT_FMT)
196196
except ValueError:
197-
dt = datetime.fromtimestamp(epoch / 1000.0, tz=UTC)
197+
try:
198+
dt = datetime.fromtimestamp(epoch / 1000.0, tz=UTC)
199+
except (OverflowError, OSError, ValueError):
200+
# A non-finite or out-of-range epoch (e.g. "inf", 1e30, or a
201+
# huge millis value) overflows fromtimestamp. Fall through to
202+
# the ValueError below rather than leaking OverflowError/OSError.
203+
dt = None
198204

199205
if dt is None:
200206
raise ValueError(f"Could not convert `{value}` to datetime.")

tests/utils/test_date.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ def test_to_datetime() -> None:
4141
assert to_datetime("31536000000") == target
4242

4343

44+
@pytest.mark.parametrize(
45+
"value",
46+
["inf", "-inf", "1e30", "99999999999999999999", float("inf")],
47+
)
48+
def test_to_datetime_out_of_range_raises(value: t.Any) -> None:
49+
# A non-finite or out-of-range epoch overflows fromtimestamp; it must raise
50+
# the documented ValueError rather than leaking OverflowError/OSError.
51+
with pytest.raises(ValueError):
52+
to_datetime(value)
53+
54+
4455
@pytest.mark.parametrize(
4556
"expression, result",
4657
[

0 commit comments

Comments
 (0)