Skip to content

Commit

Permalink
[flake8-tidy-imports] extend autofix of relative imports (#2990)
Browse files Browse the repository at this point in the history
This extends the autofix for TID252 to work with for relative imports without `module` (i.e. `from .. import`). Tested with `matplotlib` and `bokeh`.
(Previously it would panic on unwrap of the module) 

Note that pandas has [replaced](pandas-dev/pandas@6057d7a) `absolufy-imports` with `ruff` now!
  • Loading branch information
sbrugman committed Feb 17, 2023
1 parent 0dd590f commit a934d01
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

from ..protocol import commands, definitions, responses
from ..server import example
from .. import server
from . import logger, models
57 changes: 37 additions & 20 deletions crates/ruff/src/rules/flake8_tidy_imports/relative_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,50 @@ fn fix_banned_relative_import(
module_path: Option<&Vec<String>>,
stylist: &Stylist,
) -> Option<Fix> {
// Only fix is the module path is known
// Only fix is the module path is known.
if let Some(mut parts) = module_path.cloned() {
// Remove relative level from module path
// Remove relative level from module path.
for _ in 0..*level? {
parts.pop();
}

let call_path = from_relative_import(&parts, module.unwrap());
let module_name = call_path.as_slice();

// Require import to be a valid PEP 8 module:
// https://python.org/dev/peps/pep-0008/#package-and-module-names
if module_name.iter().any(|f| !is_lower_with_underscore(f)) {
return None;
}
let module_name = if let Some(module) = module {
let call_path = from_relative_import(&parts, module);
// Require import to be a valid PEP 8 module:
// https://python.org/dev/peps/pep-0008/#package-and-module-names
if !call_path.iter().all(|part| is_lower_with_underscore(part)) {
return None;
}
call_path.as_slice().join(".")
} else if parts.len() > 1 {
let module = parts.last().unwrap();
let call_path = from_relative_import(&parts, module);
// Require import to be a valid PEP 8 module:
// https://python.org/dev/peps/pep-0008/#package-and-module-names
if !call_path.iter().all(|part| is_lower_with_underscore(part)) {
return None;
}
call_path.as_slice().join(".")
} else {
// Require import to be a valid PEP 8 module:
// https://python.org/dev/peps/pep-0008/#package-and-module-names
if !parts.iter().all(|part| is_lower_with_underscore(part)) {
return None;
}
parts.join(".")
};

let content = match &stmt.node {
StmtKind::ImportFrom { names, .. } => unparse_stmt(
&create_stmt(StmtKind::ImportFrom {
module: Some(module_name.join(".")),
names: names.clone(),
level: Some(0),
}),
stylist,
),
_ => return None,
let StmtKind::ImportFrom { names, .. } = &stmt.node else {
unreachable!("Expected StmtKind::ImportFrom");
};
let content = unparse_stmt(
&create_stmt(StmtKind::ImportFrom {
module: Some(module_name),
names: names.clone(),
level: Some(0),
}),
stylist,
);

Some(Fix::replacement(
content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,23 @@ expression: diagnostics
row: 6
column: 28
parent: ~
- kind:
RelativeImports:
strictness: parents
location:
row: 7
column: 0
end_location:
row: 7
column: 21
fix:
content:
- from my_package.sublib.sublib import server
location:
row: 7
column: 0
end_location:
row: 7
column: 21
parent: ~

0 comments on commit a934d01

Please sign in to comment.