Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-datetimez] Also exempt .time() (DTZ901) #14394

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=...)`
Loading