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

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

Issue 753543006: pylint: upgrade to 1.4.0 (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Created 6 years 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
« no previous file with comments | « third_party/pylint/checkers/misc.py ('k') | third_party/pylint/checkers/python3.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-2014 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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 import sys
19 19
20 import astroid 20 import astroid
21 21
22 from pylint.interfaces import IAstroidChecker 22 from pylint.interfaces import IAstroidChecker, INFERENCE, INFERENCE_FAILURE, HIG H
23 from pylint.checkers import BaseChecker 23 from pylint.checkers import BaseChecker
24 from pylint.checkers.utils import check_messages 24 from pylint.checkers.utils import (
25 check_messages,
26 has_known_bases,
27 node_frame_class,
28 )
25 29
26 MSGS = { 30 MSGS = {
27 'E1001': ('Use of __slots__ on an old style class', 31 'E1001': ('Use of __slots__ on an old style class',
28 'slots-on-old-class', 32 'slots-on-old-class',
29 'Used when an old style class uses the __slots__ attribute.', 33 'Used when an old style class uses the __slots__ attribute.',
30 {'maxversion': (3, 0)}), 34 {'maxversion': (3, 0)}),
31 'E1002': ('Use of super on an old style class', 35 'E1002': ('Use of super on an old style class',
32 'super-on-old-class', 36 'super-on-old-class',
33 'Used when an old style class uses the super builtin.', 37 'Used when an old style class uses the super builtin.',
34 {'maxversion': (3, 0)}), 38 {'maxversion': (3, 0)}),
35 'E1003': ('Bad first argument %r given to super()', 39 'E1003': ('Bad first argument %r given to super()',
36 'bad-super-call', 40 'bad-super-call',
37 'Used when another argument than the current class is given as \ 41 'Used when another argument than the current class is given as \
38 first argument of the super builtin.'), 42 first argument of the super builtin.'),
39 'E1004': ('Missing argument to super()', 43 'E1004': ('Missing argument to super()',
40 'missing-super-argument', 44 'missing-super-argument',
41 'Used when the super builtin didn\'t receive an \ 45 'Used when the super builtin didn\'t receive an \
42 argument.', 46 argument.',
43 {'maxversion': (3, 0)}), 47 {'maxversion': (3, 0)}),
44 'W1001': ('Use of "property" on an old style class', 48 'W1001': ('Use of "property" on an old style class',
45 'property-on-old-class', 49 'property-on-old-class',
46 'Used when PyLint detect the use of the builtin "property" \ 50 'Used when Pylint detect the use of the builtin "property" \
47 on an old style class while this is relying on new style \ 51 on an old style class while this is relying on new style \
48 classes features.', 52 classes features.',
49 {'maxversion': (3, 0)}), 53 {'maxversion': (3, 0)}),
50 'C1001': ('Old-style class defined.', 54 'C1001': ('Old-style class defined.',
51 'old-style-class', 55 'old-style-class',
52 'Used when a class is defined that does not inherit from another' 56 'Used when a class is defined that does not inherit from another'
53 'class and does not inherit explicitly from "object".', 57 'class and does not inherit explicitly from "object".',
54 {'maxversion': (3, 0)}) 58 {'maxversion': (3, 0)})
55 } 59 }
56 60
(...skipping 10 matching lines...) Expand all
67 # configuration section name 71 # configuration section name
68 name = 'newstyle' 72 name = 'newstyle'
69 # messages 73 # messages
70 msgs = MSGS 74 msgs = MSGS
71 priority = -2 75 priority = -2
72 # configuration options 76 # configuration options
73 options = () 77 options = ()
74 78
75 @check_messages('slots-on-old-class', 'old-style-class') 79 @check_messages('slots-on-old-class', 'old-style-class')
76 def visit_class(self, node): 80 def visit_class(self, node):
77 """check __slots__ usage 81 """ Check __slots__ in old style classes and old
82 style class definition.
78 """ 83 """
79 if '__slots__' in node and not node.newstyle: 84 if '__slots__' in node and not node.newstyle:
80 self.add_message('slots-on-old-class', node=node) 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)
81 # The node type could be class, exception, metaclass, or 89 # The node type could be class, exception, metaclass, or
82 # interface. Presumably, the non-class-type nodes would always 90 # interface. Presumably, the non-class-type nodes would always
83 # have an explicit base class anyway. 91 # have an explicit base class anyway.
84 if not node.bases and node.type == 'class': 92 if not node.bases and node.type == 'class' and not node.metaclass():
85 self.add_message('old-style-class', node=node) 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)
86 96
87 @check_messages('property-on-old-class') 97 @check_messages('property-on-old-class')
88 def visit_callfunc(self, node): 98 def visit_callfunc(self, node):
89 """check property usage""" 99 """check property usage"""
90 parent = node.parent.frame() 100 parent = node.parent.frame()
91 if (isinstance(parent, astroid.Class) and 101 if (isinstance(parent, astroid.Class) and
92 not parent.newstyle and 102 not parent.newstyle and
93 isinstance(node.func, astroid.Name)): 103 isinstance(node.func, astroid.Name)):
104 confidence = (INFERENCE if has_known_bases(parent)
105 else INFERENCE_FAILURE)
94 name = node.func.name 106 name = node.func.name
95 if name == 'property': 107 if name == 'property':
96 self.add_message('property-on-old-class', node=node) 108 self.add_message('property-on-old-class', node=node,
109 confidence=confidence)
97 110
98 @check_messages('super-on-old-class', 'bad-super-call', 'missing-super-argum ent') 111 @check_messages('super-on-old-class', 'bad-super-call', 'missing-super-argum ent')
99 def visit_function(self, node): 112 def visit_function(self, node):
100 """check use of super""" 113 """check use of super"""
101 # ignore actual functions or method within a new style class 114 # ignore actual functions or method within a new style class
102 if not node.is_method(): 115 if not node.is_method():
103 return 116 return
104 klass = node.parent.frame() 117 klass = node.parent.frame()
105 for stmt in node.nodes_of_class(astroid.CallFunc): 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
106 expr = stmt.func 122 expr = stmt.func
107 if not isinstance(expr, astroid.Getattr): 123 if not isinstance(expr, astroid.Getattr):
108 continue 124 continue
109 call = expr.expr 125 call = expr.expr
110 # skip the test if using super 126 # skip the test if using super
111 if isinstance(call, astroid.CallFunc) and \ 127 if isinstance(call, astroid.CallFunc) and \
112 isinstance(call.func, astroid.Name) and \ 128 isinstance(call.func, astroid.Name) and \
113 call.func.name == 'super': 129 call.func.name == 'super':
130 confidence = (INFERENCE if has_known_bases(klass)
131 else INFERENCE_FAILURE)
114 if not klass.newstyle: 132 if not klass.newstyle:
115 # super should not be used on an old style class 133 # super should not be used on an old style class
116 self.add_message('super-on-old-class', node=node) 134 self.add_message('super-on-old-class', node=node,
135 confidence=confidence)
117 else: 136 else:
118 # super first arg should be the class 137 # super first arg should be the class
119 if not call.args and sys.version_info[0] == 3: 138 if not call.args and sys.version_info[0] == 3:
120 # unless Python 3 139 # unless Python 3
121 continue 140 continue
122 141
123 try: 142 try:
124 supcls = (call.args and call.args[0].infer().next() 143 supcls = (call.args and next(call.args[0].infer())
125 or None) 144 or None)
126 except astroid.InferenceError: 145 except astroid.InferenceError:
127 continue 146 continue
128 147
129 if supcls is None: 148 if supcls is None:
130 self.add_message('missing-super-argument', node=call) 149 self.add_message('missing-super-argument', node=call,
150 confidence=confidence)
131 continue 151 continue
132 152
133 if klass is not supcls: 153 if klass is not supcls:
134 name = None 154 name = None
135 # if supcls is not YES, then supcls was infered 155 # if supcls is not YES, then supcls was infered
136 # and use its name. Otherwise, try to look 156 # and use its name. Otherwise, try to look
137 # for call.args[0].name 157 # for call.args[0].name
138 if supcls is not astroid.YES: 158 if supcls is not astroid.YES:
139 name = supcls.name 159 name = supcls.name
140 else: 160 else:
141 if hasattr(call.args[0], 'name'): 161 if hasattr(call.args[0], 'name'):
142 name = call.args[0].name 162 name = call.args[0].name
143 if name is not None: 163 if name is not None:
144 self.add_message('bad-super-call', 164 self.add_message('bad-super-call',
145 node=call, 165 node=call,
146 args=(name, )) 166 args=(name, ),
167 confidence=confidence)
147 168
148 169
149 def register(linter): 170 def register(linter):
150 """required method to auto register this checker """ 171 """required method to auto register this checker """
151 linter.register_checker(NewStyleConflictChecker(linter)) 172 linter.register_checker(NewStyleConflictChecker(linter))
OLDNEW
« no previous file with comments | « third_party/pylint/checkers/misc.py ('k') | third_party/pylint/checkers/python3.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698