Skip to content

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

@ItsDrike

Description

@ItsDrike

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions