Skip to content

Commit

Permalink
Avoid auto-fixing unused imports in __init__.py (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Oct 27, 2022
1 parent bad2d7b commit 389fe1f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
28 changes: 20 additions & 8 deletions src/check_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2115,15 +2115,27 @@ impl<'a> Checker<'a> {
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);
if self.path.ends_with("__init__.py") {
self.checks.push(Check::new(
CheckKind::UnusedImport(
full_names.into_iter().map(String::from).collect(),
true,
),
self.locate_check(Range::from_located(child)),
));
} else {
let mut check = Check::new(
CheckKind::UnusedImport(
full_names.into_iter().map(String::from).collect(),
false,
),
self.locate_check(Range::from_located(child)),
);
if let Some(fix) = fix {
check.amend(fix);
}
self.checks.push(check);
}

self.checks.push(check);
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ pub enum CheckKind {
UndefinedExport(String),
UndefinedLocal(String),
UndefinedName(String),
UnusedImport(Vec<String>),
UnusedImport(Vec<String>, bool),
UnusedVariable(String),
YieldOutsideFunction,
// flake8-builtins
Expand Down Expand Up @@ -402,7 +402,7 @@ impl CheckCode {
CheckCode::W292 => CheckKind::NoNewLineAtEndOfFile,
CheckCode::W605 => CheckKind::InvalidEscapeSequence('c'),
// pyflakes
CheckCode::F401 => CheckKind::UnusedImport(vec!["...".to_string()]),
CheckCode::F401 => CheckKind::UnusedImport(vec!["...".to_string()], false),
CheckCode::F402 => CheckKind::ImportShadowedByLoopVar("...".to_string(), 1),
CheckCode::F403 => CheckKind::ImportStarUsed("...".to_string()),
CheckCode::F404 => CheckKind::LateFutureImport,
Expand Down Expand Up @@ -751,7 +751,7 @@ impl CheckKind {
CheckKind::UndefinedExport(_) => &CheckCode::F822,
CheckKind::UndefinedLocal(_) => &CheckCode::F823,
CheckKind::UndefinedName(_) => &CheckCode::F821,
CheckKind::UnusedImport(_) => &CheckCode::F401,
CheckKind::UnusedImport(_, _) => &CheckCode::F401,
CheckKind::UnusedVariable(_) => &CheckCode::F841,
CheckKind::YieldOutsideFunction => &CheckCode::F704,
// pycodestyle warnings
Expand Down Expand Up @@ -980,9 +980,13 @@ impl CheckKind {
CheckKind::UndefinedName(name) => {
format!("Undefined name `{name}`")
}
CheckKind::UnusedImport(names) => {
CheckKind::UnusedImport(names, in_init_py) => {
let names = names.iter().map(|name| format!("`{name}`")).join(", ");
format!("{names} imported but unused")
if *in_init_py {
format!("{names} imported but unused and missing from `__all__`")
} else {
format!("{names} imported but unused")
}
}
CheckKind::UnusedVariable(name) => {
format!("Local variable `{name}` is assigned to but never used")
Expand Down Expand Up @@ -1338,7 +1342,7 @@ impl CheckKind {
| CheckKind::SuperCallWithParameters
| CheckKind::TypeOfPrimitive(_)
| CheckKind::UnnecessaryAbspath
| CheckKind::UnusedImport(_)
| CheckKind::UnusedImport(_, false)
| CheckKind::UnusedLoopControlVariable(_)
| CheckKind::UnusedNOQA(_)
| CheckKind::UsePEP585Annotation(_)
Expand Down
21 changes: 14 additions & 7 deletions src/snapshots/ruff__linter__tests__F401_F401_0.py.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ expression: checks
---
- kind:
UnusedImport:
- functools
- - functools
- false
location:
row: 2
column: 1
Expand All @@ -23,7 +24,8 @@ expression: checks
applied: false
- kind:
UnusedImport:
- collections.OrderedDict
- - collections.OrderedDict
- false
location:
row: 4
column: 1
Expand All @@ -42,7 +44,8 @@ expression: checks
applied: false
- kind:
UnusedImport:
- logging.handlers
- - logging.handlers
- false
location:
row: 12
column: 1
Expand All @@ -61,7 +64,8 @@ expression: checks
applied: false
- kind:
UnusedImport:
- shelve
- - shelve
- false
location:
row: 33
column: 5
Expand All @@ -80,7 +84,8 @@ expression: checks
applied: false
- kind:
UnusedImport:
- importlib
- - importlib
- false
location:
row: 34
column: 5
Expand All @@ -99,7 +104,8 @@ expression: checks
applied: false
- kind:
UnusedImport:
- pathlib
- - pathlib
- false
location:
row: 38
column: 5
Expand All @@ -118,7 +124,8 @@ expression: checks
applied: false
- kind:
UnusedImport:
- pickle
- - pickle
- false
location:
row: 53
column: 9
Expand Down
12 changes: 8 additions & 4 deletions src/snapshots/ruff__linter__tests__F401_F401_5.py.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ expression: checks
---
- kind:
UnusedImport:
- a.b.c
- - a.b.c
- false
location:
row: 2
column: 1
Expand All @@ -23,7 +24,8 @@ expression: checks
applied: false
- kind:
UnusedImport:
- d.e.f
- - d.e.f
- false
location:
row: 3
column: 1
Expand All @@ -42,7 +44,8 @@ expression: checks
applied: false
- kind:
UnusedImport:
- h.i
- - h.i
- false
location:
row: 4
column: 1
Expand All @@ -61,7 +64,8 @@ expression: checks
applied: false
- kind:
UnusedImport:
- j.k
- - j.k
- false
location:
row: 5
column: 1
Expand Down
3 changes: 2 additions & 1 deletion src/snapshots/ruff__linter__tests__future_annotations.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ expression: checks
---
- kind:
UnusedImport:
- models.Nut
- - models.Nut
- false
location:
row: 5
column: 1
Expand Down

0 comments on commit 389fe1f

Please sign in to comment.