diff --git a/ChangeLog b/ChangeLog index 941233fc43..c4510509c8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,12 @@ What's New in Pylint 2.4.0? Release date: TBA +* Added a new check, ``property-with-parameters``. + + This check is emitted when we detect that a defined property also + has parameters, which are useless. + Close #3006 + * Excluded protocol classes from a couple of checks. Close #3002. * Add a check `unnecessary-comprehension` that detects unnecessary comprehensions. diff --git a/doc/whatsnew/2.4.rst b/doc/whatsnew/2.4.rst index 592a4e4296..bf4a8f068f 100644 --- a/doc/whatsnew/2.4.rst +++ b/doc/whatsnew/2.4.rst @@ -20,6 +20,11 @@ New checkers Close #2905 +* Added a new check ``property-with-parameters`` which detects when a property + has more than a single argument. + + Close #3006 + * Added `subprocess-run-check` to handle subprocess.run without explicitly set `check` keyword. Close #2848 diff --git a/pylint/checkers/classes.py b/pylint/checkers/classes.py index 2dc9d3b3be..31a3f574d3 100644 --- a/pylint/checkers/classes.py +++ b/pylint/checkers/classes.py @@ -623,6 +623,12 @@ def _has_same_layout_slots(slots, assigned_value): "Used when a class inherit from object, which under python3 is implicit, " "hence can be safely removed from bases.", ), + "R0206": ( + "Cannot have defined parameters for properties", + "property-with-parameters", + "Used when we detect that a property also has parameters, which are useless, " + "given that properties cannot be called with additional arguments.", + ), } @@ -871,6 +877,7 @@ def visit_functiondef(self, node): return self._check_useless_super_delegation(node) + self._check_property_with_parameters(node) klass = node.parent.frame() self._meth_could_be_func = True @@ -1053,6 +1060,10 @@ def form_annotations(annotations): "useless-super-delegation", node=function, args=(function.name,) ) + def _check_property_with_parameters(self, node): + if node.args.args and len(node.args.args) > 1 and decorated_with_property(node): + self.add_message("property-with-parameters", node=node) + def _check_slots(self, node): if "__slots__" not in node.locals: return diff --git a/tests/functional/property_with_parameters.py b/tests/functional/property_with_parameters.py new file mode 100644 index 0000000000..2f521991f1 --- /dev/null +++ b/tests/functional/property_with_parameters.py @@ -0,0 +1,7 @@ +# pylint: disable=missing-docstring, too-few-public-methods + + +class Cls: + @property + def attribute(self, param, param1): # [property-with-parameters] + return param + param1 diff --git a/tests/functional/property_with_parameters.txt b/tests/functional/property_with_parameters.txt new file mode 100644 index 0000000000..325d893068 --- /dev/null +++ b/tests/functional/property_with_parameters.txt @@ -0,0 +1 @@ +property-with-parameters:6:Cls.attribute:Cannot have defined parameters for properties