Skip to content

Commit 5c6950c

Browse files
Ignore leading underscores in private/protected methods for camelCase style (#10228)
1 parent 3f99fe7 commit 5c6950c

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

doc/whatsnew/fragments/10189.bugfix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed raising invalid-name when using camelCase for private methods with two leading underscores.
2+
3+
Closes #10189

pylint/checkers/base/name_checker/naming_style.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CamelCaseStyle(NamingStyle):
6464
MOD_NAME_RGX = re.compile(r"[^\W\dA-Z][^\W_]*$")
6565
CONST_NAME_RGX = re.compile(r"([^\W\dA-Z][^\W_]*|__.*__)$")
6666
COMP_VAR_RGX = MOD_NAME_RGX
67-
DEFAULT_NAME_RGX = re.compile(r"([^\W\dA-Z][^\W_]*|__[^\W\dA-Z_]\w+__)$")
67+
DEFAULT_NAME_RGX = re.compile(r"(?:__)?([^\W\dA-Z][^\W_]*|__[^\W\dA-Z_]\w+__)$")
6868
CLASS_ATTRIBUTE_RGX = re.compile(r"([^\W\dA-Z][^\W_]*|__.*__)$")
6969

7070

tests/functional/n/namePresetCamelCase.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# pylint: disable=missing-docstring,too-few-public-methods
1+
# pylint: disable=missing-docstring,too-few-public-methods,unused-private-member
22
__version__ = "1.0"
33
SOME_CONSTANT = 42 # [invalid-name]
44

@@ -18,6 +18,11 @@ def myPublicX(self):
1818
def __eq__(self, other):
1919
return isinstance(other, MyClass) and self.myPublicX == other.myPublicX
2020

21+
def __privateMethod(self):
22+
pass
23+
24+
def _protectedMethod(self):
25+
pass
2126

2227
def say_hello(): # [invalid-name]
2328
pass
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
invalid-name:3:0:3:13::"Constant name ""SOME_CONSTANT"" doesn't conform to camelCase naming style ('([^\\W\\dA-Z][^\\W_]*|__.*__)$' pattern)":HIGH
2-
invalid-name:10:0:10:13:MyClass:"Class name ""MyClass"" doesn't conform to camelCase naming style ('[^\\W\\dA-Z][^\\W_]*$' pattern)":HIGH
3-
invalid-name:22:0:22:13:say_hello:"Function name ""say_hello"" doesn't conform to camelCase naming style ('([^\\W\\dA-Z][^\\W_]*|__[^\\W\\dA-Z_]\\w+__)$' pattern)":HIGH
1+
invalid-name:3:0:3:13::"Constant name ""SOME_CONSTANT"" doesn't conform to camelCase naming style ('([^\\W\\dA-Z][^\\W_]*|__.*__)$' pattern)":HIGH
2+
invalid-name:10:0:10:13:MyClass:"Class name ""MyClass"" doesn't conform to camelCase naming style ('[^\\W\\dA-Z][^\\W_]*$' pattern)":HIGH
3+
invalid-name:27:0:27:13:say_hello:"Function name ""say_hello"" doesn't conform to camelCase naming style ('(?:__)?([^\\W\\dA-Z][^\\W_]*|__[^\\W\\dA-Z_]\\w+__)$' pattern)":HIGH

0 commit comments

Comments
 (0)