-
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-name` / `C2401`](https://pylint.pycqa.org/en/latest/user_guide/messages/convention/non-ascii-name.html) See #970 ## Test Plan `cargo test` and manually
- Loading branch information
1 parent
7a5f988
commit 90ebea8
Showing
10 changed files
with
241 additions
and
4 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_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,31 @@ | ||
ápple_count: int = 1 # C2401 | ||
ápple_count += 2 # C2401 | ||
ápple_count = 3 # C2401 | ||
|
||
(ápple_count for ápple_count in y) | ||
|
||
|
||
def func(ápple_count): | ||
global ápple_count | ||
nonlocal ápple_count | ||
|
||
|
||
def ápple_count(): | ||
pass | ||
|
||
|
||
match ápple_count: | ||
case ápple_count: | ||
pass | ||
|
||
ápple_count: int | ||
|
||
try: | ||
1/0 | ||
except ápple_count: | ||
pass | ||
|
||
# OK | ||
print(ápple_count) | ||
ápple_count == 3 | ||
apple_count = 4 |
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
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
116 changes: 116 additions & 0 deletions
116
crates/ruff_linter/src/rules/pylint/rules/non_ascii_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,116 @@ | ||
use std::fmt; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_semantic::{Binding, BindingKind}; | ||
use ruff_source_file::Locator; | ||
use ruff_text_size::Ranged; | ||
|
||
/// ## What it does | ||
/// Checks for the use of non-ASCII characters in variable names. | ||
/// | ||
/// ## Why is this bad? | ||
/// The use of non-ASCII characters in variable names can cause confusion | ||
/// and compatibility issues (see: [PEP 672]). | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// ápple_count: int | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// apple_count: int | ||
/// ``` | ||
/// | ||
/// [PEP 672]: https://peps.python.org/pep-0672/ | ||
#[violation] | ||
pub struct NonAsciiName { | ||
name: String, | ||
kind: Kind, | ||
} | ||
|
||
impl Violation for NonAsciiName { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Self { name, kind } = self; | ||
format!("{kind} name `{name}` contains a non-ASCII character, consider renaming it") | ||
} | ||
} | ||
|
||
/// PLC2401 | ||
pub(crate) fn non_ascii_name(binding: &Binding, locator: &Locator) -> Option<Diagnostic> { | ||
let name = binding.name(locator); | ||
if name.is_ascii() { | ||
return None; | ||
} | ||
|
||
let kind = match binding.kind { | ||
BindingKind::Annotation => Kind::Annotation, | ||
BindingKind::Argument => Kind::Argument, | ||
BindingKind::NamedExprAssignment => Kind::NamedExprAssignment, | ||
BindingKind::UnpackedAssignment => Kind::UnpackedAssignment, | ||
BindingKind::Assignment => Kind::Assignment, | ||
BindingKind::TypeParam => Kind::TypeParam, | ||
BindingKind::LoopVar => Kind::LoopVar, | ||
BindingKind::Global => Kind::Global, | ||
BindingKind::Nonlocal(_) => Kind::Nonlocal, | ||
BindingKind::ClassDefinition(_) => Kind::ClassDefinition, | ||
BindingKind::FunctionDefinition(_) => Kind::FunctionDefinition, | ||
BindingKind::BoundException => Kind::BoundException, | ||
|
||
BindingKind::Builtin | ||
| BindingKind::Export(_) | ||
| BindingKind::FutureImport | ||
| BindingKind::Import(_) | ||
| BindingKind::FromImport(_) | ||
| BindingKind::SubmoduleImport(_) | ||
| BindingKind::Deletion | ||
| BindingKind::UnboundException(_) => { | ||
return None; | ||
} | ||
}; | ||
|
||
Some(Diagnostic::new( | ||
NonAsciiName { | ||
name: name.to_string(), | ||
kind, | ||
}, | ||
binding.range(), | ||
)) | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
enum Kind { | ||
Annotation, | ||
Argument, | ||
NamedExprAssignment, | ||
UnpackedAssignment, | ||
Assignment, | ||
TypeParam, | ||
LoopVar, | ||
Global, | ||
Nonlocal, | ||
ClassDefinition, | ||
FunctionDefinition, | ||
BoundException, | ||
} | ||
|
||
impl fmt::Display for Kind { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Kind::Annotation => f.write_str("Annotation"), | ||
Kind::Argument => f.write_str("Argument"), | ||
Kind::NamedExprAssignment => f.write_str("Variable"), | ||
Kind::UnpackedAssignment => f.write_str("Variable"), | ||
Kind::Assignment => f.write_str("Variable"), | ||
Kind::TypeParam => f.write_str("Type parameter"), | ||
Kind::LoopVar => f.write_str("Variable"), | ||
Kind::Global => f.write_str("Global"), | ||
Kind::Nonlocal => f.write_str("Nonlocal"), | ||
Kind::ClassDefinition => f.write_str("Class"), | ||
Kind::FunctionDefinition => f.write_str("Function"), | ||
Kind::BoundException => f.write_str("Exception"), | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
.../rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2401_non_ascii_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,79 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
non_ascii_name.py:1:1: PLC2401 Variable name `ápple_count` contains a non-ASCII character, consider renaming it | ||
| | ||
1 | ápple_count: int = 1 # C2401 | ||
| ^^^^^^^^^^^ PLC2401 | ||
2 | ápple_count += 2 # C2401 | ||
3 | ápple_count = 3 # C2401 | ||
| | ||
|
||
non_ascii_name.py:2:1: PLC2401 Variable name `ápple_count` contains a non-ASCII character, consider renaming it | ||
| | ||
1 | ápple_count: int = 1 # C2401 | ||
2 | ápple_count += 2 # C2401 | ||
| ^^^^^^^^^^^ PLC2401 | ||
3 | ápple_count = 3 # C2401 | ||
| | ||
|
||
non_ascii_name.py:3:1: PLC2401 Variable name `ápple_count` contains a non-ASCII character, consider renaming it | ||
| | ||
1 | ápple_count: int = 1 # C2401 | ||
2 | ápple_count += 2 # C2401 | ||
3 | ápple_count = 3 # C2401 | ||
| ^^^^^^^^^^^ PLC2401 | ||
4 | | ||
5 | (ápple_count for ápple_count in y) | ||
| | ||
|
||
non_ascii_name.py:5:18: PLC2401 Variable name `ápple_count` contains a non-ASCII character, consider renaming it | ||
| | ||
3 | ápple_count = 3 # C2401 | ||
4 | | ||
5 | (ápple_count for ápple_count in y) | ||
| ^^^^^^^^^^^ PLC2401 | ||
| | ||
|
||
non_ascii_name.py:8:10: PLC2401 Argument name `ápple_count` contains a non-ASCII character, consider renaming it | ||
| | ||
8 | def func(ápple_count): | ||
| ^^^^^^^^^^^ PLC2401 | ||
9 | global ápple_count | ||
10 | nonlocal ápple_count | ||
| | ||
|
||
non_ascii_name.py:9:12: PLC2401 Global name `ápple_count` contains a non-ASCII character, consider renaming it | ||
| | ||
8 | def func(ápple_count): | ||
9 | global ápple_count | ||
| ^^^^^^^^^^^ PLC2401 | ||
10 | nonlocal ápple_count | ||
| | ||
|
||
non_ascii_name.py:13:5: PLC2401 Function name `ápple_count` contains a non-ASCII character, consider renaming it | ||
| | ||
13 | def ápple_count(): | ||
| ^^^^^^^^^^^ PLC2401 | ||
14 | pass | ||
| | ||
|
||
non_ascii_name.py:18:10: PLC2401 Variable name `ápple_count` contains a non-ASCII character, consider renaming it | ||
| | ||
17 | match ápple_count: | ||
18 | case ápple_count: | ||
| ^^^^^^^^^^^ PLC2401 | ||
19 | pass | ||
| | ||
|
||
non_ascii_name.py:21:1: PLC2401 Annotation name `ápple_count` contains a non-ASCII character, consider renaming it | ||
| | ||
19 | pass | ||
20 | | ||
21 | ápple_count: int | ||
| ^^^^^^^^^^^ PLC2401 | ||
22 | | ||
23 | try: | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.