Skip to content

Commit

Permalink
[flake8-datetimez] Also exempt .time() (DTZ901) (#14394)
Browse files Browse the repository at this point in the history
## Summary

Resolves #14378.

## Test Plan

`cargo nextest run` and `cargo insta test`.

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
  • Loading branch information
InSyncWithFoo and charliermarsh authored Nov 18, 2024
1 parent 80f5cdc commit 3c9e76e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
datetime.datetime.max.replace(tzinfo=...)
datetime.datetime.min.replace(tzinfo=...)

datetime.datetime.max.time()
datetime.datetime.min.time()

datetime.datetime.max.time(foo=...)
datetime.datetime.min.time(foo=...)


from datetime import datetime

Expand All @@ -28,3 +34,9 @@
# No error
datetime.max.replace(tzinfo=...)
datetime.min.replace(tzinfo=...)

datetime.max.time()
datetime.min.time()

datetime.max.time(foo=...)
datetime.min.time(foo=...)
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(crate) fn datetime_max_min(checker: &mut Checker, expr: &Expr) {
_ => return,
};

if followed_by_replace_tzinfo(checker.semantic()) {
if usage_is_safe(checker.semantic()) {
return;
}

Expand All @@ -79,8 +79,8 @@ pub(crate) fn datetime_max_min(checker: &mut Checker, expr: &Expr) {
.push(Diagnostic::new(DatetimeMinMax { min_max }, expr.range()));
}

/// Check if the current expression has the pattern `foo.replace(tzinfo=bar)`.
fn followed_by_replace_tzinfo(semantic: &SemanticModel) -> bool {
/// Check if the current expression has the pattern `foo.replace(tzinfo=bar)` or `foo.time()`.
fn usage_is_safe(semantic: &SemanticModel) -> bool {
let Some(parent) = semantic.current_expression_parent() else {
return false;
};
Expand All @@ -90,7 +90,7 @@ fn followed_by_replace_tzinfo(semantic: &SemanticModel) -> bool {

match (parent, grandparent) {
(Expr::Attribute(ExprAttribute { attr, .. }), Expr::Call(ExprCall { arguments, .. })) => {
attr.as_str() == "replace" && arguments.find_keyword("tzinfo").is_some()
attr == "time" || (attr == "replace" && arguments.find_keyword("tzinfo").is_some())
}
_ => false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,40 @@ DTZ901.py:9:1: DTZ901 Use of `datetime.datetime.min` without timezone informatio
|
= help: Replace with `datetime.datetime.min.replace(tzinfo=...)`

DTZ901.py:21:1: DTZ901 Use of `datetime.datetime.max` without timezone information
DTZ901.py:27:1: DTZ901 Use of `datetime.datetime.max` without timezone information
|
20 | # Error
21 | datetime.max
26 | # Error
27 | datetime.max
| ^^^^^^^^^^^^ DTZ901
22 | datetime.min
28 | datetime.min
|
= help: Replace with `datetime.datetime.max.replace(tzinfo=...)`

DTZ901.py:22:1: DTZ901 Use of `datetime.datetime.min` without timezone information
DTZ901.py:28:1: DTZ901 Use of `datetime.datetime.min` without timezone information
|
20 | # Error
21 | datetime.max
22 | datetime.min
26 | # Error
27 | datetime.max
28 | datetime.min
| ^^^^^^^^^^^^ DTZ901
23 |
24 | datetime.max.replace(year=...)
29 |
30 | datetime.max.replace(year=...)
|
= help: Replace with `datetime.datetime.min.replace(tzinfo=...)`

DTZ901.py:24:1: DTZ901 Use of `datetime.datetime.max` without timezone information
DTZ901.py:30:1: DTZ901 Use of `datetime.datetime.max` without timezone information
|
22 | datetime.min
23 |
24 | datetime.max.replace(year=...)
28 | datetime.min
29 |
30 | datetime.max.replace(year=...)
| ^^^^^^^^^^^^ DTZ901
25 | datetime.min.replace(hour=...)
31 | datetime.min.replace(hour=...)
|
= help: Replace with `datetime.datetime.max.replace(tzinfo=...)`

DTZ901.py:25:1: DTZ901 Use of `datetime.datetime.min` without timezone information
DTZ901.py:31:1: DTZ901 Use of `datetime.datetime.min` without timezone information
|
24 | datetime.max.replace(year=...)
25 | datetime.min.replace(hour=...)
30 | datetime.max.replace(year=...)
31 | datetime.min.replace(hour=...)
| ^^^^^^^^^^^^ DTZ901
|
= help: Replace with `datetime.datetime.min.replace(tzinfo=...)`

0 comments on commit 3c9e76e

Please sign in to comment.