New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
humanize overflows before datetime does #245
Comments
This is https://stackoverflow.com/a/49267139/724176 and we're going before https://docs.python.org/3/library/datetime.html#datetime.date.min date = now - value
# date = 2021-12-02 13:13:36.763168 - 999999999 days, 0:00:00 999,999,999 days is ~2,739,726 years, so 2021 - ~2,739,726 is definitely before year 1! This takes place in date, delta = _date_and_delta(value, now=when)
if date is None:
return value
... I wonder, is it worth refactoring so we avoid that
I originally found it a bit strange to pretend things are fine when there's an error, but it's been done like this for a long time, so this is certainly one option. On the other hand, I'd guess dealing with 2 million years is pretty rare, and perhaps the user should handle the exception? Is handling an exception preferable to getting back the unaltered delta? |
When the value passed to # other stuff
else:
try:
value = int(value)
delta = dt.timedelta(seconds=value)
date = now - delta
except (ValueError, TypeError):
return None, value |
import datetime as dt
import humanize
d = dt.timedelta(days=999999999)
print(d)
h = humanize.naturaldelta(d, when=dt.date.resolution)
print(h) I can misuse the |
I'm thinking we should either let the |
After more though, I think the better option (of those two you mention) would be to raise |
Should I also open a PR to deprecate/warn users about this undocumented behavior (the returning of a timedelta instead of a string)? |
I'm a bit wary about changing the behaviour as this is an old library (we just missed the 10-year anniversary! 🍰 ) with lots of downloads, and I think the Django one some of these are based on does a similar thing. Maybe the best thing to do is document the behaviour more clearly? |
To summarize: the original issue is that an OverflowError is raised by Can I still refactor |
Yes, documentation and refactoring welcome! |
Assign me, please! Otherwise, I might loose track of this issue. |
✅ |
What did you do?
I tried to humanize a very long time.
What did you expect to happen?
I expect the time to be converted into years/millenia/eon if it is within the bounds of datetime i.e. <= 999 999 999 days, and if the number is higher than that or uncountable with the compute platform (64/32 bits) then something like "longer than countable".
At the very least, this type of error should be handled like others in
_date_and_delta
: the timedelta is returned unaltered.What actually happened?
I received an overflow error from an operation within humanize.
What versions are you using?
Please include code that reproduces the issue.
The best reproductions are self-contained scripts with minimal dependencies.
Once we discuss how to best handle this error, I'd be happy to open a PR.
The text was updated successfully, but these errors were encountered: