mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Add DTZ002 documentation (#6146)
## Summary Adds documentation for DTZ002. Related to https://github.com/astral-sh/ruff/issues/2646. ## Test Plan `python scripts/test_docs_formatted.py`
This commit is contained in:
parent
764d35667f
commit
6ea3c178fd
2 changed files with 40 additions and 9 deletions
|
@ -8,6 +8,40 @@ use crate::checkers::ast::Checker;
|
||||||
|
|
||||||
use super::helpers;
|
use super::helpers;
|
||||||
|
|
||||||
|
/// ## What it does
|
||||||
|
/// Checks for usage of `datetime.datetime.today()`.
|
||||||
|
///
|
||||||
|
/// ## Why is this bad?
|
||||||
|
/// `datetime` objects are "naive" by default, in that they do not include
|
||||||
|
/// timezone information. "Naive" objects are easy to understand, but ignore
|
||||||
|
/// some aspects of reality, which can lead to subtle bugs. Timezone-aware
|
||||||
|
/// `datetime` objects are preferred, as they represent a specific moment in
|
||||||
|
/// time, unlike "naive" objects.
|
||||||
|
///
|
||||||
|
/// `datetime.datetime.today()` crates a "naive" object; instead, use
|
||||||
|
/// instead, use `datetime.datetime.now(tz=)` to create a timezone-aware
|
||||||
|
/// object.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
/// ```python
|
||||||
|
/// import datetime
|
||||||
|
///
|
||||||
|
/// datetime.datetime.today()
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Use instead:
|
||||||
|
/// ```python
|
||||||
|
/// import datetime
|
||||||
|
///
|
||||||
|
/// datetime.datetime.now(tz=datetime.timezone.utc)
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Or, for Python 3.11 and later:
|
||||||
|
/// ```python
|
||||||
|
/// import datetime
|
||||||
|
///
|
||||||
|
/// datetime.datetime.now(tz=datetime.UTC)
|
||||||
|
/// ```
|
||||||
#[violation]
|
#[violation]
|
||||||
pub struct CallDatetimeToday;
|
pub struct CallDatetimeToday;
|
||||||
|
|
||||||
|
@ -21,12 +55,6 @@ impl Violation for CallDatetimeToday {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks for `datetime.datetime.today()`. (DTZ002)
|
|
||||||
///
|
|
||||||
/// ## Why is this bad?
|
|
||||||
///
|
|
||||||
/// It uses the system local timezone.
|
|
||||||
/// Use `datetime.datetime.now(tz=)` instead.
|
|
||||||
pub(crate) fn call_datetime_today(checker: &mut Checker, func: &Expr, location: TextRange) {
|
pub(crate) fn call_datetime_today(checker: &mut Checker, func: &Expr, location: TextRange) {
|
||||||
if !checker
|
if !checker
|
||||||
.semantic()
|
.semantic()
|
||||||
|
|
|
@ -14,9 +14,12 @@ use super::helpers;
|
||||||
///
|
///
|
||||||
/// ## Why is this bad?
|
/// ## Why is this bad?
|
||||||
/// `datetime` objects are "naive" by default, in that they do not include
|
/// `datetime` objects are "naive" by default, in that they do not include
|
||||||
/// timezone information. By providing a `tzinfo`, a `datetime` can be made
|
/// timezone information. "Naive" objects are easy to understand, but ignore
|
||||||
/// timezone "aware". "Naive" objects are easy to understand, but ignore some
|
/// some aspects of reality, which can lead to subtle bugs. Timezone-aware
|
||||||
/// aspects of reality, which can lead to subtle bugs.
|
/// `datetime` objects are preferred, as they represent a specific moment in
|
||||||
|
/// time, unlike "naive" objects.
|
||||||
|
///
|
||||||
|
/// By providing a `tzinfo` value, a `datetime` can be made timezone-aware.
|
||||||
///
|
///
|
||||||
/// ## Example
|
/// ## Example
|
||||||
/// ```python
|
/// ```python
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue