-
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.
[pylint] - Implement
non-ascii-module-import
(C2403
)
- Loading branch information
1 parent
78d172a
commit 5c53f4b
Showing
8 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_import_name.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,5 @@ | ||
from os.path import join as los # Ok | ||
from os.path import join as łos # Error | ||
|
||
import os.path.join as łos # Error | ||
import os.path.join as los # 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
38 changes: 38 additions & 0 deletions
38
crates/ruff_linter/src/rules/pylint/rules/non_ascii_import_name.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,38 @@ | ||
use ast::Identifier; | ||
use ruff_python_ast::{self as ast}; | ||
|
||
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 symbol names. | ||
/// | ||
/// ## Why is this bad? | ||
/// Pylint discourages the use of non-ASCII characters in symbol names as | ||
/// they can cause confusion and compatibility issues. | ||
/// | ||
/// ## References | ||
/// - [PEP 672](https://peps.python.org/pep-0672/) | ||
#[violation] | ||
pub struct NonAsciiImportName; | ||
|
||
impl Violation for NonAsciiImportName { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Symbol name contains a non-ASCII character, consider renaming it.") | ||
} | ||
} | ||
|
||
/// PLC2403 | ||
pub(crate) fn non_ascii_import_name(checker: &mut Checker, target: &Identifier) { | ||
if target.to_string().is_ascii() { | ||
return; | ||
}; | ||
|
||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(NonAsciiImportName, target.range())); | ||
} |
22 changes: 22 additions & 0 deletions
22
...pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_import_name.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,22 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
non_ascii_import_name.py:2:29: PLC2403 Symbol name contains a non-ASCII character, consider renaming it. | ||
| | ||
1 | from os.path import join as los # Ok | ||
2 | from os.path import join as łos # Error | ||
| ^^^ PLC2403 | ||
3 | | ||
4 | import os.path.join as łos # Error | ||
| | ||
|
||
non_ascii_import_name.py:4:24: PLC2403 Symbol name contains a non-ASCII character, consider renaming it. | ||
| | ||
2 | from os.path import join as łos # Error | ||
3 | | ||
4 | import os.path.join as łos # Error | ||
| ^^^ PLC2403 | ||
5 | import os.path.join as los # Ok | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.