OLD | NEW |
1 # Copyright (c) 2005-2014 LOGILAB S.A. (Paris, FRANCE). | 1 # Copyright (c) 2005-2006 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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
16 """check for new / old style related problems | 16 """check for new / old style related problems |
17 """ | 17 """ |
18 import sys | |
19 | 18 |
20 import astroid | 19 from logilab import astng |
21 | 20 |
22 from pylint.interfaces import IAstroidChecker | 21 from pylint.interfaces import IASTNGChecker |
23 from pylint.checkers import BaseChecker | 22 from pylint.checkers import BaseChecker |
24 from pylint.checkers.utils import check_messages | 23 from pylint.checkers.utils import check_messages |
25 | 24 |
26 MSGS = { | 25 MSGS = { |
27 'E1001': ('Use of __slots__ on an old style class', | 26 'E1001': ('Use of __slots__ on an old style class', |
28 'slots-on-old-class', | 27 'Used when an old style class uses the __slots__ attribute.'), |
29 'Used when an old style class uses the __slots__ attribute.', | |
30 {'maxversion': (3, 0)}), | |
31 'E1002': ('Use of super on an old style class', | 28 'E1002': ('Use of super on an old style class', |
32 'super-on-old-class', | 29 'Used when an old style class uses the super builtin.'), |
33 'Used when an old style class uses the super builtin.', | 30 'E1003': ('Bad first argument %r given to super class', |
34 {'maxversion': (3, 0)}), | |
35 'E1003': ('Bad first argument %r given to super()', | |
36 'bad-super-call', | |
37 'Used when another argument than the current class is given as \ | 31 'Used when another argument than the current class is given as \ |
38 first argument of the super builtin.'), | 32 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)}), | |
44 'W1001': ('Use of "property" on an old style class', | 33 'W1001': ('Use of "property" on an old style class', |
45 'property-on-old-class', | |
46 'Used when PyLint detect the use of the builtin "property" \ | 34 'Used when PyLint detect the use of the builtin "property" \ |
47 on an old style class while this is relying on new style \ | 35 on an old style class while this is relying on new style \ |
48 classes features.', | 36 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)}) | |
55 } | 37 } |
56 | 38 |
57 | 39 |
58 class NewStyleConflictChecker(BaseChecker): | 40 class NewStyleConflictChecker(BaseChecker): |
59 """checks for usage of new style capabilities on old style classes and | 41 """checks for usage of new style capabilities on old style classes and |
60 other new/old styles conflicts problems | 42 other new/old styles conflicts problems |
61 * use of property, __slots__, super | 43 * use of property, __slots__, super |
62 * "super" usage | 44 * "super" usage |
63 """ | 45 """ |
64 | 46 |
65 __implements__ = (IAstroidChecker,) | 47 __implements__ = (IASTNGChecker,) |
66 | 48 |
67 # configuration section name | 49 # configuration section name |
68 name = 'newstyle' | 50 name = 'newstyle' |
69 # messages | 51 # messages |
70 msgs = MSGS | 52 msgs = MSGS |
71 priority = -2 | 53 priority = -2 |
72 # configuration options | 54 # configuration options |
73 options = () | 55 options = () |
74 | 56 |
75 @check_messages('slots-on-old-class', 'old-style-class') | 57 @check_messages('E1001') |
76 def visit_class(self, node): | 58 def visit_class(self, node): |
77 """check __slots__ usage | 59 """check __slots__ usage |
78 """ | 60 """ |
79 if '__slots__' in node and not node.newstyle: | 61 if '__slots__' in node and not node.newstyle: |
80 self.add_message('slots-on-old-class', node=node) | 62 self.add_message('E1001', 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) | |
86 | 63 |
87 @check_messages('property-on-old-class') | 64 @check_messages('W1001') |
88 def visit_callfunc(self, node): | 65 def visit_callfunc(self, node): |
89 """check property usage""" | 66 """check property usage""" |
90 parent = node.parent.frame() | 67 parent = node.parent.frame() |
91 if (isinstance(parent, astroid.Class) and | 68 if (isinstance(parent, astng.Class) and |
92 not parent.newstyle and | 69 not parent.newstyle and |
93 isinstance(node.func, astroid.Name)): | 70 isinstance(node.func, astng.Name)): |
94 name = node.func.name | 71 name = node.func.name |
95 if name == 'property': | 72 if name == 'property': |
96 self.add_message('property-on-old-class', node=node) | 73 self.add_message('W1001', node=node) |
97 | 74 |
98 @check_messages('super-on-old-class', 'bad-super-call', 'missing-super-argum
ent') | 75 @check_messages('E1002', 'E1003') |
99 def visit_function(self, node): | 76 def visit_function(self, node): |
100 """check use of super""" | 77 """check use of super""" |
101 # ignore actual functions or method within a new style class | 78 # ignore actual functions or method within a new style class |
102 if not node.is_method(): | 79 if not node.is_method(): |
103 return | 80 return |
104 klass = node.parent.frame() | 81 klass = node.parent.frame() |
105 for stmt in node.nodes_of_class(astroid.CallFunc): | 82 for stmt in node.nodes_of_class(astng.CallFunc): |
106 expr = stmt.func | 83 expr = stmt.func |
107 if not isinstance(expr, astroid.Getattr): | 84 if not isinstance(expr, astng.Getattr): |
108 continue | 85 continue |
109 call = expr.expr | 86 call = expr.expr |
110 # skip the test if using super | 87 # skip the test if using super |
111 if isinstance(call, astroid.CallFunc) and \ | 88 if isinstance(call, astng.CallFunc) and \ |
112 isinstance(call.func, astroid.Name) and \ | 89 isinstance(call.func, astng.Name) and \ |
113 call.func.name == 'super': | 90 call.func.name == 'super': |
114 if not klass.newstyle: | 91 if not klass.newstyle: |
115 # super should not be used on an old style class | 92 # super should not be used on an old style class |
116 self.add_message('super-on-old-class', node=node) | 93 self.add_message('E1002', node=node) |
117 else: | 94 else: |
118 # super first arg should be the class | 95 # 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 | |
123 try: | 96 try: |
124 supcls = (call.args and call.args[0].infer().next() | 97 supcls = (call.args and call.args[0].infer().next() |
125 or None) | 98 or None) |
126 except astroid.InferenceError: | 99 except astng.InferenceError: |
127 continue | 100 continue |
128 | |
129 if supcls is None: | |
130 self.add_message('missing-super-argument', node=call) | |
131 continue | |
132 | |
133 if klass is not supcls: | 101 if klass is not supcls: |
134 name = None | 102 supcls = getattr(supcls, 'name', supcls) |
135 # if supcls is not YES, then supcls was infered | 103 self.add_message('E1003', node=node, args=supcls) |
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, )) | |
147 | 104 |
148 | 105 |
149 def register(linter): | 106 def register(linter): |
150 """required method to auto register this checker """ | 107 """required method to auto register this checker """ |
151 linter.register_checker(NewStyleConflictChecker(linter)) | 108 linter.register_checker(NewStyleConflictChecker(linter)) |
OLD | NEW |