Skip to content

Inconsistencies between documentation/annotations and behavior #28

Description

@serhiy-storchaka

naturaltime() supports timedelta.

It is not documented, but humanize.naturaltime() accepts timedelta. I used it for years until type annotations were added, and MyPy started to complain. timedelta is still accepted at runtime.

>>> import datetime, humanize
>>> humanize.naturaltime(datetime.timedelta(days=5, hours=3))
'5 days ago'
>>> humanize.naturaltime(datetime.timedelta(hours=123))
'5 days ago'

I expect one of two things:

  1. Document that humanize.naturaltime() accepts timedelta, not only datetime | int and update type annotation.
  2. Make humanize.naturaltime() rejecting timedelta (of course, after some deprecation period).

Option 1 is more compatible, but option 2 looks more logical.

naturaltime() and naturaldelta() accept not only int, but float too.

>>> humanize.naturaltime(23.5)
'23 seconds ago'
>>> humanize.naturaldelta(23.5)
'23 seconds'

It is expected, since timestamp in Python is float, but it should be documented, and annotations should be updated.

naturaltime(), naturaldelta() , naturaldate() and naturalday() accept arbitrary object without error.

>>> humanize.naturaltime([1, 2, 3])
'[1, 2, 3]'
>>> humanize.naturaldelta([1, 2, 3])
'[1, 2, 3]'
>>> humanize.naturaldate([1, 2, 3])
'[1, 2, 3]'
>>> humanize.naturalday([1, 2, 3])
'[1, 2, 3]'

It is unsafe, because allows programming errors to slip unnoticed. humanize.naturalsize() raises an error, as expected.

Humanize 4.2.1

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions