From b75ea94f586554f9fcce3dca39ff4672b14e9977 Mon Sep 17 00:00:00 2001 From: Harutaka Kawamura Date: Sun, 23 Oct 2022 01:49:13 +0900 Subject: [PATCH] Fix uppercase and lowercase check (#461) --- resources/test/fixtures/N811.py | 5 +- resources/test/fixtures/N812.py | 5 +- resources/test/fixtures/N813.py | 3 +- resources/test/fixtures/N814.py | 3 +- resources/test/fixtures/N817.py | 2 +- src/pep8_naming/checks.rs | 82 +++++++++++++++++-- .../ruff__linter__tests__N811_N811.py.snap | 23 ++++-- .../ruff__linter__tests__N812_N812.py.snap | 23 ++++-- .../ruff__linter__tests__N813_N813.py.snap | 17 +++- .../ruff__linter__tests__N814_N814.py.snap | 17 +++- .../ruff__linter__tests__N817_N817.py.snap | 6 +- 11 files changed, 151 insertions(+), 35 deletions(-) diff --git a/resources/test/fixtures/N811.py b/resources/test/fixtures/N811.py index 09b55c677f..0ef646edc9 100644 --- a/resources/test/fixtures/N811.py +++ b/resources/test/fixtures/N811.py @@ -1,2 +1,3 @@ -import mod.SUBMOD as submod -from mod import BAD as bad +import mod.CONST as const +from mod import CONSTANT as constant +from mod import ANOTHER_CONSTANT as another_constant diff --git a/resources/test/fixtures/N812.py b/resources/test/fixtures/N812.py index 89fcdb4036..e7a6f0ca10 100644 --- a/resources/test/fixtures/N812.py +++ b/resources/test/fixtures/N812.py @@ -1,2 +1,3 @@ -import mod.submod as SubMod -from mod import bad as Bad +import modl.lowercase as Lower +from mod import lowercase as Lowercase +from mod import another_lowercase as AnotherLowercase diff --git a/resources/test/fixtures/N813.py b/resources/test/fixtures/N813.py index 228dae2b50..3f4d29779d 100644 --- a/resources/test/fixtures/N813.py +++ b/resources/test/fixtures/N813.py @@ -1,2 +1,3 @@ -import mod.SubMod as submod +import mod.Camel as camel from mod import CamelCase as camelcase +from mod import AnotherCamelCase as another_camelcase diff --git a/resources/test/fixtures/N814.py b/resources/test/fixtures/N814.py index 49104057b9..65a03383a5 100644 --- a/resources/test/fixtures/N814.py +++ b/resources/test/fixtures/N814.py @@ -1,2 +1,3 @@ -import mod.SubMod as SUBMOD +import mod.Camel as CAMEL from mod import CamelCase as CAMELCASE +from mod import AnotherCamelCase as ANOTHER_CAMELCASE diff --git a/resources/test/fixtures/N817.py b/resources/test/fixtures/N817.py index 0a7a8323b2..a315a3e76e 100644 --- a/resources/test/fixtures/N817.py +++ b/resources/test/fixtures/N817.py @@ -1,2 +1,2 @@ -import mod.SubMod as SM +import mod.CaMel as CM from mod import CamelCase as CC diff --git a/src/pep8_naming/checks.rs b/src/pep8_naming/checks.rs index 3c6d9650f0..87a6d00677 100644 --- a/src/pep8_naming/checks.rs +++ b/src/pep8_naming/checks.rs @@ -114,12 +114,36 @@ pub fn dunder_function_name(func_def: &Stmt, scope: &Scope, name: &str) -> Optio None } +fn is_lower(s: &str) -> bool { + let mut cased = false; + for c in s.chars() { + if c.is_uppercase() { + return false; + } else if !cased && c.is_lowercase() { + cased = true; + } + } + cased +} + +fn is_upper(s: &str) -> bool { + let mut cased = false; + for c in s.chars() { + if c.is_lowercase() { + return false; + } else if (!cased) && c.is_uppercase() { + cased = true; + } + } + cased +} + pub fn constant_imported_as_non_constant( import_from: &Stmt, name: &str, asname: &str, ) -> Option { - if name.chars().all(|c| c.is_uppercase()) && !asname.chars().all(|c| c.is_uppercase()) { + if is_upper(name) && !is_upper(asname) { return Some(Check::new( CheckKind::ConstantImportedAsNonConstant(name.to_string(), asname.to_string()), Range::from_located(import_from), @@ -133,7 +157,7 @@ pub fn lowercase_imported_as_non_lowercase( name: &str, asname: &str, ) -> Option { - if name.chars().all(|c| c.is_lowercase()) && asname.to_lowercase() != asname { + if is_lower(name) && asname.to_lowercase() != asname { return Some(Check::new( CheckKind::LowercaseImportedAsNonLowercase(name.to_string(), asname.to_string()), Range::from_located(import_from), @@ -143,9 +167,8 @@ pub fn lowercase_imported_as_non_lowercase( } fn is_camelcase(name: &str) -> bool { - !name.chars().all(|c| c.is_uppercase()) && !name.chars().all(|c| c.is_lowercase()) + !is_lower(name) && !is_upper(name) && !name.contains('_') } - fn is_acronym(name: &str, asname: &str) -> bool { name.chars().filter(|c| c.is_uppercase()).join("") == asname } @@ -155,7 +178,7 @@ pub fn camelcase_imported_as_lowercase( name: &str, asname: &str, ) -> Option { - if is_camelcase(name) && asname.chars().all(|c| c.is_lowercase()) { + if is_camelcase(name) && is_lower(asname) { return Some(Check::new( CheckKind::CamelcaseImportedAsLowercase(name.to_string(), asname.to_string()), Range::from_located(import_from), @@ -169,7 +192,7 @@ pub fn camelcase_imported_as_constant( name: &str, asname: &str, ) -> Option { - if is_camelcase(name) && asname.chars().all(|c| c.is_uppercase()) && !is_acronym(name, asname) { + if is_camelcase(name) && is_upper(asname) && !is_acronym(name, asname) { return Some(Check::new( CheckKind::CamelcaseImportedAsConstant(name.to_string(), asname.to_string()), Range::from_located(import_from), @@ -183,7 +206,7 @@ pub fn camelcase_imported_as_acronym( name: &str, asname: &str, ) -> Option { - if is_camelcase(name) && asname.chars().all(|c| c.is_uppercase()) && is_acronym(name, asname) { + if is_camelcase(name) && is_upper(asname) && is_acronym(name, asname) { return Some(Check::new( CheckKind::CamelcaseImportedAsAcronym(name.to_string(), asname.to_string()), Range::from_located(import_from), @@ -191,3 +214,48 @@ pub fn camelcase_imported_as_acronym( } None } + +#[cfg(test)] +mod tests { + use super::{is_acronym, is_camelcase, is_lower, is_upper}; + + #[test] + fn test_is_lower() -> () { + assert!(is_lower("abc")); + assert!(is_lower("a_b_c")); + assert!(is_lower("a2c")); + assert!(!is_lower("aBc")); + assert!(!is_lower("ABC")); + assert!(!is_lower("")); + assert!(!is_lower("_")); + } + + #[test] + fn test_is_upper() -> () { + assert!(is_upper("ABC")); + assert!(is_upper("A_B_C")); + assert!(is_upper("A2C")); + assert!(!is_upper("aBc")); + assert!(!is_upper("abc")); + assert!(!is_upper("")); + assert!(!is_upper("_")); + } + + #[test] + fn test_is_camelcase() -> () { + assert!(is_camelcase("Camel")); + assert!(is_camelcase("CamelCase")); + assert!(!is_camelcase("camel")); + assert!(!is_camelcase("camel_case")); + assert!(!is_camelcase("CAMEL")); + assert!(!is_camelcase("CAMEL_CASE")); + } + + #[test] + fn test_is_acronym() -> () { + assert!(is_acronym("AB", "AB")); + assert!(is_acronym("AbcDef", "AD")); + assert!(!is_acronym("AbcDef", "Ad")); + assert!(!is_acronym("AbcDef", "AB")); + } +} diff --git a/src/snapshots/ruff__linter__tests__N811_N811.py.snap b/src/snapshots/ruff__linter__tests__N811_N811.py.snap index edd702c863..fbae026c6f 100644 --- a/src/snapshots/ruff__linter__tests__N811_N811.py.snap +++ b/src/snapshots/ruff__linter__tests__N811_N811.py.snap @@ -4,24 +4,35 @@ expression: checks --- - kind: ConstantImportedAsNonConstant: - - SUBMOD - - submod + - CONST + - const location: row: 1 column: 1 end_location: row: 1 - column: 28 + column: 26 fix: ~ - kind: ConstantImportedAsNonConstant: - - BAD - - bad + - CONSTANT + - constant location: row: 2 column: 1 end_location: row: 2 - column: 27 + column: 37 + fix: ~ +- kind: + ConstantImportedAsNonConstant: + - ANOTHER_CONSTANT + - another_constant + location: + row: 3 + column: 1 + end_location: + row: 3 + column: 53 fix: ~ diff --git a/src/snapshots/ruff__linter__tests__N812_N812.py.snap b/src/snapshots/ruff__linter__tests__N812_N812.py.snap index de25d165ad..74055764c3 100644 --- a/src/snapshots/ruff__linter__tests__N812_N812.py.snap +++ b/src/snapshots/ruff__linter__tests__N812_N812.py.snap @@ -4,24 +4,35 @@ expression: checks --- - kind: LowercaseImportedAsNonLowercase: - - submod - - SubMod + - lowercase + - Lower location: row: 1 column: 1 end_location: row: 1 - column: 28 + column: 31 fix: ~ - kind: LowercaseImportedAsNonLowercase: - - bad - - Bad + - lowercase + - Lowercase location: row: 2 column: 1 end_location: row: 2 - column: 27 + column: 39 + fix: ~ +- kind: + LowercaseImportedAsNonLowercase: + - another_lowercase + - AnotherLowercase + location: + row: 3 + column: 1 + end_location: + row: 3 + column: 54 fix: ~ diff --git a/src/snapshots/ruff__linter__tests__N813_N813.py.snap b/src/snapshots/ruff__linter__tests__N813_N813.py.snap index cbbca74e17..a178a76a7b 100644 --- a/src/snapshots/ruff__linter__tests__N813_N813.py.snap +++ b/src/snapshots/ruff__linter__tests__N813_N813.py.snap @@ -4,14 +4,14 @@ expression: checks --- - kind: CamelcaseImportedAsLowercase: - - SubMod - - submod + - Camel + - camel location: row: 1 column: 1 end_location: row: 1 - column: 28 + column: 26 fix: ~ - kind: CamelcaseImportedAsLowercase: @@ -24,4 +24,15 @@ expression: checks row: 2 column: 39 fix: ~ +- kind: + CamelcaseImportedAsLowercase: + - AnotherCamelCase + - another_camelcase + location: + row: 3 + column: 1 + end_location: + row: 3 + column: 54 + fix: ~ diff --git a/src/snapshots/ruff__linter__tests__N814_N814.py.snap b/src/snapshots/ruff__linter__tests__N814_N814.py.snap index 1ca26a030d..6bab36cedf 100644 --- a/src/snapshots/ruff__linter__tests__N814_N814.py.snap +++ b/src/snapshots/ruff__linter__tests__N814_N814.py.snap @@ -4,14 +4,14 @@ expression: checks --- - kind: CamelcaseImportedAsConstant: - - SubMod - - SUBMOD + - Camel + - CAMEL location: row: 1 column: 1 end_location: row: 1 - column: 28 + column: 26 fix: ~ - kind: CamelcaseImportedAsConstant: @@ -24,4 +24,15 @@ expression: checks row: 2 column: 39 fix: ~ +- kind: + CamelcaseImportedAsConstant: + - AnotherCamelCase + - ANOTHER_CAMELCASE + location: + row: 3 + column: 1 + end_location: + row: 3 + column: 54 + fix: ~ diff --git a/src/snapshots/ruff__linter__tests__N817_N817.py.snap b/src/snapshots/ruff__linter__tests__N817_N817.py.snap index eaa5188de4..6b16af81b3 100644 --- a/src/snapshots/ruff__linter__tests__N817_N817.py.snap +++ b/src/snapshots/ruff__linter__tests__N817_N817.py.snap @@ -4,14 +4,14 @@ expression: checks --- - kind: CamelcaseImportedAsAcronym: - - SubMod - - SM + - CaMel + - CM location: row: 1 column: 1 end_location: row: 1 - column: 24 + column: 23 fix: ~ - kind: CamelcaseImportedAsAcronym: