Skip to content

Commit e16584e

Browse files
committed
fix(naturaldelta): only suppress OverflowError for non-finite floats
The previous fix caught all OverflowErrors, which incorrectly swallowed the documented OverflowError for truly too-large finite float values. Use math.isfinite() to distinguish inf/-inf (return unchanged) from a legitimately too-large finite value (re-raise).
1 parent fe0bf42 commit e16584e

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/humanize/time.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,14 @@ def naturaldelta(
148148
int(value) # Explicitly don't support string such as "NaN" or "inf"
149149
value = float(value)
150150
delta = dt.timedelta(seconds=value)
151-
except (ValueError, TypeError, OverflowError):
151+
except OverflowError:
152+
# int(value) raises OverflowError for non-finite floats (inf/-inf).
153+
# Like NaN they are returned unchanged. A truly too-large *finite*
154+
# float still raises, preserving the documented OverflowError contract.
155+
if not math.isfinite(value):
156+
return str(value)
157+
raise
158+
except (ValueError, TypeError):
152159
return str(value)
153160

154161
use_months = months

0 commit comments

Comments
 (0)