Doesn't check that overriden method signature match if it's a private method, closes #18772

authorSylvain Th?nault <sylvain.thenault@logilab.fr>
changeset085dad997b71
branchdefault
phasepublic
hiddenno
parent revision#8752260a4f41 fix crash when using set in regexp for --generated-members option. Closes #88914
child revision#40de65ba89b2 Add checking for protected attribute assignement, closes #7394.
files modified by this revision
ChangeLog
checkers/classes.py
checkers/utils.py
test/input/func_noerror_classes_meth_signature.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1339073643 -7200
# Thu Jun 07 14:54:03 2012 +0200
# Node ID 085dad997b71fc45e65cd5591d9482d5817e4210
# Parent 8752260a4f4183cfe7b068ce39f8a50ad9b26f75
Doesn't check that overriden method signature match if it's a private method, closes #18772

diff --git a/ChangeLog b/ChangeLog
@@ -1,10 +1,13 @@
1  ChangeLog for PyLint
2  ====================
3 
4  	--
5 
6 +    * #18772 no prototype consistency check for mangled methods (patch by
7 +    lothiraldan@gmail.com)
8 +
9      * #92911: emit W0102 when sets are used as default arguments in functions
10        (patch by tmarek@google.com)
11 
12      * #77982: do not emit E0602 for loop variables of comprehensions
13        used as argument values inside a decorator (patch by tmarek@google.com)
@@ -385,11 +388,11 @@
14 
15      * new W0614 message to differentiate between unused `import X` and
16        unused `from X import *` (#3209, patch submitted by Daniel Drake)
17 
18      * included Daniel Drake's patch to have a different message E1003 instead of
19 -      E1001 when a missing member is found but an inference failure has been 
20 +      E1001 when a missing member is found but an inference failure has been
21        detected
22 
23      * msvs reporter for Visual Studio line number reporting (#3285)
24 
25      * allow disable-all option inline (#3218, patch submitted by Daniel Drake)
@@ -448,11 +451,11 @@
26      * usability changes:
27 
28          - parseable, html and color options are now handled by a single
29  	output-format option
30          - enable-<checkerid> and disable-all options are now handled by
31 -	two (exclusive) enable-checker and disable-checker options 
32 +	two (exclusive) enable-checker and disable-checker options
33  	taking a comma separated list of checker names as value
34          - renamed debug-mode option to errors-only
35 
36      * started a reference user manual
37 
@@ -480,11 +483,11 @@
38 
39      * fix some E1101 false positive with abstract method or classes defining
40        __getattr__
41 
42      * dirty fix to avoid "_socketobject" has not "connect" member. The actual
43 -      problem is that astng isn't able to understand the code used to create 
44 +      problem is that astng isn't able to understand the code used to create
45        socket.socket object with exec
46 
47      * added an option in the similarity checker to ignore docstrings, enabled
48        by default
49 
@@ -508,35 +511,35 @@
50      * E0212 and E0214 (metaclass/class method should have mcs/cls as first
51        argument have been reclassified to C0202 and C0203 since this not as
52        well established as "self" for instance method (E0213)
53 
54      * W0224 has been reclassified into F0220 (failed to resolve interfaces
55 -      implemented by a class)   
56 +      implemented by a class)
57 
58      * a new typecheck checker, introducing the following checks:
59 
60          - E1101, access to unexistent member (implements #10430), remove
61            the need of E0201 and so some options has been moved from the
62            classes checker to this one
63          - E1102, calling a non callable object
64          - E1111 and W1111 when an assignment is done on a function call but the
65 -          inferred function returns None (implements #10431) 
66 +          inferred function returns None (implements #10431)
67 
68      * change in the base checker:
69 
70          - checks module level and instance attribute names (new const-rgx
71            and attr-rgx configuration option) (implements #10209  and
72 -          #10440) 
73 +          #10440)
74          - list comprehension and generator expression variables have their
75            own regular expression  (the inlinevar-rgx option) (implements
76            #9146)
77          - the C0101 check with its min-name-length option has
78            been removed (this can be specified in the regxp after all...)
79          - W0103 and W0121 are now handled by the variables checker
80            (W0103 is now W0603 and W0604 has been splitted into different messages)
81          - W0131 and W0132 messages  have been reclassified to C0111 and
82 -          C0112 respectively 
83 +          C0112 respectively
84          - new W0104 message on statement without effect
85 
86      * regexp support for dummy-variables (dummy-variables-rgx option
87        replace dummy-variables) (implements #10027)
88 
@@ -574,11 +577,11 @@
89 
90      * fixed some E0203 ("access to member before its definition") false
91        positive
92 
93      * fixed E0214 ("metaclass method first argument should be mcs) false
94 -      positive with staticmethod used on a metaclass 
95 +      positive with staticmethod used on a metaclass
96 
97      * fixed packaging which was missing the test/regrtest_data directory
98 
99      * W0212 (method could be a function) has been reclassified in the
100        REFACTOR category as R0201, and is no more considerer when a method
@@ -633,11 +636,11 @@
101      * make profile option saveable
102 
103      * fix Windows .bat file,  patch contributed by Amaury Forgeot d'Arc
104 
105      * fix one more false positive for E0601 (access before definition)
106 -      with for loop such as "for i in range(10): print i" (test 
107 +      with for loop such as "for i in range(10): print i" (test
108        func_noerror_defined_and_used_on_same_line)
109 
110      * fix false positive for E0201 (undefined member) when accessing to
111        __name__ on a class object
112 
@@ -655,11 +658,11 @@
113  2005-05-27  --  0.7.0
114      * WARNING: pylint is no longer a logilab subpackage. Users may have to
115        manually remove the old logilab/pylint directory.
116 
117      * introduce a new --additional-builtins option to handle user defined
118 -      builtins 
119 +      builtins
120 
121      * --reports option has now -r as short alias, and -i for --include-ids
122 
123      * fix a bug in the variables checker which may causing some false
124        positives when variables are defined and used within the same
@@ -687,11 +690,11 @@
125      * fix bug in the variables checker, causing non detection of some
126        actual name error (close #8884, test
127        func_nameerror_on_string_substitution)
128 
129      * fix bug in the classes checker which was making pylint crash if
130 -      "object" is assigned in a class inheriting from it (test 
131 +      "object" is assigned in a class inheriting from it (test
132        func_noerror_object_as_class_attribute)
133 
134      * fix problem with the similar checker when related options are
135        defined in a configuration file
136 
@@ -711,11 +714,11 @@
137 
138 
139 
140  2005-02-16  --  0.6.2
141      * fix false positive on E0201 ("access to undefined member") with
142 -      metaclasses 
143 +      metaclasses
144 
145      * fix false positive on E0203 ("access to member before its
146        definition") when attributes are defined in a parent class
147 
148      * fix false positive on W0706 ("identifier used to raise an exception
@@ -750,16 +753,16 @@
149 
150      * fix a bug which may cause crashes on resolving parent classes
151 
152      * fix problems with the format checker: don't chock on files
153        containing multiple CR, avoid C0322, C0323, C0324 false positives
154 -      with triple quoted string with quote inside 
155 +      with triple quoted string with quote inside
156 
157      * correctly detect access to member defined latter in __init__ method
158 
159      * now depends on common 0.8.1 to fix problem with interface resolution
160 -      (close #8606)	
161 +      (close #8606)
162 
163      * new --list-msgs option describing available checkers and their
164        messages
165 
166      * added windows specific documentation to the README file, contributed
diff --git a/checkers/classes.py b/checkers/classes.py
@@ -20,11 +20,12 @@
167  from logilab import astng
168  from logilab.astng import YES, Instance, are_exclusive
169 
170  from pylint.interfaces import IASTNGChecker
171  from pylint.checkers import BaseChecker
172 -from pylint.checkers.utils import PYMETHODS, overrides_a_method, check_messages
173 +from pylint.checkers.utils import (PYMETHODS, overrides_a_method,
174 +    check_messages, is_attr_private)
175 
176  def class_is_abstract(node):
177      """return true if the given class node should be considered as an abstract
178      class
179      """
@@ -527,10 +528,12 @@
180          if method1.args.args is None or refmethod.args.args is None:
181              return
182          # if we use *args, **kwargs, skip the below checks
183          if method1.args.vararg or method1.args.kwarg:
184              return
185 +        if is_attr_private(method1.name):
186 +            return
187          if len(method1.args.args) != len(refmethod.args.args):
188              self.add_message('W0221', args=class_type, node=method1)
189          elif len(method1.args.defaults) < len(refmethod.args.defaults):
190              self.add_message('W0222', args=class_type, node=method1)
191 
diff --git a/checkers/utils.py b/checkers/utils.py
@@ -16,10 +16,11 @@
192  # this program; if not, write to the Free Software Foundation, Inc.,
193  # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
194  """some functions that may be useful for various checkers
195  """
196 
197 +import re
198  import string
199  from logilab import astng
200  from logilab.astng import scoped_nodes
201  from logilab.common.compat import builtins
202  BUILTINS_NAME = builtins.__name__
@@ -44,12 +45,12 @@
203          name = node.name
204          if is_builtin(name):
205              return (True, (name, 'builtins'))
206          else:
207              scope, stmts = node.lookup(name)
208 -            if (stmts and 
209 -                not isinstance(stmts[0].ass_type(), 
210 +            if (stmts and
211 +                not isinstance(stmts[0].ass_type(),
212                                 (astng.Assign, astng.AugAssign, astng.ExceptHandler))):
213                  return (True, (name, 'outer scope (line %i)' % (stmts[0].lineno,)))
214      return (False, None)
215 
216 
@@ -318,5 +319,12 @@
217                  keys.add(key)
218              elif c != '%':
219                  num_args += 1
220          i += 1
221      return keys, num_args
222 +
223 +def is_attr_private(attrname):
224 +    """Check that attribute name is private (at least two leading underscores,
225 +    at most one trailing underscore)
226 +    """
227 +    regex = re.compile('^_{2,}.*[^_]+_?$')
228 +    return regex.match(attrname)
229 \ No newline at end of file
diff --git a/test/input/func_noerror_classes_meth_signature.py b/test/input/func_noerror_classes_meth_signature.py
@@ -1,12 +1,38 @@
230 -# pylint: disable=C0111,R0922,R0903
231 -"""#2485
232 +# pylint: disable=C0111,R0922,R0903,W0231
233 +"""#2485:
234  W0222 "Signature differs from overriden method" false positive
235 +#18772:
236 +no prototype consistency check for mangled methods
237  """
238  __revision__ = 1
239  class Super(object):
240 +    def __init__(self):
241 +        pass
242 +
243 +    def __private(self):
244 +        pass
245 +
246 +    def __private2_(self):
247 +        pass
248 +
249 +    def ___private3(self):
250 +        pass
251 +
252      def method(self, param):
253          raise NotImplementedError
254 
255  class Sub(Super):
256 +    def __init__(self, arg):
257 +        pass
258 +
259 +    def __private(self, arg):
260 +        pass
261 +
262 +    def __private2_(self, arg):
263 +        pass
264 +
265 +    def ___private3(self, arg):
266 +        pass
267 +
268      def method(self, param = 'abc'):
269          pass