Oops, forgot about that part! I've attached a new diff which now
includes the input and message test files. They show the bug is fixed,
and that pylint still properly detects when an attribute is not defined
in a proper place.
If there's anything else needed, let me know.
Thanks,
Scott
Sylvain Thénault wrote:
On 22 mars 12:57, Scott Pilkey wrote:
Hello,
Hi,
I came up with this fix a little while ago, but haven't gotten a
chance to submit my proposed fix until now.
It's very straight-forward. In the existing code, you only check the
first item in any node list, when the entire list actually needs to
be checked to see if the defining method exists. I just deleted a
few lines, added a loop, and bingo, all finished. It fixes the
example problem in the ticket description, and I believe it is
correct.
I have attached the diff (hg diff > pylint9018.diff). If there's
anything else required to get this patch approved (assuming it's
good), just let me know.
could you please add a test to the functional test suite (see
test/input and test/messages, more info on the ml archives) demonstrating
the pb is fixed and won't show up anymore?
diff -r f5f084e5267a checkers/classes.py
--- a/checkers/classes.py Thu Mar 04 12:12:32 2010 +0100
+++ b/checkers/classes.py Wed Mar 24 04:34:33 2010 -0400
@@ -179,13 +179,20 @@
isinstance(n.statement(), (astng.Delete, astng.AugAssign))]
if not nodes:
continue # error detected by typechecking
- node = nodes[0] # XXX
- frame = node.frame()
- if frame.name not in defining_methods:
+ attr_defined = False
+ # check if any method attr is defined in is a defining method
+ for node in nodes:
+ if node.frame().name in defining_methods:
+ attr_defined = True
+ if not attr_defined:
# check attribute is defined in a parent's __init__
for parent in cnode.instance_attr_ancestors(attr):
- frame = parent.instance_attrs[attr][0].frame() # XXX
- if frame.name in defining_methods:
+ attr_defined = False
+ # check if any parent method attr is defined in is a
defining method
+ for node in parent.instance_attrs[attr]:
+ if node.frame().name in defining_methods:
+ attr_defined = True
+ if attr_defined:
# we're done :)
break
else:
diff -r f5f084e5267a test/input/func_defining-attr-methods_order.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/input/func_defining-attr-methods_order.py Wed Mar 24 04:34:33
2010 -0400
@@ -0,0 +1,33 @@
+# pylint: disable-msg=C0103
+
+''' Test that y is defined properly, z is not.
+ Default defining methods are __init__,
+ __new__, and setUp.
+ Order of methods should not matter. '''
+
+__revision__ = ''
+
+class A:
+ ''' class A '''
+
+ def __init__(self):
+ ''' __init__ docstring filler '''
+ self.x = 0
+ self.setUp()
+
+ def set_y(self, y):
+ ''' set_y docstring filler '''
+ self.y = y
+
+ def set_x(self, x):
+ ''' set_x docstring filler '''
+ self.x = x
+
+ def set_z(self, z):
+ ''' set_z docstring filler '''
+ self.z = z
+
+ def setUp(self):
+ ''' setUp docstring filler '''
+ self.x = 0
+ self.y = 0
diff -r f5f084e5267a test/messages/func_defining-attr-methods_order.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/messages/func_defining-attr-methods_order.txt Wed Mar 24
04:34:33 2010 -0400
@@ -0,0 +1,1 @@
+W: 28:A.set_z: Attribute 'z' defined outside __init__
_______________________________________________
Python-Projects mailing list
Python-Projects@lists.logilab.org
http://lists.logilab.org/mailman/listinfo/python-projects