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