Skip to content

Commit

Permalink
add good-dunder-names option
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Oct 26, 2022
1 parent 37ca3dd commit 84f410e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pylint/extensions/dunder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from astroid import nodes

from pylint.checkers import BaseChecker
from pylint.constants import DUNDER_METHODS, EXTRA_DUNDER_METHODS, DUNDER_PROPERTIES
from pylint.constants import DUNDER_METHODS, DUNDER_PROPERTIES, EXTRA_DUNDER_METHODS
from pylint.interfaces import HIGH

if TYPE_CHECKING:
Expand All @@ -29,10 +29,24 @@ class DunderChecker(BaseChecker):
"not within the predefined list of dunder names.",
),
}
options = ()
options = (
(
"good-dunder-names",
{
"default": [],
"type": "csv",
"metavar": "<comma-separated names>",
"help": "Good dunder names which should always be accepted.",
},
),
)

def open(self) -> None:
self._dunder_methods = EXTRA_DUNDER_METHODS + DUNDER_PROPERTIES
self._dunder_methods = (
EXTRA_DUNDER_METHODS
+ DUNDER_PROPERTIES
+ self.linter.config.good_dunder_names
)
for since_vers, dunder_methods in DUNDER_METHODS.items():
if since_vers <= self.linter.config.py_version:
self._dunder_methods.extend(list(dunder_methods.keys()))
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/ext/bad_dunder/bad_dunder_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def __inv__(self): # [bad-dunder-name]
# author likely meant to call the invert dunder method
pass

def __allowed__(self):
# user-configured allowed dunder name
pass

def _protected_method(self):
print("Protected")

Expand Down
2 changes: 2 additions & 0 deletions tests/functional/ext/bad_dunder/bad_dunder_name.rc
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[MAIN]
load-plugins=pylint.extensions.dunder

good-dunder-names = __allowed__,

0 comments on commit 84f410e

Please sign in to comment.