naturaldelta() documents that a value which cannot be converted to a
number is returned unchanged. float("nan") was handled because int(nan)
raises ValueError, but float("inf")/float("-inf") raised an uncaught
OverflowError from int(value), which the except clause omitted.
Catch OverflowError alongside ValueError/TypeError around the int()
guard so infinity is returned unchanged, symmetric with nan. The catch
is scoped to the int() conversion only, so a finite value too large for
datetime.timedelta still raises OverflowError from timedelta(), as
documented.
Fixes #333
Summary
Fixes #333.
naturaldelta() documents that a value which cannot be converted to a number is "returned unchanged." float("nan") is handled (its int() raises ValueError, which is caught), but float("inf")/float("-inf") raise an uncaught OverflowError from int(value), because the except (ValueError, TypeError) clause omitted OverflowError. The behavior was asymmetric and contradicted the docstring.
Before
After
Implementation note
The catch is scoped to the int(value) guard only. A finite value too large to convert to a datetime.timedelta (e.g. 1e40) still raises OverflowError from timedelta(), as documented under Raises: - this is verified by an added test so the non-finite guard does not swallow the legitimate overflow.
Tests
Added test_naturaldelta_nonfinite (nan, inf, -inf returned unchanged) and test_naturaldelta_overflow (finite 1e40 still raises OverflowError). Full tests/test_time.py passes (387 passed).