Hello, pylinters.

I'd like to submit a small patch allow @prop.setter syntax to work without
warnings in pylint.  Consider the following code:

1    @property
2    def x(self):
3        return self._x
4    @x.setter
5    def x(self, value):
6        self._x = value

This will currently trigger E1101 (no 'setter' member) for line 4 and  E0102
(method already defined) for line 5.  This patch should let this code
through without error.

This should resolve the following tickets:
http://www.logilab.org/ticket/50461
http://www.logilab.org/ticket/52020
http://www.logilab.org/ticket/51222

Thanks for a great tool!

                    - Damien
diff -r edc4c99cbd33 -r 46fd519f8188 checkers/base.py
--- a/checkers/base.py	Tue Oct 04 10:32:07 2011 +0200
+++ b/checkers/base.py	Thu Oct 06 16:45:03 2011 -0700
@@ -101,6 +101,22 @@
                   nice_stats[node_type].get('percent_badname', '0'))
     sect.append(Table(children=lines, cols=6, rheaders=1))
 
+def redefined_by_decorator(node):
+    """return True if the object is a method redefined via decorator.
+
+    For example:
+        @property
+        def x(self): return self._x
+        @x.setter
+        def x(self, value): self._x = value
+    """
+    if node.decorators:
+        for decorator in node.decorators.nodes:
+            if (isinstance(decorator, astng.Getattr) and
+                decorator.expr.name == node.name):
+                return True
+    return False
+
 class _BasicChecker(BaseChecker):
     __implements__ = IASTNGChecker
     name = 'basic'
@@ -142,7 +158,8 @@
 
     @check_messages('E0100', 'E0101', 'E0102', 'E0106')
     def visit_function(self, node):
-        self._check_redefinition(node.is_method() and 'method' or 'function', node)
+        if not redefined_by_decorator(node):
+            self._check_redefinition(node.is_method() and 'method' or 'function', node)
         # checks for max returns, branch, return in __init__
         returns = node.nodes_of_class(astng.Return,
                                       skip_klass=(astng.Function, astng.Class))
diff -r edc4c99cbd33 -r 46fd519f8188 checkers/typecheck.py
--- a/checkers/typecheck.py	Tue Oct 04 10:32:07 2011 +0200
+++ b/checkers/typecheck.py	Thu Oct 06 16:45:03 2011 -0700
@@ -170,6 +170,8 @@
                 # XXX method / function
                 continue
             except NotFoundError:
+                if isinstance(owner, astng.Function) and owner.decorators:
+                   continue
                 if isinstance(owner, Instance) and owner.has_dynamic_getattr():
                     continue
                 # explicit skipping of optparse'Values class
_______________________________________________
Python-Projects mailing list
[email protected]
http://lists.logilab.org/mailman/listinfo/python-projects

Reply via email to