Skip to content

Commit

Permalink
[pylint] - Implement non-ascii-module-import (C2403)
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Oct 18, 2023
1 parent 78d172a commit 5c53f4b
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 0 deletions.
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
10 changes: 10 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {

for alias in names {
if let Some(asname) = &alias.asname {
if checker.enabled(Rule::NonAsciiImportName) {
pylint::rules::non_ascii_import_name(checker, asname);
}
if checker.enabled(Rule::BuiltinVariableShadowing) {
flake8_builtins::rules::builtin_variable_shadowing(
checker,
Expand Down Expand Up @@ -716,6 +719,13 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
}
}
}
if checker.enabled(Rule::NonAsciiImportName) {
for name in names {
if let Some(asname) = name.asname.as_ref() {
pylint::rules::non_ascii_import_name(checker, asname);
}
}
}
if checker.enabled(Rule::DeprecatedMockImport) {
pyupgrade::rules::deprecated_mock_import(checker, stmt);
}
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, "C2403") => (RuleGroup::Preview, rules::pylint::rules::NonAsciiImportName),
#[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 @@ -138,6 +138,7 @@ mod tests {
#[test_case(Rule::NoSelfUse, Path::new("no_self_use.py"))]
#[test_case(Rule::MisplacedBareRaise, Path::new("misplaced_bare_raise.py"))]
#[test_case(Rule::LiteralMembership, Path::new("literal_membership.py"))]
#[test_case(Rule::NonAsciiImportName, Path::new("non_ascii_import_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 @@ -33,6 +33,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_import_name::*;
pub(crate) use nonlocal_without_binding::*;
pub(crate) use property_with_parameters::*;
pub(crate) use redefined_loop_name::*;
Expand Down Expand Up @@ -97,6 +98,7 @@ mod misplaced_bare_raise;
mod named_expr_without_context;
mod nested_min_max;
mod no_self_use;
mod non_ascii_import_name;
mod nonlocal_without_binding;
mod property_with_parameters;
mod redefined_loop_name;
Expand Down
38 changes: 38 additions & 0 deletions crates/ruff_linter/src/rules/pylint/rules/non_ascii_import_name.rs
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()));
}
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
|


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 5c53f4b

Please sign in to comment.