Skip to content
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

Report DTZ violation on datetime.datetime.max and min #13217

Closed
ItsDrike opened this issue Sep 2, 2024 · 0 comments · Fixed by #14288
Closed

Report DTZ violation on datetime.datetime.max and min #13217

ItsDrike opened this issue Sep 2, 2024 · 0 comments · Fixed by #14288
Labels
rule Implementing or modifying a lint rule

Comments

@ItsDrike
Copy link

ItsDrike commented Sep 2, 2024

Currently, ruff is properly detecting most cases where a datetime object is used without a timezone info, however, it does not report the use of datetime.max or datetime.min, which are constants that don't have a timezone set, which could end up causing pretty annoying and confusing issues.

One example that I've recently ran into:

from datetime import datetime

MAX_TIME = datetime.max.timestamp()  # ValueError: year 10000 is out of range

This happens because the datetime here was blindly constructed with no tz info to be: datetime.datetime(9999, 12, 31, 23, 59, 59, 999999), however, my timezone is UTC+2, and timestamp is always calculated in UTC, yet the overflow check there is just numeric for the 10k year. This means I end up having my datetime.max over the max UTC timestamp by 2 hours. What people should instead do here is something like: datetime.max.replace(tzinfo=UTC).

In my case, I was able to catch this quickly, because of the exception, however, for people with timezones that are smaller than UTC, rather than bigger, they wouldn't even notice something like this and it could lead to inconsistencies in the max timestamps.

It might therefore be worth it to have ruff report the use of these constants without replace, with a new DTZ rule.

@ItsDrike ItsDrike changed the title Report DTZ005 on datetime.datetime.max and min Report DTZ violation on datetime.datetime.max and min Sep 2, 2024
@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants