From eec60cf4c05f2753e848a8cf40854dae71043ee0 Mon Sep 17 00:00:00 2001 From: alexprabhat99 Date: Tue, 11 Feb 2025 15:06:45 +0530 Subject: [PATCH 1/3] ignore leading underscores in private methods for camelCase style --- pylint/checkers/base/name_checker/naming_style.py | 2 +- tests/functional/n/namePresetCamelCase.py | 4 +++- tests/functional/n/namePresetCamelCase.txt | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pylint/checkers/base/name_checker/naming_style.py b/pylint/checkers/base/name_checker/naming_style.py index 0198ae7d1e..72f4dd8ed9 100644 --- a/pylint/checkers/base/name_checker/naming_style.py +++ b/pylint/checkers/base/name_checker/naming_style.py @@ -64,7 +64,7 @@ class CamelCaseStyle(NamingStyle): MOD_NAME_RGX = re.compile(r"[^\W\dA-Z][^\W_]*$") CONST_NAME_RGX = re.compile(r"([^\W\dA-Z][^\W_]*|__.*__)$") COMP_VAR_RGX = MOD_NAME_RGX - DEFAULT_NAME_RGX = re.compile(r"([^\W\dA-Z][^\W_]*|__[^\W\dA-Z_]\w+__)$") + DEFAULT_NAME_RGX = re.compile(r"(?:__)?([^\W\dA-Z][^\W_]*|__[^\W\dA-Z_]\w+__)$") CLASS_ATTRIBUTE_RGX = re.compile(r"([^\W\dA-Z][^\W_]*|__.*__)$") diff --git a/tests/functional/n/namePresetCamelCase.py b/tests/functional/n/namePresetCamelCase.py index e048ec4d8a..862a560255 100644 --- a/tests/functional/n/namePresetCamelCase.py +++ b/tests/functional/n/namePresetCamelCase.py @@ -1,4 +1,4 @@ -# pylint: disable=missing-docstring,too-few-public-methods +# pylint: disable=missing-docstring,too-few-public-methods,unused-private-member __version__ = "1.0" SOME_CONSTANT = 42 # [invalid-name] @@ -18,6 +18,8 @@ def myPublicX(self): def __eq__(self, other): return isinstance(other, MyClass) and self.myPublicX == other.myPublicX + def __privateMethod(self): + pass def say_hello(): # [invalid-name] pass diff --git a/tests/functional/n/namePresetCamelCase.txt b/tests/functional/n/namePresetCamelCase.txt index 900f7dceb9..a68f721f8e 100644 --- a/tests/functional/n/namePresetCamelCase.txt +++ b/tests/functional/n/namePresetCamelCase.txt @@ -1,3 +1,3 @@ -invalid-name:3:0:3:13::"Constant name ""SOME_CONSTANT"" doesn't conform to camelCase naming style ('([^\\W\\dA-Z][^\\W_]*|__.*__)$' pattern)":HIGH -invalid-name:10:0:10:13:MyClass:"Class name ""MyClass"" doesn't conform to camelCase naming style ('[^\\W\\dA-Z][^\\W_]*$' pattern)":HIGH -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 +invalid-name:3:0:3:13::"Constant name ""SOME_CONSTANT"" doesn't conform to camelCase naming style ('([^\\W\\dA-Z][^\\W_]*|__.*__)$' pattern)":HIGH +invalid-name:10:0:10:13:MyClass:"Class name ""MyClass"" doesn't conform to camelCase naming style ('[^\\W\\dA-Z][^\\W_]*$' pattern)":HIGH +invalid-name:24:0:24:13:say_hello:"Function name ""say_hello"" doesn't conform to camelCase naming style ('(?:__)?([^\\W\\dA-Z][^\\W_]*|__[^\\W\\dA-Z_]\\w+__)$' pattern)":HIGH From 9eb990f1a663e5a2be429bf7ec57685aa8f67b95 Mon Sep 17 00:00:00 2001 From: Alex Prabhat Bara Date: Tue, 11 Feb 2025 15:13:58 +0530 Subject: [PATCH 2/3] added changelog --- doc/whatsnew/fragments/10189.bugfix | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 doc/whatsnew/fragments/10189.bugfix diff --git a/doc/whatsnew/fragments/10189.bugfix b/doc/whatsnew/fragments/10189.bugfix new file mode 100644 index 0000000000..4f5cf545de --- /dev/null +++ b/doc/whatsnew/fragments/10189.bugfix @@ -0,0 +1,3 @@ +Fixed raising invalid-name when using camelCase for private methods with two leading underscores. + +Closes #10189 From 0af27fcfbd6cc4d28fefd60f274b877fa0107312 Mon Sep 17 00:00:00 2001 From: alexprabhat99 Date: Tue, 11 Feb 2025 16:41:30 +0530 Subject: [PATCH 3/3] ignore leading underscore in protected methods for camelCase style --- tests/functional/n/namePresetCamelCase.py | 3 +++ tests/functional/n/namePresetCamelCase.txt | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/functional/n/namePresetCamelCase.py b/tests/functional/n/namePresetCamelCase.py index 862a560255..575b851c66 100644 --- a/tests/functional/n/namePresetCamelCase.py +++ b/tests/functional/n/namePresetCamelCase.py @@ -21,5 +21,8 @@ def __eq__(self, other): def __privateMethod(self): pass + def _protectedMethod(self): + pass + def say_hello(): # [invalid-name] pass diff --git a/tests/functional/n/namePresetCamelCase.txt b/tests/functional/n/namePresetCamelCase.txt index a68f721f8e..ff2f17ae29 100644 --- a/tests/functional/n/namePresetCamelCase.txt +++ b/tests/functional/n/namePresetCamelCase.txt @@ -1,3 +1,3 @@ invalid-name:3:0:3:13::"Constant name ""SOME_CONSTANT"" doesn't conform to camelCase naming style ('([^\\W\\dA-Z][^\\W_]*|__.*__)$' pattern)":HIGH invalid-name:10:0:10:13:MyClass:"Class name ""MyClass"" doesn't conform to camelCase naming style ('[^\\W\\dA-Z][^\\W_]*$' pattern)":HIGH -invalid-name:24:0:24:13:say_hello:"Function name ""say_hello"" doesn't conform to camelCase naming style ('(?:__)?([^\\W\\dA-Z][^\\W_]*|__[^\\W\\dA-Z_]\\w+__)$' pattern)":HIGH +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