Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement "datetime.UTC alias" check from pyupgrade (#1341)
- Loading branch information
Showing
9 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import datetime | ||
import datetime as dt | ||
from datetime import timezone | ||
from datetime import timezone as tz | ||
|
||
print(datetime.timezone(-1)) | ||
print(timezone.utc) | ||
print(tz.utc) | ||
|
||
print(datetime.timezone.utc) | ||
print(dt.timezone.utc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use rustpython_ast::Expr; | ||
|
||
use crate::ast::helpers::{collect_call_paths, compose_call_path, dealias_call_path}; | ||
use crate::ast::types::Range; | ||
use crate::autofix::Fix; | ||
use crate::checkers::ast::Checker; | ||
use crate::checks::{Check, CheckCode, CheckKind}; | ||
|
||
/// UP017 | ||
pub fn datetime_utc_alias(checker: &mut Checker, expr: &Expr) { | ||
let dealiased_call_path = dealias_call_path(collect_call_paths(expr), &checker.import_aliases); | ||
if dealiased_call_path == ["datetime", "timezone", "utc"] { | ||
let mut check = Check::new(CheckKind::DatetimeTimezoneUTC, Range::from_located(expr)); | ||
if checker.patch(&CheckCode::UP017) { | ||
check.amend(Fix::replacement( | ||
compose_call_path(expr) | ||
.unwrap() | ||
.replace("timezone.utc", "UTC"), | ||
expr.location, | ||
expr.end_location.unwrap(), | ||
)); | ||
} | ||
checker.add_check(check); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/pyupgrade/snapshots/ruff__pyupgrade__tests__datetime_utc_alias_py311.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
source: src/pyupgrade/mod.rs | ||
expression: checks | ||
--- | ||
- kind: DatetimeTimezoneUTC | ||
location: | ||
row: 10 | ||
column: 6 | ||
end_location: | ||
row: 10 | ||
column: 27 | ||
fix: | ||
content: datetime.UTC | ||
location: | ||
row: 10 | ||
column: 6 | ||
end_location: | ||
row: 10 | ||
column: 27 | ||
- kind: DatetimeTimezoneUTC | ||
location: | ||
row: 11 | ||
column: 6 | ||
end_location: | ||
row: 11 | ||
column: 21 | ||
fix: | ||
content: dt.UTC | ||
location: | ||
row: 11 | ||
column: 6 | ||
end_location: | ||
row: 11 | ||
column: 21 | ||
|