From 8d59bf83070258a37aeb0fbb2c1d017f305c495e Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 27 Nov 2024 14:20:12 +0000 Subject: [PATCH] Ensure N811 flags single-character constants imported as lowercase --- .../resources/test/fixtures/pep8_naming/N811.py | 2 ++ .../resources/test/fixtures/pep8_naming/N814.py | 1 + .../rules/constant_imported_as_non_constant.rs | 13 ++++++++----- ...__rules__pep8_naming__tests__N811_N811.py.snap | 15 ++++++++++++--- ...__rules__pep8_naming__tests__N814_N814.py.snap | 3 +-- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pep8_naming/N811.py b/crates/ruff_linter/resources/test/fixtures/pep8_naming/N811.py index 859edcc29b3237..c18b8f992d8593 100644 --- a/crates/ruff_linter/resources/test/fixtures/pep8_naming/N811.py +++ b/crates/ruff_linter/resources/test/fixtures/pep8_naming/N811.py @@ -2,6 +2,8 @@ from mod import CONSTANT as constant from mod import ANOTHER_CONSTANT as another_constant import mod.CON as c +from mod import C as c +# These are all OK: import django.db.models.Q as Query1 from django.db.models import Q as Query2 diff --git a/crates/ruff_linter/resources/test/fixtures/pep8_naming/N814.py b/crates/ruff_linter/resources/test/fixtures/pep8_naming/N814.py index 0d47a4d0347fbf..4a6809043000c3 100644 --- a/crates/ruff_linter/resources/test/fixtures/pep8_naming/N814.py +++ b/crates/ruff_linter/resources/test/fixtures/pep8_naming/N814.py @@ -2,5 +2,6 @@ from mod import CamelCase as CAMELCASE from mod import AnotherCamelCase as ANOTHER_CAMELCASE +# These are all OK: import mod.AppleFruit as A from mod import BananaFruit as B diff --git a/crates/ruff_linter/src/rules/pep8_naming/rules/constant_imported_as_non_constant.rs b/crates/ruff_linter/src/rules/pep8_naming/rules/constant_imported_as_non_constant.rs index 92e6b135a65966..54a4a70259b610 100644 --- a/crates/ruff_linter/src/rules/pep8_naming/rules/constant_imported_as_non_constant.rs +++ b/crates/ruff_linter/src/rules/pep8_naming/rules/constant_imported_as_non_constant.rs @@ -4,7 +4,7 @@ use ruff_python_ast::{Alias, Stmt}; use ruff_python_stdlib::str; use ruff_text_size::Ranged; -use crate::rules::pep8_naming::settings::IgnoreNames; +use crate::rules::pep8_naming::{helpers, settings::IgnoreNames}; /// ## What it does /// Checks for constant imports that are aliased to non-constant-style @@ -65,10 +65,13 @@ pub(crate) fn constant_imported_as_non_constant( stmt: &Stmt, ignore_names: &IgnoreNames, ) -> Option { - // Single-character names are ambiguous. It could be a class or a constant. - name.chars().nth(1)?; - - if str::is_cased_uppercase(name) && !str::is_cased_uppercase(asname) { + if str::is_cased_uppercase(name) + && !(str::is_cased_uppercase(asname) + // Single-character names are ambiguous. + // It could be a class or a constant, so allow it to be imported + // as `SCREAMING_SNAKE_CASE` *or* `CamelCase`. + || (name.chars().nth(1).is_none()) && helpers::is_camelcase(asname)) + { // Ignore any explicitly-allowed names. if ignore_names.matches(name) || ignore_names.matches(asname) { return None; diff --git a/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__N811_N811.py.snap b/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__N811_N811.py.snap index f4df43bc5b9530..680195dbffe538 100644 --- a/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__N811_N811.py.snap +++ b/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__N811_N811.py.snap @@ -1,6 +1,5 @@ --- source: crates/ruff_linter/src/rules/pep8_naming/mod.rs -snapshot_kind: text --- N811.py:1:8: N811 Constant `CONST` imported as non-constant `const` | @@ -26,6 +25,7 @@ N811.py:3:17: N811 Constant `ANOTHER_CONSTANT` imported as non-constant `another 3 | from mod import ANOTHER_CONSTANT as another_constant | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ N811 4 | import mod.CON as c +5 | from mod import C as c | N811.py:4:8: N811 Constant `CON` imported as non-constant `c` @@ -34,6 +34,15 @@ N811.py:4:8: N811 Constant `CON` imported as non-constant `c` 3 | from mod import ANOTHER_CONSTANT as another_constant 4 | import mod.CON as c | ^^^^^^^^^^^^ N811 -5 | -6 | import django.db.models.Q as Query1 +5 | from mod import C as c + | + +N811.py:5:17: N811 Constant `C` imported as non-constant `c` + | +3 | from mod import ANOTHER_CONSTANT as another_constant +4 | import mod.CON as c +5 | from mod import C as c + | ^^^^^^ N811 +6 | +7 | # These are all OK: | diff --git a/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__N814_N814.py.snap b/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__N814_N814.py.snap index df0d4f983f6844..57da8ddfaeed8e 100644 --- a/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__N814_N814.py.snap +++ b/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__N814_N814.py.snap @@ -1,6 +1,5 @@ --- source: crates/ruff_linter/src/rules/pep8_naming/mod.rs -snapshot_kind: text --- N814.py:1:8: N814 Camelcase `Camel` imported as constant `CAMEL` | @@ -25,5 +24,5 @@ N814.py:3:17: N814 Camelcase `AnotherCamelCase` imported as constant `ANOTHER_CA 3 | from mod import AnotherCamelCase as ANOTHER_CAMELCASE | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ N814 4 | -5 | import mod.AppleFruit as A +5 | # These are all OK: |