Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ff816f
commit 34eaf34
Showing
12 changed files
with
310 additions
and
53 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,9 @@ | ||
from builtins import * | ||
from builtins import ascii, bytes, compile | ||
from builtins import str as _str | ||
from six.moves import filter, zip, zip_longest | ||
from io import open | ||
import io | ||
import six | ||
import six.moves | ||
import builtins |
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 |
---|---|---|
|
@@ -996,6 +996,7 @@ | |
"UP026", | ||
"UP027", | ||
"UP028", | ||
"UP029", | ||
"W", | ||
"W2", | ||
"W29", | ||
|
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,107 @@ | ||
use itertools::Itertools; | ||
use log::error; | ||
use rustpython_ast::{Alias, AliasData, Located}; | ||
use rustpython_parser::ast::Stmt; | ||
|
||
use crate::ast::types::Range; | ||
use crate::autofix; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::{Check, CheckKind}; | ||
|
||
const BUILTINS: &[&str] = &[ | ||
"*", | ||
"ascii", | ||
"bytes", | ||
"chr", | ||
"dict", | ||
"filter", | ||
"hex", | ||
"input", | ||
"int", | ||
"isinstance", | ||
"list", | ||
"map", | ||
"max", | ||
"min", | ||
"next", | ||
"object", | ||
"oct", | ||
"open", | ||
"pow", | ||
"range", | ||
"round", | ||
"str", | ||
"super", | ||
"zip", | ||
]; | ||
const IO: &[&str] = &["open"]; | ||
const SIX_MOVES_BUILTINS: &[&str] = BUILTINS; | ||
const SIX: &[&str] = &["callable", "next"]; | ||
const SIX_MOVES: &[&str] = &["filter", "input", "map", "range", "zip"]; | ||
|
||
/// UP029 | ||
pub fn unnecessary_builtin_import( | ||
checker: &mut Checker, | ||
stmt: &Stmt, | ||
module: &str, | ||
names: &[Located<AliasData>], | ||
) { | ||
let deprecated_names = match module { | ||
"builtins" => BUILTINS, | ||
"io" => IO, | ||
"six" => SIX, | ||
"six.moves" => SIX_MOVES, | ||
"six.moves.builtins" => SIX_MOVES_BUILTINS, | ||
_ => return, | ||
}; | ||
|
||
let mut unused_imports: Vec<&Alias> = vec![]; | ||
for alias in names { | ||
if alias.node.asname.is_some() { | ||
continue; | ||
} | ||
if deprecated_names.contains(&alias.node.name.as_str()) { | ||
unused_imports.push(alias); | ||
} | ||
} | ||
|
||
if unused_imports.is_empty() { | ||
return; | ||
} | ||
let mut check = Check::new( | ||
CheckKind::UnnecessaryBuiltinImport( | ||
unused_imports | ||
.iter() | ||
.map(|alias| alias.node.name.to_string()) | ||
.sorted() | ||
.collect(), | ||
), | ||
Range::from_located(stmt), | ||
); | ||
|
||
if checker.patch(check.kind.code()) { | ||
let deleted: Vec<&Stmt> = checker.deletions.iter().map(|node| node.0).collect(); | ||
let defined_by = checker.current_stmt(); | ||
let defined_in = checker.current_stmt_parent(); | ||
let unused_imports: Vec<String> = unused_imports | ||
.iter() | ||
.map(|alias| format!("{module}.{}", alias.node.name)) | ||
.collect(); | ||
match autofix::helpers::remove_unused_imports( | ||
unused_imports.iter().map(String::as_str), | ||
defined_by.0, | ||
defined_in.map(|node| node.0), | ||
&deleted, | ||
checker.locator, | ||
) { | ||
Ok(fix) => { | ||
if fix.content.is_empty() || fix.content == "pass" { | ||
checker.deletions.insert(defined_by.clone()); | ||
} | ||
check.amend(fix); | ||
} | ||
Err(e) => error!("Failed to remove builtin import: {e}"), | ||
} | ||
} | ||
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
79 changes: 79 additions & 0 deletions
79
src/pyupgrade/snapshots/ruff__pyupgrade__tests__UP029_UP029.py.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,79 @@ | ||
--- | ||
source: src/pyupgrade/mod.rs | ||
expression: checks | ||
--- | ||
- kind: | ||
UnnecessaryBuiltinImport: | ||
- "*" | ||
location: | ||
row: 1 | ||
column: 0 | ||
end_location: | ||
row: 1 | ||
column: 22 | ||
fix: | ||
content: "" | ||
location: | ||
row: 1 | ||
column: 0 | ||
end_location: | ||
row: 2 | ||
column: 0 | ||
parent: ~ | ||
- kind: | ||
UnnecessaryBuiltinImport: | ||
- ascii | ||
- bytes | ||
location: | ||
row: 2 | ||
column: 0 | ||
end_location: | ||
row: 2 | ||
column: 42 | ||
fix: | ||
content: from builtins import compile | ||
location: | ||
row: 2 | ||
column: 0 | ||
end_location: | ||
row: 2 | ||
column: 42 | ||
parent: ~ | ||
- kind: | ||
UnnecessaryBuiltinImport: | ||
- filter | ||
- zip | ||
location: | ||
row: 4 | ||
column: 0 | ||
end_location: | ||
row: 4 | ||
column: 46 | ||
fix: | ||
content: from six.moves import zip_longest | ||
location: | ||
row: 4 | ||
column: 0 | ||
end_location: | ||
row: 4 | ||
column: 46 | ||
parent: ~ | ||
- kind: | ||
UnnecessaryBuiltinImport: | ||
- open | ||
location: | ||
row: 5 | ||
column: 0 | ||
end_location: | ||
row: 5 | ||
column: 19 | ||
fix: | ||
content: "" | ||
location: | ||
row: 5 | ||
column: 0 | ||
end_location: | ||
row: 6 | ||
column: 0 | ||
parent: ~ | ||
|
Oops, something went wrong.