Skip to content

Commit

Permalink
Enable UTC-import for datetime-utc-alias fix
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 14, 2023
1 parent 56476df commit 7dade46
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
22 changes: 10 additions & 12 deletions crates/ruff/src/rules/pyupgrade/rules/datetime_utc_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use rustpython_parser::ast::{Expr, Ranged};

use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::collect_call_path;

use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;
use crate::registry::AsRule;

#[violation]
Expand Down Expand Up @@ -34,17 +34,15 @@ pub(crate) fn datetime_utc_alias(checker: &mut Checker, expr: &Expr) {
{
let mut diagnostic = Diagnostic::new(DatetimeTimezoneUTC, expr.range());
if checker.patch(diagnostic.kind.rule()) {
// If the reference was structured as, e.g., `datetime.timezone.utc`, then we can
// replace it with `datetime.UTC`. If `timezone` was imported via `from datetime import
// timezone`, then the replacement is more complicated.
if collect_call_path(expr).map_or(false, |call_path| {
matches!(call_path.as_slice(), ["datetime", "timezone", "utc"])
}) {
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
"datetime.UTC".to_string(),
expr.range(),
)));
}
diagnostic.try_set_fix(|| {
let (import_edit, binding) = checker.importer.get_or_import_symbol(
&ImportRequest::import_from("datetime", "UTC"),
expr.start(),
checker.semantic(),
)?;
let reference_edit = Edit::range_replacement(binding, expr.range());
Ok(Fix::suggested_edits(import_edit, [reference_edit]))
});
}
checker.diagnostics.push(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff/src/rules/pyupgrade/mod.rs
---
UP017.py:7:7: UP017 Use `datetime.UTC` alias
UP017.py:7:7: UP017 [*] Use `datetime.UTC` alias
|
6 | print(datetime.timezone(-1))
7 | print(timezone.utc)
Expand All @@ -10,7 +10,17 @@ UP017.py:7:7: UP017 Use `datetime.UTC` alias
|
= help: Convert to `datetime.UTC` alias

UP017.py:8:7: UP017 Use `datetime.UTC` alias
Suggested fix
4 4 | from datetime import timezone as tz
5 5 |
6 6 | print(datetime.timezone(-1))
7 |-print(timezone.utc)
7 |+print(datetime.UTC)
8 8 | print(tz.utc)
9 9 |
10 10 | print(datetime.timezone.utc)

UP017.py:8:7: UP017 [*] Use `datetime.UTC` alias
|
6 | print(datetime.timezone(-1))
7 | print(timezone.utc)
Expand All @@ -21,6 +31,16 @@ UP017.py:8:7: UP017 Use `datetime.UTC` alias
|
= help: Convert to `datetime.UTC` alias

Suggested fix
5 5 |
6 6 | print(datetime.timezone(-1))
7 7 | print(timezone.utc)
8 |-print(tz.utc)
8 |+print(datetime.UTC)
9 9 |
10 10 | print(datetime.timezone.utc)
11 11 | print(dt.timezone.utc)

UP017.py:10:7: UP017 [*] Use `datetime.UTC` alias
|
8 | print(tz.utc)
Expand All @@ -39,12 +59,19 @@ UP017.py:10:7: UP017 [*] Use `datetime.UTC` alias
10 |+print(datetime.UTC)
11 11 | print(dt.timezone.utc)

UP017.py:11:7: UP017 Use `datetime.UTC` alias
UP017.py:11:7: UP017 [*] Use `datetime.UTC` alias
|
10 | print(datetime.timezone.utc)
11 | print(dt.timezone.utc)
| ^^^^^^^^^^^^^^^ UP017
|
= help: Convert to `datetime.UTC` alias

Suggested fix
8 8 | print(tz.utc)
9 9 |
10 10 | print(datetime.timezone.utc)
11 |-print(dt.timezone.utc)
11 |+print(datetime.UTC)


0 comments on commit 7dade46

Please sign in to comment.