Skip to content

Commit

Permalink
implement C2401
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Oct 20, 2023
1 parent a525f09 commit 4f2bf1b
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ápple_count: int = 1 # C2401
ápple_count += 2 # C2401
ápple_count = 3 # C2401

# this rule only works on assignment!
ápple_count == 3 # Ok

# normal ascii
apple_count = 4 # Ok
11 changes: 11 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
}
}
Stmt::AugAssign(ast::StmtAugAssign { target, .. }) => {
if checker.enabled(Rule::NonAsciiName) {
pylint::rules::non_ascii_name(checker, target);
}
if checker.enabled(Rule::GlobalStatement) {
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
pylint::rules::global_statement(checker, id);
Expand Down Expand Up @@ -1314,6 +1317,11 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
}
}
Stmt::Assign(assign @ ast::StmtAssign { targets, value, .. }) => {
if checker.enabled(Rule::NonAsciiName) {
for target in targets {
pylint::rules::non_ascii_name(checker, target);
}
}
if checker.enabled(Rule::LambdaAssignment) {
if let [target] = &targets[..] {
pycodestyle::rules::lambda_assignment(checker, target, value, None, stmt);
Expand Down Expand Up @@ -1427,6 +1435,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
..
},
) => {
if checker.enabled(Rule::NonAsciiName) {
pylint::rules::non_ascii_name(checker, target);
}
if let Some(value) = value {
if checker.enabled(Rule::LambdaAssignment) {
pycodestyle::rules::lambda_assignment(
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Pylint, "C0205") => (RuleGroup::Stable, rules::pylint::rules::SingleStringSlots),
(Pylint, "C0208") => (RuleGroup::Stable, rules::pylint::rules::IterationOverSet),
(Pylint, "C0414") => (RuleGroup::Stable, rules::pylint::rules::UselessImportAlias),
(Pylint, "C2401") => (RuleGroup::Preview, rules::pylint::rules::NonAsciiName),
#[allow(deprecated)]
(Pylint, "C1901") => (RuleGroup::Nursery, rules::pylint::rules::CompareToEmptyString),
(Pylint, "C3002") => (RuleGroup::Stable, rules::pylint::rules::UnnecessaryDirectLambdaCall),
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pylint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ mod tests {
#[test_case(Rule::MisplacedBareRaise, Path::new("misplaced_bare_raise.py"))]
#[test_case(Rule::LiteralMembership, Path::new("literal_membership.py"))]
#[test_case(Rule::GlobalAtModuleLevel, Path::new("global_at_module_level.py"))]
#[test_case(Rule::NonAsciiName, Path::new("non_ascii_name.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path(
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/pylint/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub(crate) use misplaced_bare_raise::*;
pub(crate) use named_expr_without_context::*;
pub(crate) use nested_min_max::*;
pub(crate) use no_self_use::*;
pub(crate) use non_ascii_name::*;
pub(crate) use nonlocal_without_binding::*;
pub(crate) use property_with_parameters::*;
pub(crate) use redefined_loop_name::*;
Expand Down Expand Up @@ -99,6 +100,7 @@ mod misplaced_bare_raise;
mod named_expr_without_context;
mod nested_min_max;
mod no_self_use;
mod non_ascii_name;
mod nonlocal_without_binding;
mod property_with_parameters;
mod redefined_loop_name;
Expand Down
41 changes: 41 additions & 0 deletions crates/ruff_linter/src/rules/pylint/rules/non_ascii_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use ruff_python_ast::{self as ast, Expr};

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 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 NonAsciiName;

impl Violation for NonAsciiName {
#[derive_message_formats]
fn message(&self) -> String {
format!("Symbol name contains a non-ASCII character, consider renaming it.")
}
}

/// PLC2401
pub(crate) fn non_ascii_name(checker: &mut Checker, target: &Expr) {
let Expr::Name(ast::ExprName { id, .. }) = target else {
return;
};

if id.is_ascii() {
return;
}

checker
.diagnostics
.push(Diagnostic::new(NonAsciiName, target.range()));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
non_ascii_name.py:1:1: PLC2401 Symbol name 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 Symbol name 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 Symbol name 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 | # this rule only works on assignment!
|


4 changes: 4 additions & 0 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4f2bf1b

Please sign in to comment.