Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TID252 to fix all valid module paths #3796

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from ..server import example
from .. import server
from . import logger, models
from ..protocol.UpperCaseModule import some_function
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def some_function():
pass
24 changes: 17 additions & 7 deletions crates/ruff/src/rules/flake8_tidy_imports/relative_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use ruff_macros::{derive_message_formats, violation, CacheKey};
use ruff_python_ast::helpers::{create_stmt, from_relative_import, unparse_stmt};
use ruff_python_ast::source_code::Stylist;
use ruff_python_ast::types::Range;
use ruff_python_stdlib::identifiers::is_module_name;
use ruff_python_stdlib::identifiers::is_identifier;
use ruff_python_stdlib::keyword::KWLIST;

use crate::checkers::ast::Checker;
use crate::registry::AsRule;
Expand Down Expand Up @@ -106,25 +107,34 @@ fn fix_banned_relative_import(

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:
// Require import to be a valid module:
// https://python.org/dev/peps/pep-0008/#package-and-module-names
if !call_path.iter().all(|part| is_module_name(part)) {
if !call_path
.iter()
.all(|part| is_identifier(part) && !KWLIST.contains(part))
{
return None;
}
call_path.as_slice().join(".")
} else if parts.len() > 1 {
let module = parts.pop().unwrap();
let call_path = from_relative_import(&parts, &module);
// Require import to be a valid PEP 8 module:
// Require import to be a valid module:
// https://python.org/dev/peps/pep-0008/#package-and-module-names
if !call_path.iter().all(|part| is_module_name(part)) {
if !call_path
.iter()
.all(|part| is_identifier(part) && !KWLIST.contains(part))
{
return None;
}
call_path.as_slice().join(".")
} else {
// Require import to be a valid PEP 8 module:
// Require import to be a valid module:
// https://python.org/dev/peps/pep-0008/#package-and-module-names
if !parts.iter().all(|part| is_module_name(part)) {
if !parts
.iter()
.all(|part| is_identifier(part) && !KWLIST.contains(&part.as_str()))
{
return None;
}
parts.join(".")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,25 @@ expression: diagnostics
row: 8
column: 21
parent: ~
- kind:
name: RelativeImports
body: Relative imports from parent modules are banned
suggestion: Replace relative imports from parent modules with absolute imports
fixable: true
location:
row: 10
column: 0
end_location:
row: 10
column: 52
fix:
edits:
- content: from my_package.sublib.protocol.UpperCaseModule import some_function
location:
row: 10
column: 0
end_location:
row: 10
column: 52
parent: ~