Skip to content

Commit

Permalink
Added a new check, property-with-parameters.
Browse files Browse the repository at this point in the history
This check is emitted when we detect that a defined property also
has parameters, which are useless.
Close #3006
  • Loading branch information
PCManticore committed Jul 16, 2019
1 parent 0b68f34 commit a636569
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pylint/checkers/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
),
}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/property_with_parameters.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/functional/property_with_parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
property-with-parameters:6:Cls.attribute:Cannot have defined parameters for properties

0 comments on commit a636569

Please sign in to comment.