From 5c53f4bbe1f2f79edde915f41cc95c861b915d3f Mon Sep 17 00:00:00 2001 From: Steve C Date: Wed, 18 Oct 2023 18:39:06 -0400 Subject: [PATCH 1/4] [pylint] - Implement `non-ascii-module-import` (`C2403`) --- .../fixtures/pylint/non_ascii_import_name.py | 5 +++ .../src/checkers/ast/analyze/statement.rs | 10 +++++ crates/ruff_linter/src/codes.rs | 1 + crates/ruff_linter/src/rules/pylint/mod.rs | 1 + .../ruff_linter/src/rules/pylint/rules/mod.rs | 2 + .../pylint/rules/non_ascii_import_name.rs | 38 +++++++++++++++++++ ...sts__PLC2403_non_ascii_import_name.py.snap | 22 +++++++++++ ruff.schema.json | 4 ++ 8 files changed, 83 insertions(+) create mode 100644 crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_import_name.py create mode 100644 crates/ruff_linter/src/rules/pylint/rules/non_ascii_import_name.rs create mode 100644 crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_import_name.py.snap diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_import_name.py b/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_import_name.py new file mode 100644 index 0000000000000..a910449cce066 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_import_name.py @@ -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 diff --git a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs index bf5313b981e00..69d4829f0b4ec 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs @@ -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, @@ -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); } diff --git a/crates/ruff_linter/src/codes.rs b/crates/ruff_linter/src/codes.rs index 6835e7e725a98..008135f48f8c0 100644 --- a/crates/ruff_linter/src/codes.rs +++ b/crates/ruff_linter/src/codes.rs @@ -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), diff --git a/crates/ruff_linter/src/rules/pylint/mod.rs b/crates/ruff_linter/src/rules/pylint/mod.rs index d17a662cbdb4f..90a835c2a8f21 100644 --- a/crates/ruff_linter/src/rules/pylint/mod.rs +++ b/crates/ruff_linter/src/rules/pylint/mod.rs @@ -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( diff --git a/crates/ruff_linter/src/rules/pylint/rules/mod.rs b/crates/ruff_linter/src/rules/pylint/rules/mod.rs index c4d7bd831b8ed..01269692477bb 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/mod.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/mod.rs @@ -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::*; @@ -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; diff --git a/crates/ruff_linter/src/rules/pylint/rules/non_ascii_import_name.rs b/crates/ruff_linter/src/rules/pylint/rules/non_ascii_import_name.rs new file mode 100644 index 0000000000000..8d7f0e014cb3e --- /dev/null +++ b/crates/ruff_linter/src/rules/pylint/rules/non_ascii_import_name.rs @@ -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())); +} diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_import_name.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_import_name.py.snap new file mode 100644 index 0000000000000..19593be03d95a --- /dev/null +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_import_name.py.snap @@ -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 + | + + diff --git a/ruff.schema.json b/ruff.schema.json index 7d676b8f99ea2..68d094f45ebbe 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -2906,6 +2906,10 @@ "PLC19", "PLC190", "PLC1901", + "PLC2", + "PLC24", + "PLC240", + "PLC2403", "PLC3", "PLC30", "PLC300", From 1c76a4dbe7234e4c9d34a04487a8da4c3ea81ac8 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 20 Oct 2023 13:36:29 -0400 Subject: [PATCH 2/4] Rename rule --- ...cii_import_name.py => non_ascii_module_import.py} | 3 +++ .../src/checkers/ast/analyze/statement.rs | 4 ++-- crates/ruff_linter/src/rules/pylint/mod.rs | 2 +- crates/ruff_linter/src/rules/pylint/rules/mod.rs | 4 ++-- ...cii_import_name.rs => non_ascii_module_import.rs} | 12 +++++------- ...__tests__PLC2403_non_ascii_module_import.py.snap} | 4 ++-- 6 files changed, 15 insertions(+), 14 deletions(-) rename crates/ruff_linter/resources/test/fixtures/pylint/{non_ascii_import_name.py => non_ascii_module_import.py} (73%) rename crates/ruff_linter/src/rules/pylint/rules/{non_ascii_import_name.rs => non_ascii_module_import.rs} (77%) rename crates/ruff_linter/src/rules/pylint/snapshots/{ruff_linter__rules__pylint__tests__PLC2403_non_ascii_import_name.py.snap => ruff_linter__rules__pylint__tests__PLC2403_non_ascii_module_import.py.snap} (66%) diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_import_name.py b/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_module_import.py similarity index 73% rename from crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_import_name.py rename to crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_module_import.py index a910449cce066..06ef9e8b2e2b0 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_import_name.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_module_import.py @@ -3,3 +3,6 @@ import os.path.join as łos # Error import os.path.join as los # Ok + +import os.path.łos # Error +import os.path.los # Ok diff --git a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs index f5e4a7075c132..b8c199f0df6b8 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs @@ -549,7 +549,7 @@ 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); + pylint::rules::non_ascii_module_import(checker, asname); } if checker.enabled(Rule::BuiltinVariableShadowing) { flake8_builtins::rules::builtin_variable_shadowing( @@ -725,7 +725,7 @@ 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); + pylint::rules::non_ascii_module_import(checker, asname); } } } diff --git a/crates/ruff_linter/src/rules/pylint/mod.rs b/crates/ruff_linter/src/rules/pylint/mod.rs index cd1a2c94a30bc..a1a6c3c591aa4 100644 --- a/crates/ruff_linter/src/rules/pylint/mod.rs +++ b/crates/ruff_linter/src/rules/pylint/mod.rs @@ -140,7 +140,7 @@ mod tests { #[test_case(Rule::LiteralMembership, Path::new("literal_membership.py"))] #[test_case(Rule::GlobalAtModuleLevel, Path::new("global_at_module_level.py"))] #[test_case(Rule::UnnecessaryLambda, Path::new("unnecessary_lambda.py"))] - #[test_case(Rule::NonAsciiImportName, Path::new("non_ascii_import_name.py"))] + #[test_case(Rule::NonAsciiImportName, Path::new("non_ascii_module_import.py"))] fn rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy()); let diagnostics = test_path( diff --git a/crates/ruff_linter/src/rules/pylint/rules/mod.rs b/crates/ruff_linter/src/rules/pylint/rules/mod.rs index 67d85c2b529ac..0b8e3f20f3313 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/mod.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/mod.rs @@ -34,7 +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_import_name::*; +pub(crate) use non_ascii_module_import::*; pub(crate) use nonlocal_without_binding::*; pub(crate) use property_with_parameters::*; pub(crate) use redefined_loop_name::*; @@ -101,7 +101,7 @@ mod misplaced_bare_raise; mod named_expr_without_context; mod nested_min_max; mod no_self_use; -mod non_ascii_import_name; +mod non_ascii_module_import; mod nonlocal_without_binding; mod property_with_parameters; mod redefined_loop_name; diff --git a/crates/ruff_linter/src/rules/pylint/rules/non_ascii_import_name.rs b/crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs similarity index 77% rename from crates/ruff_linter/src/rules/pylint/rules/non_ascii_import_name.rs rename to crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs index 8d7f0e014cb3e..76061786c527c 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/non_ascii_import_name.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs @@ -27,12 +27,10 @@ impl Violation for NonAsciiImportName { } /// PLC2403 -pub(crate) fn non_ascii_import_name(checker: &mut Checker, target: &Identifier) { - if target.to_string().is_ascii() { - return; +pub(crate) fn non_ascii_module_import(checker: &mut Checker, target: &Identifier) { + if !target.as_str().is_ascii() { + checker + .diagnostics + .push(Diagnostic::new(NonAsciiImportName, target.range())); }; - - checker - .diagnostics - .push(Diagnostic::new(NonAsciiImportName, target.range())); } diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_import_name.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_module_import.py.snap similarity index 66% rename from crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_import_name.py.snap rename to crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_module_import.py.snap index 19593be03d95a..ccba3ea8bb98d 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_import_name.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_module_import.py.snap @@ -1,7 +1,7 @@ --- 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. +non_ascii_module_import.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 @@ -10,7 +10,7 @@ non_ascii_import_name.py:2:29: PLC2403 Symbol name contains a non-ASCII characte 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. +non_ascii_module_import.py:4:24: PLC2403 Symbol name contains a non-ASCII character, consider renaming it. | 2 | from os.path import join as łos # Error 3 | From e5121968d8d420e65f4d8f2431fe4f1da34d8cfa Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 20 Oct 2023 13:54:19 -0400 Subject: [PATCH 3/4] Tweak logic --- .../pylint/non_ascii_module_import.py | 17 +++- .../src/checkers/ast/analyze/statement.rs | 20 +++-- .../pylint/rules/non_ascii_module_import.rs | 79 ++++++++++++++++--- ...s__PLC2403_non_ascii_module_import.py.snap | 32 ++++++-- 4 files changed, 114 insertions(+), 34 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_module_import.py b/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_module_import.py index 06ef9e8b2e2b0..64dbc2ce865df 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_module_import.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_module_import.py @@ -1,8 +1,17 @@ -from os.path import join as los # Ok 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.join as los # OK -import os.path.łos # Error -import os.path.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 diff --git a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs index b8c199f0df6b8..84f46c7ac51a6 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs @@ -547,10 +547,10 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) { } for alias in names { + if checker.enabled(Rule::NonAsciiImportName) { + pylint::rules::non_ascii_module_import(checker, alias); + } if let Some(asname) = &alias.asname { - if checker.enabled(Rule::NonAsciiImportName) { - pylint::rules::non_ascii_module_import(checker, asname); - } if checker.enabled(Rule::BuiltinVariableShadowing) { flake8_builtins::rules::builtin_variable_shadowing( checker, @@ -701,8 +701,8 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) { range: _, }, ) => { - let module = module.as_deref(); let level = *level; + let module = module.as_deref(); if checker.enabled(Rule::ModuleImportNotAtTopOfFile) { pycodestyle::rules::module_import_not_at_top_of_file(checker, stmt); } @@ -715,6 +715,11 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) { } } } + if checker.enabled(Rule::NonAsciiImportName) { + for alias in names { + pylint::rules::non_ascii_module_import(checker, &alias); + } + } if checker.enabled(Rule::UnnecessaryFutureImport) { if checker.settings.target_version >= PythonVersion::Py37 { if let Some("__future__") = module { @@ -722,13 +727,6 @@ 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_module_import(checker, asname); - } - } - } if checker.enabled(Rule::DeprecatedMockImport) { pyupgrade::rules::deprecated_mock_import(checker, stmt); } diff --git a/crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs b/crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs index 76061786c527c..1c282b5bc989b 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs @@ -1,5 +1,4 @@ -use ast::Identifier; -use ruff_python_ast::{self as ast}; +use ruff_python_ast::{self as ast, Alias}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -8,29 +7,87 @@ 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. +/// 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; +pub struct NonAsciiImportName { + name: String, + kind: Kind, +} impl Violation for NonAsciiImportName { #[derive_message_formats] fn message(&self) -> String { - format!("Symbol name contains a non-ASCII character, consider renaming it.") + 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, target: &Identifier) { - if !target.as_str().is_ascii() { - checker - .diagnostics - .push(Diagnostic::new(NonAsciiImportName, target.range())); - }; +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(), + )); + } } diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_module_import.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_module_import.py.snap index ccba3ea8bb98d..31350f4ba9966 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_module_import.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2403_non_ascii_module_import.py.snap @@ -1,22 +1,38 @@ --- source: crates/ruff_linter/src/rules/pylint/mod.rs --- -non_ascii_module_import.py:2:29: PLC2403 Symbol name contains a non-ASCII character, consider renaming it. +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 los # Ok -2 | from os.path import join as łos # Error +1 | from os.path import join as łos # Error | ^^^ PLC2403 -3 | -4 | import os.path.join as łos # Error +2 | from os.path import join as los # OK | -non_ascii_module_import.py:4:24: PLC2403 Symbol name contains a non-ASCII character, consider renaming it. +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 łos # Error +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 +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 + | + From 7bc20ed6f32530366338581141654c8d5cd0ebdc Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 20 Oct 2023 13:55:41 -0400 Subject: [PATCH 4/4] Run clippy --- crates/ruff_linter/src/checkers/ast/analyze/statement.rs | 2 +- .../src/rules/pylint/rules/non_ascii_module_import.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs index 84f46c7ac51a6..86e9ae80666e4 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs @@ -717,7 +717,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) { } if checker.enabled(Rule::NonAsciiImportName) { for alias in names { - pylint::rules::non_ascii_module_import(checker, &alias); + pylint::rules::non_ascii_module_import(checker, alias); } } if checker.enabled(Rule::UnnecessaryFutureImport) { diff --git a/crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs b/crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs index 1c282b5bc989b..f1fc168e16117 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/non_ascii_module_import.rs @@ -1,4 +1,4 @@ -use ruff_python_ast::{self as ast, Alias}; +use ruff_python_ast::Alias; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation};