Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(511)

Side by Side Diff: third_party/pylint/checkers/newstyle.py

Issue 739393004: Revert "Revert "pylint: upgrade to 1.3.1"" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools/
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/pylint/checkers/misc.py ('k') | third_party/pylint/checkers/raw_metrics.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2005-2006 LOGILAB S.A. (Paris, FRANCE). 1 # Copyright (c) 2005-2014 LOGILAB S.A. (Paris, FRANCE).
2 # http://www.logilab.fr/ -- mailto:contact@logilab.fr 2 # http://www.logilab.fr/ -- mailto:contact@logilab.fr
3 # 3 #
4 # This program is free software; you can redistribute it and/or modify it under 4 # This program is free software; you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License as published by the Free Software 5 # the terms of the GNU General Public License as published by the Free Software
6 # Foundation; either version 2 of the License, or (at your option) any later 6 # Foundation; either version 2 of the License, or (at your option) any later
7 # version. 7 # version.
8 # 8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT 9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License along with 13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 14 # this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 """check for new / old style related problems 16 """check for new / old style related problems
17 """ 17 """
18 import sys
18 19
19 from logilab import astng 20 import astroid
20 21
21 from pylint.interfaces import IASTNGChecker 22 from pylint.interfaces import IAstroidChecker
22 from pylint.checkers import BaseChecker 23 from pylint.checkers import BaseChecker
23 from pylint.checkers.utils import check_messages 24 from pylint.checkers.utils import check_messages
24 25
25 MSGS = { 26 MSGS = {
26 'E1001': ('Use of __slots__ on an old style class', 27 'E1001': ('Use of __slots__ on an old style class',
27 'Used when an old style class uses the __slots__ attribute.'), 28 'slots-on-old-class',
29 'Used when an old style class uses the __slots__ attribute.',
30 {'maxversion': (3, 0)}),
28 'E1002': ('Use of super on an old style class', 31 'E1002': ('Use of super on an old style class',
29 'Used when an old style class uses the super builtin.'), 32 'super-on-old-class',
30 'E1003': ('Bad first argument %r given to super class', 33 'Used when an old style class uses the super builtin.',
34 {'maxversion': (3, 0)}),
35 'E1003': ('Bad first argument %r given to super()',
36 'bad-super-call',
31 'Used when another argument than the current class is given as \ 37 'Used when another argument than the current class is given as \
32 first argument of the super builtin.'), 38 first argument of the super builtin.'),
39 'E1004': ('Missing argument to super()',
40 'missing-super-argument',
41 'Used when the super builtin didn\'t receive an \
42 argument.',
43 {'maxversion': (3, 0)}),
33 'W1001': ('Use of "property" on an old style class', 44 'W1001': ('Use of "property" on an old style class',
45 'property-on-old-class',
34 'Used when PyLint detect the use of the builtin "property" \ 46 'Used when PyLint detect the use of the builtin "property" \
35 on an old style class while this is relying on new style \ 47 on an old style class while this is relying on new style \
36 classes features'), 48 classes features.',
49 {'maxversion': (3, 0)}),
50 'C1001': ('Old-style class defined.',
51 'old-style-class',
52 'Used when a class is defined that does not inherit from another'
53 'class and does not inherit explicitly from "object".',
54 {'maxversion': (3, 0)})
37 } 55 }
38 56
39 57
40 class NewStyleConflictChecker(BaseChecker): 58 class NewStyleConflictChecker(BaseChecker):
41 """checks for usage of new style capabilities on old style classes and 59 """checks for usage of new style capabilities on old style classes and
42 other new/old styles conflicts problems 60 other new/old styles conflicts problems
43 * use of property, __slots__, super 61 * use of property, __slots__, super
44 * "super" usage 62 * "super" usage
45 """ 63 """
46 64
47 __implements__ = (IASTNGChecker,) 65 __implements__ = (IAstroidChecker,)
48 66
49 # configuration section name 67 # configuration section name
50 name = 'newstyle' 68 name = 'newstyle'
51 # messages 69 # messages
52 msgs = MSGS 70 msgs = MSGS
53 priority = -2 71 priority = -2
54 # configuration options 72 # configuration options
55 options = () 73 options = ()
56 74
57 @check_messages('E1001') 75 @check_messages('slots-on-old-class', 'old-style-class')
58 def visit_class(self, node): 76 def visit_class(self, node):
59 """check __slots__ usage 77 """check __slots__ usage
60 """ 78 """
61 if '__slots__' in node and not node.newstyle: 79 if '__slots__' in node and not node.newstyle:
62 self.add_message('E1001', node=node) 80 self.add_message('slots-on-old-class', node=node)
81 # The node type could be class, exception, metaclass, or
82 # interface. Presumably, the non-class-type nodes would always
83 # have an explicit base class anyway.
84 if not node.bases and node.type == 'class':
85 self.add_message('old-style-class', node=node)
63 86
64 @check_messages('W1001') 87 @check_messages('property-on-old-class')
65 def visit_callfunc(self, node): 88 def visit_callfunc(self, node):
66 """check property usage""" 89 """check property usage"""
67 parent = node.parent.frame() 90 parent = node.parent.frame()
68 if (isinstance(parent, astng.Class) and 91 if (isinstance(parent, astroid.Class) and
69 not parent.newstyle and 92 not parent.newstyle and
70 isinstance(node.func, astng.Name)): 93 isinstance(node.func, astroid.Name)):
71 name = node.func.name 94 name = node.func.name
72 if name == 'property': 95 if name == 'property':
73 self.add_message('W1001', node=node) 96 self.add_message('property-on-old-class', node=node)
74 97
75 @check_messages('E1002', 'E1003') 98 @check_messages('super-on-old-class', 'bad-super-call', 'missing-super-argum ent')
76 def visit_function(self, node): 99 def visit_function(self, node):
77 """check use of super""" 100 """check use of super"""
78 # ignore actual functions or method within a new style class 101 # ignore actual functions or method within a new style class
79 if not node.is_method(): 102 if not node.is_method():
80 return 103 return
81 klass = node.parent.frame() 104 klass = node.parent.frame()
82 for stmt in node.nodes_of_class(astng.CallFunc): 105 for stmt in node.nodes_of_class(astroid.CallFunc):
83 expr = stmt.func 106 expr = stmt.func
84 if not isinstance(expr, astng.Getattr): 107 if not isinstance(expr, astroid.Getattr):
85 continue 108 continue
86 call = expr.expr 109 call = expr.expr
87 # skip the test if using super 110 # skip the test if using super
88 if isinstance(call, astng.CallFunc) and \ 111 if isinstance(call, astroid.CallFunc) and \
89 isinstance(call.func, astng.Name) and \ 112 isinstance(call.func, astroid.Name) and \
90 call.func.name == 'super': 113 call.func.name == 'super':
91 if not klass.newstyle: 114 if not klass.newstyle:
92 # super should not be used on an old style class 115 # super should not be used on an old style class
93 self.add_message('E1002', node=node) 116 self.add_message('super-on-old-class', node=node)
94 else: 117 else:
95 # super first arg should be the class 118 # super first arg should be the class
119 if not call.args and sys.version_info[0] == 3:
120 # unless Python 3
121 continue
122
96 try: 123 try:
97 supcls = (call.args and call.args[0].infer().next() 124 supcls = (call.args and call.args[0].infer().next()
98 or None) 125 or None)
99 except astng.InferenceError: 126 except astroid.InferenceError:
100 continue 127 continue
128
129 if supcls is None:
130 self.add_message('missing-super-argument', node=call)
131 continue
132
101 if klass is not supcls: 133 if klass is not supcls:
102 supcls = getattr(supcls, 'name', supcls) 134 name = None
103 self.add_message('E1003', node=node, args=supcls) 135 # if supcls is not YES, then supcls was infered
136 # and use its name. Otherwise, try to look
137 # for call.args[0].name
138 if supcls is not astroid.YES:
139 name = supcls.name
140 else:
141 if hasattr(call.args[0], 'name'):
142 name = call.args[0].name
143 if name is not None:
144 self.add_message('bad-super-call',
145 node=call,
146 args=(name, ))
104 147
105 148
106 def register(linter): 149 def register(linter):
107 """required method to auto register this checker """ 150 """required method to auto register this checker """
108 linter.register_checker(NewStyleConflictChecker(linter)) 151 linter.register_checker(NewStyleConflictChecker(linter))
OLDNEW
« no previous file with comments | « third_party/pylint/checkers/misc.py ('k') | third_party/pylint/checkers/raw_metrics.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698