-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-datetimez
] Usages of datetime.max
/datetime.min
(DTZ901
)
- Loading branch information
1 parent
b8a6518
commit d4f86d7
Showing
8 changed files
with
238 additions
and
3 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
crates/ruff_linter/resources/test/fixtures/flake8_datetimez/DTZ901.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import datetime | ||
|
||
|
||
# Error | ||
datetime.datetime.max | ||
datetime.datetime.min | ||
|
||
datetime.datetime.max.replace(year=...) | ||
datetime.datetime.min.replace(hour=...) | ||
|
||
|
||
# No error | ||
datetime.datetime.max.replace(tzinfo=...) | ||
datetime.datetime.min.replace(tzinfo=...) | ||
|
||
|
||
from datetime import datetime | ||
|
||
|
||
# Error | ||
datetime.max | ||
datetime.min | ||
|
||
datetime.max.replace(year=...) | ||
datetime.min.replace(hour=...) | ||
|
||
|
||
# No error | ||
datetime.max.replace(tzinfo=...) | ||
datetime.min.replace(tzinfo=...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
crates/ruff_linter/src/rules/flake8_datetimez/rules/use_datetime_max_min.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use crate::checkers::ast::Checker; | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{Expr, ExprAttribute, ExprCall}; | ||
use ruff_python_semantic::Modules; | ||
use ruff_text_size::Ranged; | ||
use std::fmt::{Display, Formatter}; | ||
|
||
#[derive(Debug, Eq, PartialEq)] | ||
enum MaxMin { | ||
/// `datetime.datetime.max` | ||
Max, | ||
/// `datetime.datetime.min` | ||
Min, | ||
} | ||
|
||
impl MaxMin { | ||
fn from(attr: &str) -> Self { | ||
match attr { | ||
"max" => Self::Max, | ||
"min" => Self::Min, | ||
_ => panic!("Unexpected argument for MaxMin"), | ||
} | ||
} | ||
} | ||
|
||
impl Display for MaxMin { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
let name = match self { | ||
MaxMin::Max => "max", | ||
MaxMin::Min => "min", | ||
}; | ||
|
||
write!(f, "{name}") | ||
} | ||
} | ||
|
||
/// ## What it does | ||
/// Checks for usages of `datetime.datetime.max` and `datetime.datetime.min`. | ||
/// | ||
/// ## Why is this bad? | ||
/// `datetime.max` and `datetime.min` are constants with no timezone information. | ||
/// Therefore, operations on them might fail unexpectedly: | ||
/// | ||
/// ```python | ||
/// # Timezone: UTC-14 | ||
/// datetime.max.timestamp() # ValueError: year 10000 is out of range | ||
/// datetime.min.timestamp() # ValueError: year 0 is out of range | ||
/// ``` | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// datetime.max | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// datetime.max.replace(tzinfo=datetime.UTC) | ||
/// ``` | ||
#[violation] | ||
pub struct UseDatetimeMaxMin(MaxMin); | ||
|
||
impl Violation for UseDatetimeMaxMin { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`datetime.datetime.{}` used", self.0) | ||
} | ||
} | ||
|
||
/// DTZ901 | ||
pub(crate) fn use_datetime_max_min(checker: &mut Checker, expr: &Expr) { | ||
let semantic = checker.semantic(); | ||
|
||
if !semantic.seen_module(Modules::DATETIME) { | ||
return; | ||
} | ||
|
||
let Some(qualified_name) = semantic.resolve_qualified_name(expr) else { | ||
return; | ||
}; | ||
|
||
let maxmin = match qualified_name.segments() { | ||
["datetime", "datetime", attr @ ("max" | "min")] => MaxMin::from(attr), | ||
_ => return, | ||
}; | ||
|
||
if followed_by_replace_tzinfo(checker) { | ||
return; | ||
} | ||
|
||
let diagnostic = Diagnostic::new(UseDatetimeMaxMin(maxmin), expr.range()); | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} | ||
|
||
/// Check if the current expression has the pattern `foo.replace(tzinfo=bar)`. | ||
pub(super) fn followed_by_replace_tzinfo(checker: &Checker) -> bool { | ||
let semantic = checker.semantic(); | ||
|
||
let Some(parent) = semantic.current_expression_parent() else { | ||
return false; | ||
}; | ||
let Some(grandparent) = semantic.current_expression_grandparent() else { | ||
return false; | ||
}; | ||
|
||
match (parent, grandparent) { | ||
(Expr::Attribute(ExprAttribute { attr, .. }), Expr::Call(ExprCall { arguments, .. })) => { | ||
attr.as_str() == "replace" && matches!(arguments.find_keyword("tzinfo"), Some(..)) | ||
} | ||
_ => false, | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...e8_datetimez/snapshots/ruff_linter__rules__flake8_datetimez__tests__DTZ901_DTZ901.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_datetimez/mod.rs | ||
snapshot_kind: text | ||
--- | ||
DTZ901.py:5:1: DTZ901 `datetime.datetime.max` used | ||
| | ||
4 | # Error | ||
5 | datetime.datetime.max | ||
| ^^^^^^^^^^^^^^^^^^^^^ DTZ901 | ||
6 | datetime.datetime.min | ||
| | ||
|
||
DTZ901.py:6:1: DTZ901 `datetime.datetime.min` used | ||
| | ||
4 | # Error | ||
5 | datetime.datetime.max | ||
6 | datetime.datetime.min | ||
| ^^^^^^^^^^^^^^^^^^^^^ DTZ901 | ||
7 | | ||
8 | datetime.datetime.max.replace(year=...) | ||
| | ||
|
||
DTZ901.py:8:1: DTZ901 `datetime.datetime.max` used | ||
| | ||
6 | datetime.datetime.min | ||
7 | | ||
8 | datetime.datetime.max.replace(year=...) | ||
| ^^^^^^^^^^^^^^^^^^^^^ DTZ901 | ||
9 | datetime.datetime.min.replace(hour=...) | ||
| | ||
|
||
DTZ901.py:9:1: DTZ901 `datetime.datetime.min` used | ||
| | ||
8 | datetime.datetime.max.replace(year=...) | ||
9 | datetime.datetime.min.replace(hour=...) | ||
| ^^^^^^^^^^^^^^^^^^^^^ DTZ901 | ||
| | ||
|
||
DTZ901.py:21:1: DTZ901 `datetime.datetime.max` used | ||
| | ||
20 | # Error | ||
21 | datetime.max | ||
| ^^^^^^^^^^^^ DTZ901 | ||
22 | datetime.min | ||
| | ||
|
||
DTZ901.py:22:1: DTZ901 `datetime.datetime.min` used | ||
| | ||
20 | # Error | ||
21 | datetime.max | ||
22 | datetime.min | ||
| ^^^^^^^^^^^^ DTZ901 | ||
23 | | ||
24 | datetime.max.replace(year=...) | ||
| | ||
|
||
DTZ901.py:24:1: DTZ901 `datetime.datetime.max` used | ||
| | ||
22 | datetime.min | ||
23 | | ||
24 | datetime.max.replace(year=...) | ||
| ^^^^^^^^^^^^ DTZ901 | ||
25 | datetime.min.replace(hour=...) | ||
| | ||
|
||
DTZ901.py:25:1: DTZ901 `datetime.datetime.min` used | ||
| | ||
24 | datetime.max.replace(year=...) | ||
25 | datetime.min.replace(hour=...) | ||
| ^^^^^^^^^^^^ DTZ901 | ||
| |