mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Avoid auto-fixing unused imports in __init__.py (#489)
This commit is contained in:
parent
bad2d7ba85
commit
389fe1ff64
5 changed files with 54 additions and 26 deletions
|
@ -2115,19 +2115,31 @@ impl<'a> Checker<'a> {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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(
|
let mut check = Check::new(
|
||||||
CheckKind::UnusedImport(full_names.into_iter().map(String::from).collect()),
|
CheckKind::UnusedImport(
|
||||||
|
full_names.into_iter().map(String::from).collect(),
|
||||||
|
false,
|
||||||
|
),
|
||||||
self.locate_check(Range::from_located(child)),
|
self.locate_check(Range::from_located(child)),
|
||||||
);
|
);
|
||||||
if let Some(fix) = fix {
|
if let Some(fix) = fix {
|
||||||
check.amend(fix);
|
check.amend(fix);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.checks.push(check);
|
self.checks.push(check);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn check_docstrings(&mut self) {
|
fn check_docstrings(&mut self) {
|
||||||
while let Some((docstring, visibility)) = self.docstrings.pop() {
|
while let Some((docstring, visibility)) = self.docstrings.pop() {
|
||||||
|
|
|
@ -265,7 +265,7 @@ pub enum CheckKind {
|
||||||
UndefinedExport(String),
|
UndefinedExport(String),
|
||||||
UndefinedLocal(String),
|
UndefinedLocal(String),
|
||||||
UndefinedName(String),
|
UndefinedName(String),
|
||||||
UnusedImport(Vec<String>),
|
UnusedImport(Vec<String>, bool),
|
||||||
UnusedVariable(String),
|
UnusedVariable(String),
|
||||||
YieldOutsideFunction,
|
YieldOutsideFunction,
|
||||||
// flake8-builtins
|
// flake8-builtins
|
||||||
|
@ -402,7 +402,7 @@ impl CheckCode {
|
||||||
CheckCode::W292 => CheckKind::NoNewLineAtEndOfFile,
|
CheckCode::W292 => CheckKind::NoNewLineAtEndOfFile,
|
||||||
CheckCode::W605 => CheckKind::InvalidEscapeSequence('c'),
|
CheckCode::W605 => CheckKind::InvalidEscapeSequence('c'),
|
||||||
// pyflakes
|
// pyflakes
|
||||||
CheckCode::F401 => CheckKind::UnusedImport(vec!["...".to_string()]),
|
CheckCode::F401 => CheckKind::UnusedImport(vec!["...".to_string()], false),
|
||||||
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,
|
||||||
|
@ -751,7 +751,7 @@ impl CheckKind {
|
||||||
CheckKind::UndefinedExport(_) => &CheckCode::F822,
|
CheckKind::UndefinedExport(_) => &CheckCode::F822,
|
||||||
CheckKind::UndefinedLocal(_) => &CheckCode::F823,
|
CheckKind::UndefinedLocal(_) => &CheckCode::F823,
|
||||||
CheckKind::UndefinedName(_) => &CheckCode::F821,
|
CheckKind::UndefinedName(_) => &CheckCode::F821,
|
||||||
CheckKind::UnusedImport(_) => &CheckCode::F401,
|
CheckKind::UnusedImport(_, _) => &CheckCode::F401,
|
||||||
CheckKind::UnusedVariable(_) => &CheckCode::F841,
|
CheckKind::UnusedVariable(_) => &CheckCode::F841,
|
||||||
CheckKind::YieldOutsideFunction => &CheckCode::F704,
|
CheckKind::YieldOutsideFunction => &CheckCode::F704,
|
||||||
// pycodestyle warnings
|
// pycodestyle warnings
|
||||||
|
@ -980,10 +980,14 @@ impl CheckKind {
|
||||||
CheckKind::UndefinedName(name) => {
|
CheckKind::UndefinedName(name) => {
|
||||||
format!("Undefined name `{name}`")
|
format!("Undefined name `{name}`")
|
||||||
}
|
}
|
||||||
CheckKind::UnusedImport(names) => {
|
CheckKind::UnusedImport(names, in_init_py) => {
|
||||||
let names = names.iter().map(|name| format!("`{name}`")).join(", ");
|
let names = names.iter().map(|name| format!("`{name}`")).join(", ");
|
||||||
|
if *in_init_py {
|
||||||
|
format!("{names} imported but unused and missing from `__all__`")
|
||||||
|
} else {
|
||||||
format!("{names} imported but unused")
|
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")
|
||||||
}
|
}
|
||||||
|
@ -1338,7 +1342,7 @@ impl CheckKind {
|
||||||
| CheckKind::SuperCallWithParameters
|
| CheckKind::SuperCallWithParameters
|
||||||
| CheckKind::TypeOfPrimitive(_)
|
| CheckKind::TypeOfPrimitive(_)
|
||||||
| CheckKind::UnnecessaryAbspath
|
| CheckKind::UnnecessaryAbspath
|
||||||
| CheckKind::UnusedImport(_)
|
| CheckKind::UnusedImport(_, false)
|
||||||
| CheckKind::UnusedLoopControlVariable(_)
|
| CheckKind::UnusedLoopControlVariable(_)
|
||||||
| CheckKind::UnusedNOQA(_)
|
| CheckKind::UnusedNOQA(_)
|
||||||
| CheckKind::UsePEP585Annotation(_)
|
| CheckKind::UsePEP585Annotation(_)
|
||||||
|
|
|
@ -4,7 +4,8 @@ expression: checks
|
||||||
---
|
---
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- functools
|
- - functools
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 2
|
row: 2
|
||||||
column: 1
|
column: 1
|
||||||
|
@ -23,7 +24,8 @@ expression: checks
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- collections.OrderedDict
|
- - collections.OrderedDict
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 4
|
row: 4
|
||||||
column: 1
|
column: 1
|
||||||
|
@ -42,7 +44,8 @@ expression: checks
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- logging.handlers
|
- - logging.handlers
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 12
|
row: 12
|
||||||
column: 1
|
column: 1
|
||||||
|
@ -61,7 +64,8 @@ expression: checks
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- shelve
|
- - shelve
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 33
|
row: 33
|
||||||
column: 5
|
column: 5
|
||||||
|
@ -80,7 +84,8 @@ expression: checks
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- importlib
|
- - importlib
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 34
|
row: 34
|
||||||
column: 5
|
column: 5
|
||||||
|
@ -99,7 +104,8 @@ expression: checks
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- pathlib
|
- - pathlib
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 38
|
row: 38
|
||||||
column: 5
|
column: 5
|
||||||
|
@ -118,7 +124,8 @@ expression: checks
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- pickle
|
- - pickle
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 53
|
row: 53
|
||||||
column: 9
|
column: 9
|
||||||
|
|
|
@ -4,7 +4,8 @@ expression: checks
|
||||||
---
|
---
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- a.b.c
|
- - a.b.c
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 2
|
row: 2
|
||||||
column: 1
|
column: 1
|
||||||
|
@ -23,7 +24,8 @@ expression: checks
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- d.e.f
|
- - d.e.f
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 3
|
row: 3
|
||||||
column: 1
|
column: 1
|
||||||
|
@ -42,7 +44,8 @@ expression: checks
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- h.i
|
- - h.i
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 4
|
row: 4
|
||||||
column: 1
|
column: 1
|
||||||
|
@ -61,7 +64,8 @@ expression: checks
|
||||||
applied: false
|
applied: false
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- j.k
|
- - j.k
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 5
|
row: 5
|
||||||
column: 1
|
column: 1
|
||||||
|
|
|
@ -4,7 +4,8 @@ expression: checks
|
||||||
---
|
---
|
||||||
- kind:
|
- kind:
|
||||||
UnusedImport:
|
UnusedImport:
|
||||||
- models.Nut
|
- - models.Nut
|
||||||
|
- false
|
||||||
location:
|
location:
|
||||||
row: 5
|
row: 5
|
||||||
column: 1
|
column: 1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue