-
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.
## Summary Adds [`non-ascii-module-import` / `C2403`](https://pylint.pycqa.org/en/latest/user_guide/messages/convention/non-ascii-module-import.html) See #970 ## Test Plan `cargo test` and manually
- Loading branch information
1 parent
348b649
commit 7a5f988
Showing
8 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_module_import.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,17 @@ | ||
from os.path import join as łos # Error | ||
from os.path import join as los # OK | ||
|
||
import os.path.join as łos # Error | ||
import os.path.join as los # OK | ||
|
||
import os.path.łos # Error (recommend an ASCII alias) | ||
import os.path.los # OK | ||
|
||
from os.path import łos # Error (recommend an ASCII alias) | ||
from os.path import los # OK | ||
|
||
from os.path.łos import foo # OK | ||
from os.path.los import foo # OK | ||
|
||
from os.path import łos as foo # OK | ||
from os.path import los as foo # OK |
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
93 changes: 93 additions & 0 deletions
93
crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs
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,93 @@ | ||
use ruff_python_ast::Alias; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for the use of non-ASCII characters in import statements. | ||
/// | ||
/// ## Why is this bad? | ||
/// Pylint discourages the use of non-ASCII characters in symbol names as | ||
/// they can cause confusion and compatibility issues. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import bár | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import bar | ||
/// ``` | ||
/// | ||
/// If the module is third-party, use an ASCII-only alias: | ||
/// ```python | ||
/// import bár as bar | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [PEP 672](https://peps.python.org/pep-0672/) | ||
#[violation] | ||
pub struct NonAsciiImportName { | ||
name: String, | ||
kind: Kind, | ||
} | ||
|
||
impl Violation for NonAsciiImportName { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Self { name, kind } = self; | ||
match kind { | ||
Kind::Aliased => { | ||
format!( | ||
"Module alias `{name}` contains a non-ASCII character, use an ASCII-only alias" | ||
) | ||
} | ||
Kind::Unaliased => { | ||
format!( | ||
"Module name `{name}` contains a non-ASCII character, use an ASCII-only alias" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
enum Kind { | ||
/// The import uses a non-ASCII alias (e.g., `import foo as bár`). | ||
Aliased, | ||
/// The imported module is non-ASCII, and could be given an ASCII alias (e.g., `import bár`). | ||
Unaliased, | ||
} | ||
|
||
/// PLC2403 | ||
pub(crate) fn non_ascii_module_import(checker: &mut Checker, alias: &Alias) { | ||
if let Some(asname) = &alias.asname { | ||
if asname.as_str().is_ascii() { | ||
return; | ||
} | ||
|
||
checker.diagnostics.push(Diagnostic::new( | ||
NonAsciiImportName { | ||
name: asname.to_string(), | ||
kind: Kind::Aliased, | ||
}, | ||
asname.range(), | ||
)); | ||
} else { | ||
if alias.name.as_str().is_ascii() { | ||
return; | ||
} | ||
|
||
checker.diagnostics.push(Diagnostic::new( | ||
NonAsciiImportName { | ||
name: alias.name.to_string(), | ||
kind: Kind::Unaliased, | ||
}, | ||
alias.name.range(), | ||
)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...lint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_module_import.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,38 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
non_ascii_module_import.py:1:29: PLC2403 Module alias `łos` contains a non-ASCII character, use an ASCII-only alias | ||
| | ||
1 | from os.path import join as łos # Error | ||
| ^^^ PLC2403 | ||
2 | from os.path import join as los # OK | ||
| | ||
|
||
non_ascii_module_import.py:4:24: PLC2403 Module alias `łos` contains a non-ASCII character, use an ASCII-only alias | ||
| | ||
2 | from os.path import join as los # OK | ||
3 | | ||
4 | import os.path.join as łos # Error | ||
| ^^^ PLC2403 | ||
5 | import os.path.join as los # OK | ||
| | ||
|
||
non_ascii_module_import.py:7:8: PLC2403 Module name `os.path.łos` contains a non-ASCII character, use an ASCII-only alias | ||
| | ||
5 | import os.path.join as los # OK | ||
6 | | ||
7 | import os.path.łos # Error (recommend an ASCII alias) | ||
| ^^^^^^^^^^^ PLC2403 | ||
8 | import os.path.los # OK | ||
| | ||
|
||
non_ascii_module_import.py:10:21: PLC2403 Module name `łos` contains a non-ASCII character, use an ASCII-only alias | ||
| | ||
8 | import os.path.los # OK | ||
9 | | ||
10 | from os.path import łos # Error (recommend an ASCII alias) | ||
| ^^^ PLC2403 | ||
11 | from os.path import los # OK | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.