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

Enable UTC-import for datetime-utc-alias fix #5100

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)