diff --git a/resources/test/fixtures/flake8_datetimez/DTZ007.py b/resources/test/fixtures/flake8_datetimez/DTZ007.py index ba90b75096..6c7cffdfcf 100644 --- a/resources/test/fixtures/flake8_datetimez/DTZ007.py +++ b/resources/test/fixtures/flake8_datetimez/DTZ007.py @@ -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 diff --git a/src/flake8_datetimez/plugins.rs b/src/flake8_datetimez/plugins.rs index f2b64e2f2c..ee861c9483 100644 --- a/src/flake8_datetimez/plugins.rs +++ b/src/flake8_datetimez/plugins.rs @@ -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, diff --git a/src/flake8_datetimez/snapshots/ruff__flake8_datetimez__tests__DTZ007_DTZ007.py.snap b/src/flake8_datetimez/snapshots/ruff__flake8_datetimez__tests__DTZ007_DTZ007.py.snap index f5d86850c0..e2f4a0cd66 100644 --- a/src/flake8_datetimez/snapshots/ruff__flake8_datetimez__tests__DTZ007_DTZ007.py.snap +++ b/src/flake8_datetimez/snapshots/ruff__flake8_datetimez__tests__DTZ007_DTZ007.py.snap @@ -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: ~