[`pep8-naming`] Consider any number of leading underscore for `N801` (#15988)

## Summary

The PR addresses the issue #15939 

Let me know if you think there are other test cases I should add ;-)
This commit is contained in:
Vasco Schiavo 2025-02-06 09:38:27 +01:00 committed by GitHub
parent 24bab7e82e
commit 81059d05fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 9 deletions

View File

@ -6,6 +6,10 @@ class _bad:
pass
class __bad:
pass
class bad_class:
pass
@ -13,6 +17,8 @@ class bad_class:
class Bad_Class:
pass
class Bad__Class:
pass
class BAD_CLASS:
pass
@ -32,3 +38,6 @@ class GoodClass:
class GOOD:
pass
class __GoodClass:
pass

View File

@ -54,7 +54,7 @@ pub(crate) fn invalid_class_name(
name: &str,
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
let stripped = name.strip_prefix('_').unwrap_or(name);
let stripped = name.trim_start_matches('_');
if !stripped.chars().next().is_some_and(char::is_uppercase) || stripped.contains('_') {
// Ignore any explicitly-allowed names.
if ignore_names.matches(name) {

View File

@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/pep8_naming/mod.rs
snapshot_kind: text
---
N801.py:1:7: N801 Class name `bad` should use CapWords convention
|
@ -16,23 +15,41 @@ N801.py:5:7: N801 Class name `_bad` should use CapWords convention
6 | pass
|
N801.py:9:7: N801 Class name `bad_class` should use CapWords convention
N801.py:9:7: N801 Class name `__bad` should use CapWords convention
|
9 | class bad_class:
| ^^^^^^^^^ N801
9 | class __bad:
| ^^^^^ N801
10 | pass
|
N801.py:13:7: N801 Class name `Bad_Class` should use CapWords convention
N801.py:13:7: N801 Class name `bad_class` should use CapWords convention
|
13 | class Bad_Class:
13 | class bad_class:
| ^^^^^^^^^ N801
14 | pass
|
N801.py:17:7: N801 Class name `BAD_CLASS` should use CapWords convention
N801.py:17:7: N801 Class name `Bad_Class` should use CapWords convention
|
17 | class BAD_CLASS:
17 | class Bad_Class:
| ^^^^^^^^^ N801
18 | pass
|
N801.py:20:7: N801 Class name `Bad__Class` should use CapWords convention
|
18 | pass
19 |
20 | class Bad__Class:
| ^^^^^^^^^^ N801
21 | pass
|
N801.py:23:7: N801 Class name `BAD_CLASS` should use CapWords convention
|
21 | pass
22 |
23 | class BAD_CLASS:
| ^^^^^^^^^ N801
24 | pass
|