From 84f410e561f34935978c2985b99e3301bd9a6a4e Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Wed, 26 Oct 2022 09:55:22 -0300 Subject: [PATCH] add good-dunder-names option --- pylint/extensions/dunder.py | 20 ++++++++++++++++--- .../ext/bad_dunder/bad_dunder_name.py | 4 ++++ .../ext/bad_dunder/bad_dunder_name.rc | 2 ++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pylint/extensions/dunder.py b/pylint/extensions/dunder.py index 8cf5335f624..e0e9af316ac 100644 --- a/pylint/extensions/dunder.py +++ b/pylint/extensions/dunder.py @@ -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: @@ -29,10 +29,24 @@ class DunderChecker(BaseChecker): "not within the predefined list of dunder names.", ), } - options = () + options = ( + ( + "good-dunder-names", + { + "default": [], + "type": "csv", + "metavar": "", + "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())) diff --git a/tests/functional/ext/bad_dunder/bad_dunder_name.py b/tests/functional/ext/bad_dunder/bad_dunder_name.py index 2cfb72d8e47..48247aba031 100644 --- a/tests/functional/ext/bad_dunder/bad_dunder_name.py +++ b/tests/functional/ext/bad_dunder/bad_dunder_name.py @@ -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") diff --git a/tests/functional/ext/bad_dunder/bad_dunder_name.rc b/tests/functional/ext/bad_dunder/bad_dunder_name.rc index c70980544f7..def1410c6f5 100644 --- a/tests/functional/ext/bad_dunder/bad_dunder_name.rc +++ b/tests/functional/ext/bad_dunder/bad_dunder_name.rc @@ -1,2 +1,4 @@ [MAIN] load-plugins=pylint.extensions.dunder + +good-dunder-names = __allowed__, \ No newline at end of file