question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Docs for naturaldelta are wrong/incomplete

See original GitHub issue

What did you do?

It seems the docs for naturaldelta() are wrong/incomplete/conflicting. They specify that the first parameter should be a timedelta or an int, but the API will also accept a datetime.

What did you expect to happen?

naturaldelta() should only accept timedelta or int as documented. I expect that users should create their own timedelta inline instead of using the when parameter.

What actually happened?

A datetime is converted to a timedelta internally using the when parameter, which defaults to now. This undocumented conversion behavior is enforced by the tests.

In the current implementation, the handling of when causes and overflow for large timedeltas (#245).

What versions are you using?

  • OS: Ubuntu x64
  • Python: 3.9.6
  • Humanize: 3.13.1

Please include code that reproduces the issue.

The best reproductions are self-contained scripts with minimal dependencies.

import humanize
from datetime import timedelta, datetime

now = datetime(1, 1, 1, minute=1)
later = datetime(1, 1, 1, minute=31)

# Correct documented usage; input is timedelta.
assert humanize.naturaldelta(later - now) == "30 minutes"

# Incorrect/undocumented usage; input is pair of datetimes
assert humanize.naturaldelta(now, when=later) == "30 minutes"

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
carterboxcommented, Dec 14, 2021

I will submit a PR.

0reactions
hugovkcommented, Dec 13, 2021

We have a major version coming out soon anyway to drop Python 3.6 (https://github.com/jmoiron/humanize/pull/239), so let’s deprecate when= first and release it, even if it’s not long until it’s removed.

Would one of you like to submit a PR to deprecate it?

Let’s show a warning when if when: is true, something along the lines of:

https://github.com/jmoiron/humanize/blob/49f2cd1b8b73e4b63aac5a84b07afcb92c3ca0b7/src/humanize/__init__.py#L40-L45

And add something like “Deprecated, will be removed in humanize 4.0.” to the parameter’s docstring.

Read more comments on GitHub >

github_iconTop Results From Across the Web

humanize overflows before datetime does · Issue #245 - GitHub
When the value passed to naturaldelta() is not datetime or timedelta or cannot be ... Docs for naturaldelta are wrong/incomplete #247.
Read more >
humanize 4.4.0 - PythonFix.com
... Short scale instead of long scale for Polish locale; Docs for naturaldelta are wrong/incomplete; humanize overflows before datetime does ...
Read more >
humanize - Read the Docs
Welcome to the humanize API reference. Number · Time · Filesize · I18n. Installation. From PyPI. python3 -m pip install --upgrade humanize ...
Read more >
How to use the humanize.naturaldelta function in humanize
To help you get started, we've selected a few humanize.naturaldelta examples, based on popular ways it is used in public projects.
Read more >
Python humanize functions | PythonRepo
It seems the docs for naturaldelta() are wrong/incomplete/conflicting. They specify that the first parameter should be a timedelta or an int ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found