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

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

Issue 1920403002: [content/test/gpu] Run pylint check of gpu tests in unittest instead of PRESUBMIT (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update path to LICENSE.txt of logilab/README.chromium Created 4 years, 7 months 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
OLDNEW
(Empty)
1 # Copyright (c) 2005-2014 LOGILAB S.A. (Paris, FRANCE).
2 # http://www.logilab.fr/ -- mailto:contact@logilab.fr
3 #
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
6 # Foundation; either version 2 of the License, or (at your option) any later
7 # version.
8 #
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
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 #
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.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 """check for new / old style related problems
17 """
18 import sys
19
20 import astroid
21
22 from pylint.interfaces import IAstroidChecker, INFERENCE, INFERENCE_FAILURE, HIG H
23 from pylint.checkers import BaseChecker
24 from pylint.checkers.utils import (
25 check_messages,
26 has_known_bases,
27 node_frame_class,
28 )
29
30 MSGS = {
31 'E1001': ('Use of __slots__ on an old style class',
32 'slots-on-old-class',
33 'Used when an old style class uses the __slots__ attribute.',
34 {'maxversion': (3, 0)}),
35 'E1002': ('Use of super on an old style class',
36 'super-on-old-class',
37 'Used when an old style class uses the super builtin.',
38 {'maxversion': (3, 0)}),
39 'E1003': ('Bad first argument %r given to super()',
40 'bad-super-call',
41 'Used when another argument than the current class is given as \
42 first argument of the super builtin.'),
43 'E1004': ('Missing argument to super()',
44 'missing-super-argument',
45 'Used when the super builtin didn\'t receive an \
46 argument.',
47 {'maxversion': (3, 0)}),
48 'W1001': ('Use of "property" on an old style class',
49 'property-on-old-class',
50 'Used when Pylint detect the use of the builtin "property" \
51 on an old style class while this is relying on new style \
52 classes features.',
53 {'maxversion': (3, 0)}),
54 'C1001': ('Old-style class defined.',
55 'old-style-class',
56 'Used when a class is defined that does not inherit from another'
57 'class and does not inherit explicitly from "object".',
58 {'maxversion': (3, 0)})
59 }
60
61
62 class NewStyleConflictChecker(BaseChecker):
63 """checks for usage of new style capabilities on old style classes and
64 other new/old styles conflicts problems
65 * use of property, __slots__, super
66 * "super" usage
67 """
68
69 __implements__ = (IAstroidChecker,)
70
71 # configuration section name
72 name = 'newstyle'
73 # messages
74 msgs = MSGS
75 priority = -2
76 # configuration options
77 options = ()
78
79 @check_messages('slots-on-old-class', 'old-style-class')
80 def visit_class(self, node):
81 """ Check __slots__ in old style classes and old
82 style class definition.
83 """
84 if '__slots__' in node and not node.newstyle:
85 confidence = (INFERENCE if has_known_bases(node)
86 else INFERENCE_FAILURE)
87 self.add_message('slots-on-old-class', node=node,
88 confidence=confidence)
89 # The node type could be class, exception, metaclass, or
90 # interface. Presumably, the non-class-type nodes would always
91 # have an explicit base class anyway.
92 if not node.bases and node.type == 'class' and not node.metaclass():
93 # We use confidence HIGH here because this message should only ever
94 # be emitted for classes at the root of the inheritance hierarchysel f.
95 self.add_message('old-style-class', node=node, confidence=HIGH)
96
97 @check_messages('property-on-old-class')
98 def visit_callfunc(self, node):
99 """check property usage"""
100 parent = node.parent.frame()
101 if (isinstance(parent, astroid.Class) and
102 not parent.newstyle and
103 isinstance(node.func, astroid.Name)):
104 confidence = (INFERENCE if has_known_bases(parent)
105 else INFERENCE_FAILURE)
106 name = node.func.name
107 if name == 'property':
108 self.add_message('property-on-old-class', node=node,
109 confidence=confidence)
110
111 @check_messages('super-on-old-class', 'bad-super-call', 'missing-super-argum ent')
112 def visit_function(self, node):
113 """check use of super"""
114 # ignore actual functions or method within a new style class
115 if not node.is_method():
116 return
117 klass = node.parent.frame()
118 for stmt in node.nodes_of_class(astroid.CallFunc):
119 if node_frame_class(stmt) != node_frame_class(node):
120 # Don't look down in other scopes.
121 continue
122 expr = stmt.func
123 if not isinstance(expr, astroid.Getattr):
124 continue
125 call = expr.expr
126 # skip the test if using super
127 if isinstance(call, astroid.CallFunc) and \
128 isinstance(call.func, astroid.Name) and \
129 call.func.name == 'super':
130 confidence = (INFERENCE if has_known_bases(klass)
131 else INFERENCE_FAILURE)
132 if not klass.newstyle:
133 # super should not be used on an old style class
134 self.add_message('super-on-old-class', node=node,
135 confidence=confidence)
136 else:
137 # super first arg should be the class
138 if not call.args and sys.version_info[0] == 3:
139 # unless Python 3
140 continue
141
142 try:
143 supcls = (call.args and next(call.args[0].infer())
144 or None)
145 except astroid.InferenceError:
146 continue
147
148 if supcls is None:
149 self.add_message('missing-super-argument', node=call,
150 confidence=confidence)
151 continue
152
153 if klass is not supcls:
154 name = None
155 # if supcls is not YES, then supcls was infered
156 # and use its name. Otherwise, try to look
157 # for call.args[0].name
158 if supcls is not astroid.YES:
159 name = supcls.name
160 else:
161 if hasattr(call.args[0], 'name'):
162 name = call.args[0].name
163 if name is not None:
164 self.add_message('bad-super-call',
165 node=call,
166 args=(name, ),
167 confidence=confidence)
168
169
170 def register(linter):
171 """required method to auto register this checker """
172 linter.register_checker(NewStyleConflictChecker(linter))
OLDNEW
« no previous file with comments | « third_party/pylint/pylint/checkers/misc.py ('k') | third_party/pylint/pylint/checkers/python3.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698