[`flake8-datetimez`] Clarify docs for several rules (#20778)

## Summary

Resolves #19384.

- Distinguishes more clearly between `date` and `datetime` objects.
- Uniformly links to the relevant Python docs from rules in this
category.

I've tried to be clearer, but there's still a contradiction in the rules
as written: we say "use timezone-aware objects", but `date`s are
inherently timezone-naive.

Also, the full docs don't always match the error message: for instance,
in [DTZ012](https://docs.astral.sh/ruff/rules/call-date-fromtimestamp/),
the example says to use:
```python
datetime.datetime.fromtimestamp(946684800, tz=datetime.UTC)
```
while `fix_title` returns "Use `datetime.datetime.fromtimestamp(ts,
tz=...)**.date()**` instead".
I have left this as it was for now.

## Test Plan
Ran `mkdocs` locally and inspected result.
This commit is contained in:
ageorgou 2025-10-10 14:02:24 +01:00 committed by GitHub
parent ae83a1fd2d
commit bbd3856de8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 19 additions and 9 deletions

View File

@ -11,15 +11,15 @@ use crate::checkers::ast::Checker;
/// Checks for usage of `datetime.date.fromtimestamp()`. /// Checks for usage of `datetime.date.fromtimestamp()`.
/// ///
/// ## Why is this bad? /// ## Why is this bad?
/// Python datetime objects can be naive or timezone-aware. While an aware /// Python date objects are naive, that is, not timezone-aware. While an aware
/// object represents a specific moment in time, a naive object does not /// object represents a specific moment in time, a naive object does not
/// contain enough information to unambiguously locate itself relative to other /// contain enough information to unambiguously locate itself relative to other
/// datetime objects. Since this can lead to errors, it is recommended to /// datetime objects. Since this can lead to errors, it is recommended to
/// always use timezone-aware objects. /// always use timezone-aware objects.
/// ///
/// `datetime.date.fromtimestamp(ts)` returns a naive datetime object. /// `datetime.date.fromtimestamp(ts)` returns a naive date object.
/// Instead, use `datetime.datetime.fromtimestamp(ts, tz=...)` to create a /// Instead, use `datetime.datetime.fromtimestamp(ts, tz=...).date()` to
/// timezone-aware object. /// create a timezone-aware datetime object and retrieve its date component.
/// ///
/// ## Example /// ## Example
/// ```python /// ```python
@ -32,14 +32,14 @@ use crate::checkers::ast::Checker;
/// ```python /// ```python
/// import datetime /// import datetime
/// ///
/// datetime.datetime.fromtimestamp(946684800, tz=datetime.timezone.utc) /// datetime.datetime.fromtimestamp(946684800, tz=datetime.timezone.utc).date()
/// ``` /// ```
/// ///
/// Or, for Python 3.11 and later: /// Or, for Python 3.11 and later:
/// ```python /// ```python
/// import datetime /// import datetime
/// ///
/// datetime.datetime.fromtimestamp(946684800, tz=datetime.UTC) /// datetime.datetime.fromtimestamp(946684800, tz=datetime.UTC).date()
/// ``` /// ```
/// ///
/// ## References /// ## References

View File

@ -11,14 +11,15 @@ use crate::checkers::ast::Checker;
/// Checks for usage of `datetime.date.today()`. /// Checks for usage of `datetime.date.today()`.
/// ///
/// ## Why is this bad? /// ## Why is this bad?
/// Python datetime objects can be naive or timezone-aware. While an aware /// Python date objects are naive, that is, not timezone-aware. While an aware
/// object represents a specific moment in time, a naive object does not /// object represents a specific moment in time, a naive object does not
/// contain enough information to unambiguously locate itself relative to other /// contain enough information to unambiguously locate itself relative to other
/// datetime objects. Since this can lead to errors, it is recommended to /// datetime objects. Since this can lead to errors, it is recommended to
/// always use timezone-aware objects. /// always use timezone-aware objects.
/// ///
/// `datetime.date.today` returns a naive datetime object. Instead, use /// `datetime.date.today` returns a naive date object without taking timezones
/// `datetime.datetime.now(tz=...).date()` to create a timezone-aware object. /// into account. Instead, use `datetime.datetime.now(tz=...).date()` to
/// create a timezone-aware object and retrieve its date component.
/// ///
/// ## Example /// ## Example
/// ```python /// ```python

View File

@ -42,6 +42,9 @@ use crate::rules::flake8_datetimez::helpers;
/// ///
/// datetime.datetime.now(tz=datetime.UTC) /// datetime.datetime.now(tz=datetime.UTC)
/// ``` /// ```
///
/// ## References
/// - [Python documentation: Aware and Naive Objects](https://docs.python.org/3/library/datetime.html#aware-and-naive-objects)
#[derive(ViolationMetadata)] #[derive(ViolationMetadata)]
pub(crate) struct CallDatetimeToday; pub(crate) struct CallDatetimeToday;

View File

@ -41,6 +41,9 @@ use crate::rules::flake8_datetimez::helpers::{self, DatetimeModuleAntipattern};
/// ///
/// datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=datetime.UTC) /// datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=datetime.UTC)
/// ``` /// ```
///
/// ## References
/// - [Python documentation: Aware and Naive Objects](https://docs.python.org/3/library/datetime.html#aware-and-naive-objects)
#[derive(ViolationMetadata)] #[derive(ViolationMetadata)]
pub(crate) struct CallDatetimeWithoutTzinfo(DatetimeModuleAntipattern); pub(crate) struct CallDatetimeWithoutTzinfo(DatetimeModuleAntipattern);

View File

@ -38,6 +38,9 @@ use crate::checkers::ast::Checker;
/// ///
/// datetime.datetime.max.replace(tzinfo=datetime.UTC) /// datetime.datetime.max.replace(tzinfo=datetime.UTC)
/// ``` /// ```
///
/// ## References
/// - [Python documentation: Aware and Naive Objects](https://docs.python.org/3/library/datetime.html#aware-and-naive-objects)
#[derive(ViolationMetadata)] #[derive(ViolationMetadata)]
pub(crate) struct DatetimeMinMax { pub(crate) struct DatetimeMinMax {
min_max: MinMax, min_max: MinMax,