Skip to content

naturaltime() and precisedelta() raise OverflowError on float('inf'), unlike naturaldelta() #524

Backlog

Description

@Yasser-Ameur

What did you do?

Passed an infinite float to the three time functions.

import humanize

for f in (humanize.naturaldelta, humanize.naturaltime, humanize.precisedelta):
    for v in (float("inf"), float("nan")):
        try:
            print(f.__name__, v, "->", repr(f(v)))
        except Exception as e:
            print(f.__name__, v, "->", type(e).__name__, e)

What did you expect to happen?

The same result from all three. Since #334 (which closed #333), naturaldelta returns a non-finite value as its string, and its docstring says so.

What actually happened?

naturaldelta inf -> 'inf'
naturaldelta nan -> 'nan'
naturaltime inf -> OverflowError cannot convert float infinity to integer
naturaltime nan -> 'nan'
precisedelta inf -> OverflowError cannot convert float infinity to integer
precisedelta nan -> 'nan'

naturaltime and precisedelta already return 'nan', so only infinity is the odd one out. It comes from _date_and_delta, where timedelta(seconds=value) raises OverflowError; naturaldelta catches that for non-finite values and the other two do not.

I have a small change that mirrors #334 in the other two functions (a finite value that is too large, such as 1e30, still raises). I have not opened it, because #448 and #456 are reworking the same path and #434 adds a test that expects naturaltime(inf) to raise. Should inf be handled like in naturaldelta, and if so would you rather it went into those PRs or a separate one?

What versions are you using?

  • OS: Windows 11
  • Python: 3.14.7
  • Humanize: main at a950045 (4.17.0)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions