Skip to content

Commit

Permalink
Ensure N811 flags single-character constants imported as lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Nov 27, 2024
1 parent e9af7fa commit 8d59bf8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,10 +65,13 @@ pub(crate) fn constant_imported_as_non_constant(
stmt: &Stmt,
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
// 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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`
|
Expand All @@ -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`
Expand All @@ -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:
|
Original file line number Diff line number Diff line change
@@ -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`
|
Expand All @@ -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:
|

0 comments on commit 8d59bf8

Please sign in to comment.