mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-20 04:29:47 +00:00
[flake8-datetimez] Clarify docs for several rules (#20778)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / ty completion evaluation (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks walltime (small|large) (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks walltime (medium|multithreaded) (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / ty completion evaluation (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks walltime (small|large) (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks walltime (medium|multithreaded) (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
## 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:
parent
ae83a1fd2d
commit
bbd3856de8
5 changed files with 19 additions and 9 deletions
|
|
@ -11,15 +11,15 @@ use crate::checkers::ast::Checker;
|
|||
/// Checks for usage of `datetime.date.fromtimestamp()`.
|
||||
///
|
||||
/// ## 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
|
||||
/// contain enough information to unambiguously locate itself relative to other
|
||||
/// datetime objects. Since this can lead to errors, it is recommended to
|
||||
/// always use timezone-aware objects.
|
||||
///
|
||||
/// `datetime.date.fromtimestamp(ts)` returns a naive datetime object.
|
||||
/// Instead, use `datetime.datetime.fromtimestamp(ts, tz=...)` to create a
|
||||
/// timezone-aware object.
|
||||
/// `datetime.date.fromtimestamp(ts)` returns a naive date object.
|
||||
/// Instead, use `datetime.datetime.fromtimestamp(ts, tz=...).date()` to
|
||||
/// create a timezone-aware datetime object and retrieve its date component.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
|
|
@ -32,14 +32,14 @@ use crate::checkers::ast::Checker;
|
|||
/// ```python
|
||||
/// 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:
|
||||
/// ```python
|
||||
/// import datetime
|
||||
///
|
||||
/// datetime.datetime.fromtimestamp(946684800, tz=datetime.UTC)
|
||||
/// datetime.datetime.fromtimestamp(946684800, tz=datetime.UTC).date()
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
|
|
|
|||
|
|
@ -11,14 +11,15 @@ use crate::checkers::ast::Checker;
|
|||
/// Checks for usage of `datetime.date.today()`.
|
||||
///
|
||||
/// ## 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
|
||||
/// contain enough information to unambiguously locate itself relative to other
|
||||
/// datetime objects. Since this can lead to errors, it is recommended to
|
||||
/// always use timezone-aware objects.
|
||||
///
|
||||
/// `datetime.date.today` returns a naive datetime object. Instead, use
|
||||
/// `datetime.datetime.now(tz=...).date()` to create a timezone-aware object.
|
||||
/// `datetime.date.today` returns a naive date object without taking timezones
|
||||
/// into account. Instead, use `datetime.datetime.now(tz=...).date()` to
|
||||
/// create a timezone-aware object and retrieve its date component.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
|
|
|
|||
|
|
@ -42,6 +42,9 @@ use crate::rules::flake8_datetimez::helpers;
|
|||
///
|
||||
/// 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)]
|
||||
pub(crate) struct CallDatetimeToday;
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,9 @@ use crate::rules::flake8_datetimez::helpers::{self, DatetimeModuleAntipattern};
|
|||
///
|
||||
/// 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)]
|
||||
pub(crate) struct CallDatetimeWithoutTzinfo(DatetimeModuleAntipattern);
|
||||
|
||||
|
|
|
|||
|
|
@ -38,6 +38,9 @@ use crate::checkers::ast::Checker;
|
|||
///
|
||||
/// 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)]
|
||||
pub(crate) struct DatetimeMinMax {
|
||||
min_max: MinMax,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue