-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93cfce6
commit c2921e9
Showing
10 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
3 changes: 3 additions & 0 deletions
3
crates/ruff/resources/test/fixtures/pylint/import_self/module.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import import_self.module | ||
from import_self import module | ||
from . import module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use rustpython_parser::ast::Alias; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::resolve_imported_module_path; | ||
|
||
#[violation] | ||
pub struct ImportSelf { | ||
pub name: String, | ||
} | ||
|
||
impl Violation for ImportSelf { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Self { name } = self; | ||
format!("Module `{name}` imports itself") | ||
} | ||
} | ||
|
||
/// PLW0406 | ||
pub fn import_self(alias: &Alias, module_path: Option<&[String]>) -> Option<Diagnostic> { | ||
let Some(module_path) = module_path else { | ||
return None; | ||
}; | ||
|
||
if alias.node.name.split('.').eq(module_path) { | ||
return Some(Diagnostic::new( | ||
ImportSelf { | ||
name: alias.node.name.clone(), | ||
}, | ||
alias.range(), | ||
)); | ||
} | ||
|
||
None | ||
} | ||
|
||
/// PLW0406 | ||
pub fn import_from_self( | ||
level: Option<usize>, | ||
module: Option<&str>, | ||
names: &[Alias], | ||
module_path: Option<&[String]>, | ||
) -> Option<Diagnostic> { | ||
let Some(module_path) = module_path else { | ||
return None; | ||
}; | ||
let Some(imported_module_path) = resolve_imported_module_path(level, module, Some(module_path)) else { | ||
return None; | ||
}; | ||
|
||
if imported_module_path | ||
.split('.') | ||
.eq(&module_path[..module_path.len() - 1]) | ||
{ | ||
if let Some(alias) = names | ||
.iter() | ||
.find(|alias| alias.node.name == module_path[module_path.len() - 1]) | ||
{ | ||
return Some(Diagnostic::new( | ||
ImportSelf { | ||
name: format!("{}.{}", imported_module_path, alias.node.name), | ||
}, | ||
alias.range(), | ||
)); | ||
} | ||
} | ||
|
||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...rc/rules/pylint/snapshots/ruff__rules__pylint__tests__PLW0406_import_self__module.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
--- | ||
module.py:1:8: PLW0406 Module `import_self.module` imports itself | ||
| | ||
1 | import import_self.module | ||
| ^^^^^^^^^^^^^^^^^^ PLW0406 | ||
2 | from import_self import module | ||
3 | from . import module | ||
| | ||
|
||
module.py:2:25: PLW0406 Module `import_self.module` imports itself | ||
| | ||
2 | import import_self.module | ||
3 | from import_self import module | ||
| ^^^^^^ PLW0406 | ||
4 | from . import module | ||
| | ||
|
||
module.py:3:15: PLW0406 Module `import_self.module` imports itself | ||
| | ||
3 | import import_self.module | ||
4 | from import_self import module | ||
5 | from . import module | ||
| ^^^^^^ PLW0406 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.