Wrap each import in its own backticks (#346)

This commit is contained in:
Charlie Marsh 2022-10-07 15:58:30 -04:00 committed by GitHub
parent b60768debb
commit e3a7357187
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 27 deletions

View file

@ -1721,12 +1721,8 @@ impl<'a> Checker<'a> {
let child = self.parents[defined_by]; let child = self.parents[defined_by];
let parent = defined_in.map(|defined_in| self.parents[defined_in]); let parent = defined_in.map(|defined_in| self.parents[defined_in]);
let mut check = Check::new( let fix = if matches!(self.autofix, fixer::Mode::Generate | fixer::Mode::Apply)
CheckKind::UnusedImport(full_names.join(", ")), {
self.locate_check(Range::from_located(child)),
);
if matches!(self.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
let deleted: Vec<&Stmt> = self let deleted: Vec<&Stmt> = self
.deletions .deletions
.iter() .iter()
@ -1739,14 +1735,22 @@ impl<'a> Checker<'a> {
}; };
match removal_fn(&mut self.locator, &full_names, child, parent, &deleted) { match removal_fn(&mut self.locator, &full_names, child, parent, &deleted) {
Ok(fix) => { Ok(fix) => Some(fix),
if fix.content.is_empty() || fix.content == "pass" { Err(e) => {
self.deletions.insert(defined_by); error!("Failed to fix unused imports: {}", e);
None
} }
check.amend(fix)
}
Err(e) => error!("Failed to fix unused imports: {}", e),
} }
} else {
None
};
let mut check = Check::new(
CheckKind::UnusedImport(full_names.into_iter().map(String::from).collect()),
self.locate_check(Range::from_located(child)),
);
if let Some(fix) = fix {
check.amend(fix);
} }
self.checks.push(check); self.checks.push(check);

View file

@ -366,7 +366,7 @@ impl CheckCode {
CheckCode::E902 => CheckKind::IOError("IOError: `...`".to_string()), CheckCode::E902 => CheckKind::IOError("IOError: `...`".to_string()),
CheckCode::E999 => CheckKind::SyntaxError("`...`".to_string()), CheckCode::E999 => CheckKind::SyntaxError("`...`".to_string()),
// pyflakes // pyflakes
CheckCode::F401 => CheckKind::UnusedImport("...".to_string()), CheckCode::F401 => CheckKind::UnusedImport(vec!["...".to_string()]),
CheckCode::F402 => CheckKind::ImportShadowedByLoopVar("...".to_string(), 1), CheckCode::F402 => CheckKind::ImportShadowedByLoopVar("...".to_string(), 1),
CheckCode::F403 => CheckKind::ImportStarUsed("...".to_string()), CheckCode::F403 => CheckKind::ImportStarUsed("...".to_string()),
CheckCode::F404 => CheckKind::LateFutureImport, CheckCode::F404 => CheckKind::LateFutureImport,
@ -476,7 +476,7 @@ pub enum CheckKind {
UndefinedExport(String), UndefinedExport(String),
UndefinedLocal(String), UndefinedLocal(String),
UndefinedName(String), UndefinedName(String),
UnusedImport(String), UnusedImport(Vec<String>),
UnusedVariable(String), UnusedVariable(String),
YieldOutsideFunction, YieldOutsideFunction,
// flake8-builtin // flake8-builtin
@ -764,7 +764,10 @@ impl CheckKind {
CheckKind::UndefinedName(name) => { CheckKind::UndefinedName(name) => {
format!("Undefined name `{name}`") format!("Undefined name `{name}`")
} }
CheckKind::UnusedImport(name) => format!("`{name}` imported but unused"), CheckKind::UnusedImport(names) => {
let names = names.iter().map(|name| format!("`{name}`")).join(", ");
format!("{names} imported but unused")
}
CheckKind::UnusedVariable(name) => { CheckKind::UnusedVariable(name) => {
format!("Local variable `{name}` is assigned to but never used") format!("Local variable `{name}` is assigned to but never used")
} }

View file

@ -3,7 +3,8 @@ source: src/linter.rs
expression: checks expression: checks
--- ---
- kind: - kind:
UnusedImport: functools UnusedImport:
- functools
location: location:
row: 2 row: 2
column: 1 column: 1
@ -20,7 +21,8 @@ expression: checks
column: 21 column: 21
applied: false applied: false
- kind: - kind:
UnusedImport: collections.OrderedDict UnusedImport:
- collections.OrderedDict
location: location:
row: 4 row: 4
column: 1 column: 1
@ -37,7 +39,8 @@ expression: checks
column: 2 column: 2
applied: false applied: false
- kind: - kind:
UnusedImport: logging.handlers UnusedImport:
- logging.handlers
location: location:
row: 12 row: 12
column: 1 column: 1
@ -54,7 +57,8 @@ expression: checks
column: 24 column: 24
applied: false applied: false
- kind: - kind:
UnusedImport: shelve UnusedImport:
- shelve
location: location:
row: 33 row: 33
column: 5 column: 5
@ -71,7 +75,8 @@ expression: checks
column: 1 column: 1
applied: false applied: false
- kind: - kind:
UnusedImport: importlib UnusedImport:
- importlib
location: location:
row: 34 row: 34
column: 5 column: 5
@ -79,16 +84,17 @@ expression: checks
row: 34 row: 34
column: 21 column: 21
fix: fix:
content: pass content: ""
location: location:
row: 34 row: 34
column: 5 column: 1
end_location: end_location:
row: 34 row: 35
column: 21 column: 1
applied: false applied: false
- kind: - kind:
UnusedImport: pathlib UnusedImport:
- pathlib
location: location:
row: 38 row: 38
column: 5 column: 5
@ -105,7 +111,8 @@ expression: checks
column: 1 column: 1
applied: false applied: false
- kind: - kind:
UnusedImport: pickle UnusedImport:
- pickle
location: location:
row: 53 row: 53
column: 9 column: 9

View file

@ -3,7 +3,8 @@ source: src/linter.rs
expression: checks expression: checks
--- ---
- kind: - kind:
UnusedImport: models.Nut UnusedImport:
- models.Nut
location: location:
row: 5 row: 5
column: 1 column: 1