Report DTZ violation on datetime.datetime.max
and min
#13217
Labels
rule
Implementing or modifying a lint rule
datetime.datetime.max
and min
#13217
Currently, ruff is properly detecting most cases where a
datetime
object is used without a timezone info, however, it does not report the use ofdatetime.max
ordatetime.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:
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 mydatetime.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.The text was updated successfully, but these errors were encountered: