Avoid DTZ007 false-positives for non-string arguments (#1300)

This commit is contained in:
Charlie Marsh
2022-12-20 13:20:53 -05:00
committed by GitHub
parent 468ffd29fb
commit 226f682c99
3 changed files with 15 additions and 14 deletions

View File

@@ -23,6 +23,12 @@ datetime.datetime.strptime("something", "something").astimezone()
# OK
datetime.datetime.strptime("something", "%H:%M:%S%z")
# OK
datetime.datetime.strptime("something", something).astimezone()
# OK
datetime.datetime.strptime("something", something).replace(tzinfo=datetime.timezone.utc)
from datetime import datetime
# no replace orastimezone unqualified

View File

@@ -177,22 +177,17 @@ pub fn call_datetime_strptime_without_zone(
return;
}
let Some(ExprKind::Constant {
// Does the `strptime` call contain a format string with a timezone specifier?
if let Some(ExprKind::Constant {
value: Constant::Str(format),
kind: None,
}) = args.get(1).as_ref().map(|arg| &arg.node) else {
checker.add_check(Check::new(
CheckKind::CallDatetimeStrptimeWithoutZone,
location,
));
return;
}) = args.get(1).as_ref().map(|arg| &arg.node)
{
if format.contains("%z") {
return;
}
};
// Does the `strptime` call contain a format string with a timezone specifier?
if format.contains("%z") {
return;
}
let (Some(grandparent), Some(parent)) = (checker.current_expr_grandparent(), checker.current_expr_parent()) else {
checker.add_check(Check::new(
CheckKind::CallDatetimeStrptimeWithoutZone,

View File

@@ -36,10 +36,10 @@ expression: checks
fix: ~
- kind: CallDatetimeStrptimeWithoutZone
location:
row: 29
row: 35
column: 0
end_location:
row: 29
row: 35
column: 43
fix: ~