diff options
| author | David Douard <david.douard@logilab.fr> | 2013-06-17 15:06:48 +0200 |
|---|---|---|
| committer | David Douard <david.douard@logilab.fr> | 2013-06-17 15:06:48 +0200 |
| commit | f1786ae74b5f22913c00e80deaacd7870f5e3d7e (patch) | |
| tree | b113d8e035aaca7562ea64384955ec6d806841ef /checkers | |
| parent | d463a2b7bd376ed11d270903760b6a9ac762ef21 (diff) | |
| download | pylint-git-f1786ae74b5f22913c00e80deaacd7870f5e3d7e.tar.gz | |
astng has been renamed astroid
Diffstat (limited to 'checkers')
| -rw-r--r-- | checkers/__init__.py | 4 | ||||
| -rw-r--r-- | checkers/base.py | 92 | ||||
| -rw-r--r-- | checkers/classes.py | 68 | ||||
| -rw-r--r-- | checkers/design_analysis.py | 6 | ||||
| -rw-r--r-- | checkers/exceptions.py | 38 | ||||
| -rw-r--r-- | checkers/format.py | 6 | ||||
| -rw-r--r-- | checkers/imports.py | 16 | ||||
| -rw-r--r-- | checkers/logging.py | 22 | ||||
| -rw-r--r-- | checkers/misc.py | 2 | ||||
| -rw-r--r-- | checkers/newstyle.py | 20 | ||||
| -rw-r--r-- | checkers/similar.py | 2 | ||||
| -rw-r--r-- | checkers/strings.py | 32 | ||||
| -rw-r--r-- | checkers/typecheck.py | 48 | ||||
| -rw-r--r-- | checkers/utils.py | 68 | ||||
| -rw-r--r-- | checkers/variables.py | 60 |
15 files changed, 242 insertions, 242 deletions
diff --git a/checkers/__init__.py b/checkers/__init__.py index dd868c655..cc95ea77a 100644 --- a/checkers/__init__.py +++ b/checkers/__init__.py @@ -43,7 +43,7 @@ import warnings from os import listdir from os.path import dirname, join, isdir, splitext -from logilab.astng.utils import ASTWalker +from astroid.utils import ASTWalker from logilab.common.modutils import load_module_from_file from logilab.common.configuration import OptionsProviderMixIn @@ -126,7 +126,7 @@ class BaseRawChecker(BaseChecker): "use the ITokenChecker interface.", DeprecationWarning) stream = node.file_stream - stream.seek(0) # XXX may be removed with astng > 0.23 + stream.seek(0) # XXX may be removed with astroid > 0.23 self.process_tokens(tokenize.generate_tokens(stream.readline)) def process_tokens(self, tokens): diff --git a/checkers/base.py b/checkers/base.py index 92fc978e7..30ef43486 100644 --- a/checkers/base.py +++ b/checkers/base.py @@ -16,11 +16,11 @@ """basic checker for Python code""" -from logilab import astng +import astroid from logilab.common.ureports import Table -from logilab.astng import are_exclusive +from astroid import are_exclusive -from pylint.interfaces import IASTNGChecker +from pylint.interfaces import IAstroidChecker from pylint.reporters import diff_string from pylint.checkers import BaseChecker, EmptyReport from pylint.checkers.utils import ( @@ -48,8 +48,8 @@ def in_loop(node): """return True if the node is inside a kind of for loop""" parent = node.parent while parent is not None: - if isinstance(parent, (astng.For, astng.ListComp, astng.SetComp, - astng.DictComp, astng.GenExpr)): + if isinstance(parent, (astroid.For, astroid.ListComp, astroid.SetComp, + astroid.DictComp, astroid.GenExpr)): return True parent = parent.parent return False @@ -68,13 +68,13 @@ def in_nested_list(nested_list, obj): def _loop_exits_early(loop): """Returns true if a loop has a break statement in its body.""" - loop_nodes = (astng.For, astng.While) + loop_nodes = (astroid.For, astroid.While) # Loop over body explicitly to avoid matching break statements # in orelse. for child in loop.body: if isinstance(child, loop_nodes): continue - for _ in child.nodes_of_class(astng.Break, skip_klass=loop_nodes): + for _ in child.nodes_of_class(astroid.Break, skip_klass=loop_nodes): return True return False @@ -130,13 +130,13 @@ def redefined_by_decorator(node): """ if node.decorators: for decorator in node.decorators.nodes: - if (isinstance(decorator, astng.Getattr) and + if (isinstance(decorator, astroid.Getattr) and getattr(decorator.expr, 'name', None) == node.name): return True return False class _BasicChecker(BaseChecker): - __implements__ = IASTNGChecker + __implements__ = IAstroidChecker name = 'basic' class BasicErrorChecker(_BasicChecker): @@ -196,21 +196,21 @@ class BasicErrorChecker(_BasicChecker): if not redefined_by_decorator(node): self._check_redefinition(node.is_method() and 'method' or 'function', node) # checks for max returns, branch, return in __init__ - returns = node.nodes_of_class(astng.Return, - skip_klass=(astng.Function, astng.Class)) + returns = node.nodes_of_class(astroid.Return, + skip_klass=(astroid.Function, astroid.Class)) if node.is_method() and node.name == '__init__': if node.is_generator(): self.add_message('E0100', node=node) else: values = [r.value for r in returns] if [v for v in values if not (v is None or - (isinstance(v, astng.Const) and v.value is None) - or (isinstance(v, astng.Name) and v.name == 'None'))]: + (isinstance(v, astroid.Const) and v.value is None) + or (isinstance(v, astroid.Name) and v.name == 'None'))]: self.add_message('E0101', node=node) elif node.is_generator(): # make sure we don't mix non-None returns and yields for retnode in returns: - if isinstance(retnode.value, astng.Const) and \ + if isinstance(retnode.value, astroid.Const) and \ retnode.value.value is not None: self.add_message('E0106', node=node, line=retnode.fromlineno) @@ -224,12 +224,12 @@ class BasicErrorChecker(_BasicChecker): @check_messages('E0104') def visit_return(self, node): - if not isinstance(node.frame(), astng.Function): + if not isinstance(node.frame(), astroid.Function): self.add_message('E0104', node=node) @check_messages('E0105') def visit_yield(self, node): - if not isinstance(node.frame(), (astng.Function, astng.Lambda)): + if not isinstance(node.frame(), (astroid.Function, astroid.Lambda)): self.add_message('E0105', node=node) @check_messages('E0103') @@ -252,7 +252,7 @@ class BasicErrorChecker(_BasicChecker): def visit_unaryop(self, node): """check use of the non-existent ++ adn -- operator operator""" if ((node.op in '+-') and - isinstance(node.operand, astng.UnaryOp) and + isinstance(node.operand, astroid.UnaryOp) and (node.operand.op == node.op)): self.add_message('E0107', node=node, args=node.op*2) @@ -269,7 +269,7 @@ class BasicErrorChecker(_BasicChecker): """check that a node is inside a for or while loop""" _node = node.parent while _node: - if isinstance(_node, (astng.For, astng.While)): + if isinstance(_node, (astroid.For, astroid.While)): break _node = _node.parent else: @@ -296,7 +296,7 @@ functions, methods * uses of the global statement """ - __implements__ = IASTNGChecker + __implements__ = IAstroidChecker name = 'basic' msgs = { @@ -411,7 +411,7 @@ functions, methods def visit_discard(self, node): """check for various kind of statements without effect""" expr = node.value - if isinstance(expr, astng.Const) and isinstance(expr.value, + if isinstance(expr, astroid.Const) and isinstance(expr.value, basestring): # treat string statement in a separated message self.add_message('W0105', node=node) @@ -422,11 +422,11 @@ functions, methods # * a yield (which are wrapped by a discard node in _ast XXX) # warn W0106 if we have any underlying function call (we can't predict # side effects), else W0104 - if (isinstance(expr, (astng.Yield, astng.CallFunc)) or - (isinstance(node.parent, astng.TryExcept) and + if (isinstance(expr, (astroid.Yield, astroid.CallFunc)) or + (isinstance(node.parent, astroid.TryExcept) and node.parent.body == [node])): return - if any(expr.nodes_of_class(astng.CallFunc)): + if any(expr.nodes_of_class(astroid.CallFunc)): self.add_message('W0106', node=node, args=expr.as_string()) else: self.add_message('W0104', node=node) @@ -446,11 +446,11 @@ functions, methods # of the lambda. return call = node.body - if not isinstance(call, astng.CallFunc): + if not isinstance(call, astroid.CallFunc): # The body of the lambda must be a function call expression # for the lambda to be unnecessary. return - # XXX are lambda still different with astng >= 0.18 ? + # XXX are lambda still different with astroid >= 0.18 ? # *args and **kwargs need to be treated specially, since they # are structured differently between the lambda and the function # call (in the lambda they appear in the args.args list and are @@ -460,14 +460,14 @@ functions, methods ordinary_args = list(node.args.args) if node.args.kwarg: if (not call.kwargs - or not isinstance(call.kwargs, astng.Name) + or not isinstance(call.kwargs, astroid.Name) or node.args.kwarg != call.kwargs.name): return elif call.kwargs: return if node.args.vararg: if (not call.starargs - or not isinstance(call.starargs, astng.Name) + or not isinstance(call.starargs, astroid.Name) or node.args.vararg != call.starargs.name): return elif call.starargs: @@ -477,7 +477,7 @@ functions, methods if len(ordinary_args) != len(call.args): return for i in xrange(len(ordinary_args)): - if not isinstance(call.args[i], astng.Name): + if not isinstance(call.args[i], astroid.Name): return if node.args.args[i].name != call.args[i].name: return @@ -492,13 +492,13 @@ functions, methods for default in node.args.defaults: try: value = default.infer().next() - except astng.InferenceError: + except astroid.InferenceError: continue - if (isinstance(value, astng.Instance) and + if (isinstance(value, astroid.Instance) and value.qname() in ('__builtin__.set', '__builtin__.dict', '__builtin__.list')): if value is default: msg = default.as_string() - elif type(value) is astng.Instance: + elif type(value) is astroid.Instance: msg = '%s (%s)' % (default.as_string(), value.qname()) else: msg = '%s (%s)' % (default.as_string(), value.as_string()) @@ -513,7 +513,7 @@ functions, methods """ self._check_unreachable(node) # Is it inside final body of a try...finally bloc ? - self._check_not_in_finally(node, 'return', (astng.Function,)) + self._check_not_in_finally(node, 'return', (astroid.Function,)) @check_messages('W0101') def visit_continue(self, node): @@ -532,7 +532,7 @@ functions, methods # 1 - Is it right sibling ? self._check_unreachable(node) # 2 - Is it inside final body of a try...finally bloc ? - self._check_not_in_finally(node, 'break', (astng.For, astng.While,)) + self._check_not_in_finally(node, 'break', (astroid.For, astroid.While,)) @check_messages('W0101') def visit_raise(self, node): @@ -551,7 +551,7 @@ functions, methods """visit a CallFunc node -> check if this is not a blacklisted builtin call and check for * or ** use """ - if isinstance(node.func, astng.Name): + if isinstance(node.func, astroid.Name): name = node.func.name # ignore the name if it's not a builtin (i.e. not defined in the # locals nor globals scope) @@ -561,7 +561,7 @@ functions, methods self.add_message('W0141', node=node, args=name) if node.starargs or node.kwargs: scope = node.scope() - if isinstance(scope, astng.Function): + if isinstance(scope, astroid.Function): toprocess = [(n, vn) for (n, vn) in ((node.starargs, scope.args.vararg), (node.kwargs, scope.args.kwarg)) if n] if toprocess: @@ -575,7 +575,7 @@ functions, methods @check_messages('W0199') def visit_assert(self, node): """check the use of an assert statement on a tuple.""" - if node.fail is None and isinstance(node.test, astng.Tuple) and \ + if node.fail is None and isinstance(node.test, astroid.Tuple) and \ len(node.test.elts) == 2: self.add_message('W0199', node=node) @@ -584,7 +584,7 @@ functions, methods """check duplicate key in dictionary""" keys = set() for k, _ in node.items: - if isinstance(k, astng.Const): + if isinstance(k, astroid.Const): key = k.value if key in keys: self.add_message('W0109', node=node, args=key) @@ -738,14 +738,14 @@ class NameChecker(_BasicChecker): """check module level assigned names""" frame = node.frame() ass_type = node.ass_type() - if isinstance(ass_type, (astng.Comprehension, astng.Comprehension)): + if isinstance(ass_type, (astroid.Comprehension, astroid.Comprehension)): self._check_name('inlinevar', node.name, node) - elif isinstance(frame, astng.Module): - if isinstance(ass_type, astng.Assign) and not in_loop(ass_type): + elif isinstance(frame, astroid.Module): + if isinstance(ass_type, astroid.Assign) and not in_loop(ass_type): self._check_name('const', node.name, node) - elif isinstance(ass_type, astng.ExceptHandler): + elif isinstance(ass_type, astroid.ExceptHandler): self._check_name('variable', node.name, node) - elif isinstance(frame, astng.Function): + elif isinstance(frame, astroid.Function): # global introduced variable aren't in the function locals if node.name in frame: self._check_name('variable', node.name, node) @@ -753,7 +753,7 @@ class NameChecker(_BasicChecker): def _recursive_check_names(self, args, node): """check names in a possibly recursive list <arg>""" for arg in args: - if isinstance(arg, astng.AssName): + if isinstance(arg, astroid.AssName): self._check_name('argument', arg.name, node) else: self._recursive_check_names(arg.elts, node) @@ -817,12 +817,12 @@ class DocStringChecker(_BasicChecker): def visit_function(self, node): if self.config.no_docstring_rgx.match(node.name) is None: ftype = node.is_method() and 'method' or 'function' - if isinstance(node.parent.frame(), astng.Class): + if isinstance(node.parent.frame(), astroid.Class): overridden = False # check if node is from a method overridden by its ancestor for ancestor in node.parent.frame().ancestors(): if node.name in ancestor and \ - isinstance(ancestor[node.name], astng.Function): + isinstance(ancestor[node.name], astroid.Function): overridden = True break if not overridden: @@ -875,7 +875,7 @@ class LambdaForComprehensionChecker(_BasicChecker): """ if not node.args: return - if not isinstance(node.args[0], astng.Lambda): + if not isinstance(node.args[0], astroid.Lambda): return infered = safe_infer(node.func) if (infered diff --git a/checkers/classes.py b/checkers/classes.py index f2c2913a0..98a2f27df 100644 --- a/checkers/classes.py +++ b/checkers/classes.py @@ -17,10 +17,10 @@ """ from __future__ import generators -from logilab import astng -from logilab.astng import YES, Instance, are_exclusive, AssAttr +import astroid +from astroid import YES, Instance, are_exclusive, AssAttr -from pylint.interfaces import IASTNGChecker +from pylint.interfaces import IAstroidChecker from pylint.checkers import BaseChecker from pylint.checkers.utils import (PYMETHODS, overrides_a_method, check_messages, is_attr_private, is_attr_protected, node_frame_class) @@ -156,7 +156,7 @@ class ClassChecker(BaseChecker): * unreachable code """ - __implements__ = (IASTNGChecker,) + __implements__ = (IAstroidChecker,) # configuration section name name = 'classes' @@ -222,7 +222,7 @@ a metaclass class method.'} if node.type == 'class': try: node.local_attr('__init__') - except astng.NotFoundError: + except astroid.NotFoundError: self.add_message('W0232', args=node, node=node) @check_messages('E0203', 'W0201') @@ -241,7 +241,7 @@ a metaclass class method.'} defining_methods = self.config.defining_attr_methods for attr, nodes in cnode.instance_attrs.iteritems(): nodes = [n for n in nodes if not - isinstance(n.statement(), (astng.Delete, astng.AugAssign))] + isinstance(n.statement(), (astroid.Delete, astroid.AugAssign))] if not nodes: continue # error detected by typechecking attr_defined = False @@ -264,7 +264,7 @@ a metaclass class method.'} # check attribute is defined as a class attribute try: cnode.local_attr(attr) - except astng.NotFoundError: + except astroid.NotFoundError: self.add_message('W0201', args=attr, node=node) def visit_function(self, node): @@ -281,25 +281,25 @@ a metaclass class method.'} return # check signature if the method overloads inherited method for overridden in klass.local_attr_ancestors(node.name): - # get astng for the searched method + # get astroid for the searched method try: meth_node = overridden[node.name] except KeyError: # we have found the method but it's not in the local # dictionary. - # This may happen with astng build from living objects + # This may happen with astroid build from living objects continue - if not isinstance(meth_node, astng.Function): + if not isinstance(meth_node, astroid.Function): continue self._check_signature(node, meth_node, 'overridden') break if node.decorators: for decorator in node.decorators.nodes: - if isinstance(decorator, astng.Getattr) and \ + if isinstance(decorator, astroid.Getattr) and \ decorator.attrname in ('getter', 'setter', 'deleter'): # attribute affectation will call this method, not hiding it return - if isinstance(decorator, astng.Name) and decorator.name == 'property': + if isinstance(decorator, astroid.Name) and decorator.name == 'property': # attribute affectation will either call a setter or raise # an attribute error, anyway not hiding the function return @@ -308,7 +308,7 @@ a metaclass class method.'} overridden = klass.instance_attr(node.name)[0] # XXX args = (overridden.root().name, overridden.fromlineno) self.add_message('E0202', args=args, node=node) - except astng.NotFoundError: + except astroid.NotFoundError: pass def leave_function(self, node): @@ -387,8 +387,8 @@ a metaclass class method.'} return # If the expression begins with a call to super, that's ok. - if isinstance(node.expr, astng.CallFunc) and \ - isinstance(node.expr.func, astng.Name) and \ + if isinstance(node.expr, astroid.CallFunc) and \ + isinstance(node.expr.func, astroid.Name) and \ node.expr.func.name == 'super': return @@ -416,7 +416,7 @@ a metaclass class method.'} node.local_attr(attr) # yes, stop here continue - except astng.NotFoundError: + except astroid.NotFoundError: pass # is it an instance attribute of a parent class ? try: @@ -428,7 +428,7 @@ a metaclass class method.'} # is it an instance attribute ? try: defstmts = node.instance_attr(attr) - except astng.NotFoundError: + except astroid.NotFoundError: pass else: if len(defstmts) == 1: @@ -530,7 +530,7 @@ a metaclass class method.'} e0221_hack = [False] def iface_handler(obj): """filter interface objects, it should be classes""" - if not isinstance(obj, astng.Class): + if not isinstance(obj, astroid.Class): e0221_hack[0] = True self.add_message('E0221', node=node, args=(obj.as_string(),)) @@ -545,10 +545,10 @@ a metaclass class method.'} # don't check method beginning with an underscore, # usually belonging to the interface implementation continue - # get class method astng + # get class method astroid try: method = node_method(node, name) - except astng.NotFoundError: + except astroid.NotFoundError: self.add_message('E0222', args=(name, iface.name), node=node) continue @@ -558,12 +558,12 @@ a metaclass class method.'} # check signature self._check_signature(method, imethod, '%s interface' % iface.name) - except astng.InferenceError: + except astroid.InferenceError: if e0221_hack[0]: return implements = Instance(node).getattr('__implements__')[0] assignment = implements.parent - assert isinstance(assignment, astng.Assign) + assert isinstance(assignment, astroid.Assign) # assignment.expr can be a Name or a Tuple or whatever. # Use as_string() for the message # FIXME: in case of multiple interfaces, find which one could not @@ -580,14 +580,14 @@ a metaclass class method.'} klass_node = node.parent.frame() to_call = _ancestors_to_call(klass_node) not_called_yet = dict(to_call) - for stmt in node.nodes_of_class(astng.CallFunc): + for stmt in node.nodes_of_class(astroid.CallFunc): expr = stmt.func - if not isinstance(expr, astng.Getattr) \ + if not isinstance(expr, astroid.Getattr) \ or expr.attrname != '__init__': continue # skip the test if using super - if isinstance(expr.expr, astng.CallFunc) and \ - isinstance(expr.expr.func, astng.Name) and \ + if isinstance(expr.expr, astroid.CallFunc) and \ + isinstance(expr.expr.func, astroid.Name) and \ expr.expr.func.name == 'super': return try: @@ -599,7 +599,7 @@ a metaclass class method.'} except KeyError: if klass not in to_call: self.add_message('W0233', node=expr, args=klass.name) - except astng.InferenceError: + except astroid.InferenceError: continue for klass, method in not_called_yet.iteritems(): if klass.name == 'object' or method.parent.name == 'object': @@ -611,8 +611,8 @@ a metaclass class method.'} class_type is in 'class', 'interface' """ - if not (isinstance(method1, astng.Function) - and isinstance(refmethod, astng.Function)): + if not (isinstance(method1, astroid.Function) + and isinstance(refmethod, astroid.Function)): self.add_message('F0202', args=(method1, refmethod), node=method1) return # don't care about functions with unknown argument (builtins) @@ -632,7 +632,7 @@ a metaclass class method.'} """Check that attribute lookup name use first attribute variable name (self for method, cls for classmethod and mcs for metaclass). """ - return self._first_attrs and isinstance(node.expr, astng.Name) and \ + return self._first_attrs and isinstance(node.expr, astroid.Name) and \ node.expr.name == self._first_attrs[-1] def _ancestors_to_call(klass_node, method='__init__'): @@ -643,19 +643,19 @@ def _ancestors_to_call(klass_node, method='__init__'): for base_node in klass_node.ancestors(recurs=False): try: to_call[base_node] = base_node.igetattr(method).next() - except astng.InferenceError: + except astroid.InferenceError: continue return to_call def node_method(node, method_name): - """get astng for <method_name> on the given class node, ensuring it + """get astroid for <method_name> on the given class node, ensuring it is a Function node """ for n in node.local_attr(method_name): - if isinstance(n, astng.Function): + if isinstance(n, astroid.Function): return n - raise astng.NotFoundError(method_name) + raise astroid.NotFoundError(method_name) def register(linter): """required method to auto register this checker """ diff --git a/checkers/design_analysis.py b/checkers/design_analysis.py index 2fabfa6e9..2a6b3080b 100644 --- a/checkers/design_analysis.py +++ b/checkers/design_analysis.py @@ -15,9 +15,9 @@ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """check for signs of poor design""" -from logilab.astng import Function, If, InferenceError +from astroid import Function, If, InferenceError -from pylint.interfaces import IASTNGChecker +from pylint.interfaces import IAstroidChecker from pylint.checkers import BaseChecker import re @@ -134,7 +134,7 @@ class MisdesignChecker(BaseChecker): * size, complexity of functions, methods """ - __implements__ = (IASTNGChecker,) + __implements__ = (IAstroidChecker,) # configuration section name name = 'design' diff --git a/checkers/exceptions.py b/checkers/exceptions.py index db0ebcc1b..b681c6d7e 100644 --- a/checkers/exceptions.py +++ b/checkers/exceptions.py @@ -18,12 +18,12 @@ import sys from logilab.common.compat import builtins BUILTINS_NAME = builtins.__name__ -from logilab import astng -from logilab.astng import YES, Instance, unpack_infer +import astroid +from astroid import YES, Instance, unpack_infer from pylint.checkers import BaseChecker from pylint.checkers.utils import is_empty, is_raising -from pylint.interfaces import IASTNGChecker +from pylint.interfaces import IAstroidChecker OVERGENERAL_EXCEPTIONS = ('Exception',) @@ -85,7 +85,7 @@ class ExceptionsChecker(BaseChecker): * type of raise argument : string, Exceptions, other values """ - __implements__ = IASTNGChecker + __implements__ = IAstroidChecker name = 'exceptions' msgs = MSGS @@ -110,7 +110,7 @@ class ExceptionsChecker(BaseChecker): else: try: value = unpack_infer(expr).next() - except astng.InferenceError: + except astroid.InferenceError: return self._check_raise_value(node, value) @@ -118,29 +118,29 @@ class ExceptionsChecker(BaseChecker): """check for bad values, string exception and class inheritance """ value_found = True - if isinstance(expr, astng.Const): + if isinstance(expr, astroid.Const): value = expr.value if isinstance(value, str): self.add_message('W0701', node=node) else: self.add_message('E0702', node=node, args=value.__class__.__name__) - elif (isinstance(expr, astng.Name) and \ + elif (isinstance(expr, astroid.Name) and \ expr.name in ('None', 'True', 'False')) or \ - isinstance(expr, (astng.List, astng.Dict, astng.Tuple, - astng.Module, astng.Function)): + isinstance(expr, (astroid.List, astroid.Dict, astroid.Tuple, + astroid.Module, astroid.Function)): self.add_message('E0702', node=node, args=expr.name) - elif ( (isinstance(expr, astng.Name) and expr.name == 'NotImplemented') - or (isinstance(expr, astng.CallFunc) and - isinstance(expr.func, astng.Name) and + elif ( (isinstance(expr, astroid.Name) and expr.name == 'NotImplemented') + or (isinstance(expr, astroid.CallFunc) and + isinstance(expr.func, astroid.Name) and expr.func.name == 'NotImplemented') ): self.add_message('E0711', node=node) - elif isinstance(expr, astng.BinOp) and expr.op == '%': + elif isinstance(expr, astroid.BinOp) and expr.op == '%': self.add_message('W0701', node=node) - elif isinstance(expr, (Instance, astng.Class)): + elif isinstance(expr, (Instance, astroid.Class)): if isinstance(expr, Instance): expr = expr._proxied - if (isinstance(expr, astng.Class) and + if (isinstance(expr, astroid.Class) and not inherit_from_std_ex(expr) and expr.root().name != BUILTINS_NAME): if expr.newstyle: @@ -171,19 +171,19 @@ class ExceptionsChecker(BaseChecker): msg = 'empty except clause should always appear last' self.add_message('E0701', node=node, args=msg) - elif isinstance(handler.type, astng.BoolOp): + elif isinstance(handler.type, astroid.BoolOp): self.add_message('W0711', node=handler, args=handler.type.op) else: try: excs = list(unpack_infer(handler.type)) - except astng.InferenceError: + except astroid.InferenceError: continue for exc in excs: # XXX skip other non class nodes - if exc is YES or not isinstance(exc, astng.Class): + if exc is YES or not isinstance(exc, astroid.Class): continue exc_ancestors = [anc for anc in exc.ancestors() - if isinstance(anc, astng.Class)] + if isinstance(anc, astroid.Class)] for previous_exc in exceptions_classes: if previous_exc in exc_ancestors: msg = '%s is an ancestor class of %s' % ( diff --git a/checkers/format.py b/checkers/format.py index ea8cf1756..60cc44b6c 100644 --- a/checkers/format.py +++ b/checkers/format.py @@ -27,9 +27,9 @@ if not hasattr(tokenize, 'NL'): raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") from logilab.common.textutils import pretty_match -from logilab.astng import nodes +from astroid import nodes -from pylint.interfaces import ITokenChecker, IASTNGChecker +from pylint.interfaces import ITokenChecker, IAstroidChecker from pylint.checkers import BaseTokenChecker from pylint.checkers.utils import check_messages from pylint.utils import WarningScope @@ -171,7 +171,7 @@ class FormatChecker(BaseTokenChecker): * use of <> instead of != """ - __implements__ = (ITokenChecker, IASTNGChecker) + __implements__ = (ITokenChecker, IAstroidChecker) # configuration section name name = 'format' diff --git a/checkers/imports.py b/checkers/imports.py index fe204b1d4..c289b867a 100644 --- a/checkers/imports.py +++ b/checkers/imports.py @@ -19,10 +19,10 @@ from logilab.common.graph import get_cycles, DotBackend from logilab.common.modutils import is_standard_module from logilab.common.ureports import VerbatimText, Paragraph -from logilab import astng -from logilab.astng import are_exclusive +import astroid +from astroid import are_exclusive -from pylint.interfaces import IASTNGChecker +from pylint.interfaces import IAstroidChecker from pylint.checkers import BaseChecker, EmptyReport @@ -38,11 +38,11 @@ def get_first_import(node, context, name, base, level): continue if first.scope() is node.scope() and first.fromlineno > node.fromlineno: continue - if isinstance(first, astng.Import): + if isinstance(first, astroid.Import): if any(fullname == iname[0] for iname in first.names): found = True break - elif isinstance(first, astng.From): + elif isinstance(first, astroid.From): if level == first.level and any( fullname == '%s.%s' % (first.modname, iname[0]) for iname in first.names): found = True @@ -157,7 +157,7 @@ class ImportsChecker(BaseChecker): * uses of deprecated modules """ - __implements__ = IASTNGChecker + __implements__ = IAstroidChecker name = 'imports' msgs = MSGS @@ -241,7 +241,7 @@ given file (report RP0402 must not be disabled)'} prev = node.previous_sibling() if prev: # consecutive future statements are possible - if not (isinstance(prev, astng.From) + if not (isinstance(prev, astroid.From) and prev.modname == '__future__'): self.add_message('W0410', node=node) return @@ -261,7 +261,7 @@ given file (report RP0402 must not be disabled)'} def get_imported_module(self, modnode, importnode, modname): try: return importnode.do_import_module(modname) - except astng.InferenceError, ex: + except astroid.InferenceError, ex: if str(ex) != modname: args = '%r (%s)' % (modname, ex) else: diff --git a/checkers/logging.py b/checkers/logging.py index 5e36226a0..2cbdedca8 100644 --- a/checkers/logging.py +++ b/checkers/logging.py @@ -14,7 +14,7 @@ """checker for use of Python logging """ -from logilab import astng +import astroid from pylint import checkers from pylint import interfaces from pylint.checkers import utils @@ -57,7 +57,7 @@ CHECKED_CONVENIENCE_FUNCTIONS = set([ class LoggingChecker(checkers.BaseChecker): """Checks use of the logging module.""" - __implements__ = interfaces.IASTNGChecker + __implements__ = interfaces.IAstroidChecker name = 'logging' msgs = MSGS @@ -79,16 +79,16 @@ class LoggingChecker(checkers.BaseChecker): def visit_callfunc(self, node): """Checks calls to (simple forms of) logging methods.""" - if (not isinstance(node.func, astng.Getattr) - or not isinstance(node.func.expr, astng.Name)): + if (not isinstance(node.func, astroid.Getattr) + or not isinstance(node.func.expr, astroid.Name)): return try: logger_class = [inferred for inferred in node.func.expr.infer() if ( - isinstance(inferred, astng.Instance) + isinstance(inferred, astroid.Instance) and any(ancestor for ancestor in inferred._proxied.ancestors() if ( ancestor.name == 'Logger' and ancestor.parent.name == 'logging')))] - except astng.exceptions.InferenceError: + except astroid.exceptions.InferenceError: return if (node.func.expr.name != self._logging_name and not logger_class): return @@ -103,9 +103,9 @@ class LoggingChecker(checkers.BaseChecker): # Either no args, star args, or double-star args. Beyond the # scope of this checker. return - if isinstance(node.args[0], astng.BinOp) and node.args[0].op == '%': + if isinstance(node.args[0], astroid.BinOp) and node.args[0].op == '%': self.add_message('W1201', node=node) - elif isinstance(node.args[0], astng.Const): + elif isinstance(node.args[0], astroid.Const): self._check_format_string(node, 0) def _check_log_methods(self, node): @@ -116,9 +116,9 @@ class LoggingChecker(checkers.BaseChecker): # Either a malformed call, star args, or double-star args. Beyond # the scope of this checker. return - if isinstance(node.args[1], astng.BinOp) and node.args[1].op == '%': + if isinstance(node.args[1], astroid.BinOp) and node.args[1].op == '%': self.add_message('W1201', node=node) - elif isinstance(node.args[1], astng.Const): + elif isinstance(node.args[1], astroid.Const): self._check_format_string(node, 1) def _check_format_string(self, node, format_arg): @@ -171,7 +171,7 @@ class LoggingChecker(checkers.BaseChecker): Returns: Number of AST nodes that aren't keywords. """ - return sum(1 for arg in args if not isinstance(arg, astng.Keyword)) + return sum(1 for arg in args if not isinstance(arg, astroid.Keyword)) def register(linter): diff --git a/checkers/misc.py b/checkers/misc.py index 18d7586ad..9ecdde971 100644 --- a/checkers/misc.py +++ b/checkers/misc.py @@ -56,7 +56,7 @@ separated by a comma.' notes """ stream = node.file_stream - stream.seek(0) # XXX may be removed with astng > 0.23 + stream.seek(0) # XXX may be removed with astroid > 0.23 # warning notes in the code notes = [] for note in self.config.notes: diff --git a/checkers/newstyle.py b/checkers/newstyle.py index edadad88d..a2fca2093 100644 --- a/checkers/newstyle.py +++ b/checkers/newstyle.py @@ -16,9 +16,9 @@ """check for new / old style related problems """ -from logilab import astng +import astroid -from pylint.interfaces import IASTNGChecker +from pylint.interfaces import IAstroidChecker from pylint.checkers import BaseChecker from pylint.checkers.utils import check_messages @@ -48,7 +48,7 @@ class NewStyleConflictChecker(BaseChecker): * "super" usage """ - __implements__ = (IASTNGChecker,) + __implements__ = (IAstroidChecker,) # configuration section name name = 'newstyle' @@ -69,9 +69,9 @@ class NewStyleConflictChecker(BaseChecker): def visit_callfunc(self, node): """check property usage""" parent = node.parent.frame() - if (isinstance(parent, astng.Class) and + if (isinstance(parent, astroid.Class) and not parent.newstyle and - isinstance(node.func, astng.Name)): + isinstance(node.func, astroid.Name)): name = node.func.name if name == 'property': self.add_message('W1001', node=node) @@ -83,14 +83,14 @@ class NewStyleConflictChecker(BaseChecker): if not node.is_method(): return klass = node.parent.frame() - for stmt in node.nodes_of_class(astng.CallFunc): + for stmt in node.nodes_of_class(astroid.CallFunc): expr = stmt.func - if not isinstance(expr, astng.Getattr): + if not isinstance(expr, astroid.Getattr): continue call = expr.expr # skip the test if using super - if isinstance(call, astng.CallFunc) and \ - isinstance(call.func, astng.Name) and \ + if isinstance(call, astroid.CallFunc) and \ + isinstance(call.func, astroid.Name) and \ call.func.name == 'super': if not klass.newstyle: # super should not be used on an old style class @@ -100,7 +100,7 @@ class NewStyleConflictChecker(BaseChecker): try: supcls = (call.args and call.args[0].infer().next() or None) - except astng.InferenceError: + except astroid.InferenceError: continue if klass is not supcls: supcls = getattr(supcls, 'name', supcls) diff --git a/checkers/similar.py b/checkers/similar.py index 6c1b8938f..a0e3db36a 100644 --- a/checkers/similar.py +++ b/checkers/similar.py @@ -38,7 +38,7 @@ class Similar: def append_stream(self, streamid, stream): """append a file to search for similarities""" - stream.seek(0) # XXX may be removed with astng > 0.23 + stream.seek(0) # XXX may be removed with astroid > 0.23 self.linesets.append(LineSet(streamid, stream.readlines(), self.ignore_comments, diff --git a/checkers/strings.py b/checkers/strings.py index 52ff003b0..5bb187aa7 100644 --- a/checkers/strings.py +++ b/checkers/strings.py @@ -21,9 +21,9 @@ import sys import tokenize -from logilab import astng +import astroid -from pylint.interfaces import ITokenChecker, IASTNGChecker +from pylint.interfaces import ITokenChecker, IAstroidChecker from pylint.checkers import BaseChecker, BaseTokenChecker from pylint.checkers import utils @@ -72,16 +72,16 @@ MSGS = { specifiers is given too many arguments"), } -OTHER_NODES = (astng.Const, astng.List, astng.Backquote, - astng.Lambda, astng.Function, - astng.ListComp, astng.SetComp, astng.GenExpr) +OTHER_NODES = (astroid.Const, astroid.List, astroid.Backquote, + astroid.Lambda, astroid.Function, + astroid.ListComp, astroid.SetComp, astroid.GenExpr) class StringFormatChecker(BaseChecker): """Checks string formatting operations to ensure that the format string is valid and the arguments match the format string. """ - __implements__ = (IASTNGChecker,) + __implements__ = (IAstroidChecker,) name = 'string' msgs = MSGS @@ -91,7 +91,7 @@ class StringFormatChecker(BaseChecker): left = node.left args = node.right - if not (isinstance(left, astng.Const) + if not (isinstance(left, astroid.Const) and isinstance(left.value, basestring)): return format_string = left.value @@ -114,11 +114,11 @@ class StringFormatChecker(BaseChecker): # Check that the RHS of the % operator is a mapping object # that contains precisely the set of keys required by the # format string. - if isinstance(args, astng.Dict): + if isinstance(args, astroid.Dict): keys = set() unknown_keys = False for k, _ in args.items: - if isinstance(k, astng.Const): + if isinstance(k, astroid.Const): key = k.value if isinstance(key, basestring): keys.add(key) @@ -137,7 +137,7 @@ class StringFormatChecker(BaseChecker): for key in keys: if key not in required_keys: self.add_message('W1301', node=node, args=key) - elif isinstance(args, OTHER_NODES + (astng.Tuple,)): + elif isinstance(args, OTHER_NODES + (astroid.Tuple,)): type_name = type(args).__name__ self.add_message('E1303', node=node, args=type_name) # else: @@ -149,9 +149,9 @@ class StringFormatChecker(BaseChecker): # Check that the number of arguments passed to the RHS of # the % operator matches the number required by the format # string. - if isinstance(args, astng.Tuple): + if isinstance(args, astroid.Tuple): num_args = len(args.elts) - elif isinstance(args, OTHER_NODES + (astng.Dict, astng.DictComp)): + elif isinstance(args, OTHER_NODES + (astroid.Dict, astroid.DictComp)): num_args = 1 else: # The RHS of the format specifier is a name or @@ -166,7 +166,7 @@ class StringFormatChecker(BaseChecker): class StringMethodsChecker(BaseChecker): - __implements__ = (IASTNGChecker,) + __implements__ = (IAstroidChecker,) name = 'string' msgs = { 'E1310': ("Suspicious argument in %s.%s call", @@ -177,13 +177,13 @@ class StringMethodsChecker(BaseChecker): def visit_callfunc(self, node): func = utils.safe_infer(node.func) - if (isinstance(func, astng.BoundMethod) - and isinstance(func.bound, astng.Instance) + if (isinstance(func, astroid.BoundMethod) + and isinstance(func.bound, astroid.Instance) and func.bound.name in ('str', 'unicode', 'bytes') and func.name in ('strip', 'lstrip', 'rstrip') and node.args): arg = utils.safe_infer(node.args[0]) - if not isinstance(arg, astng.Const): + if not isinstance(arg, astroid.Const): return if len(arg.value) != len(set(arg.value)): self.add_message('E1310', node=node, diff --git a/checkers/typecheck.py b/checkers/typecheck.py index 3dcac24a7..fba00d41c 100644 --- a/checkers/typecheck.py +++ b/checkers/typecheck.py @@ -13,16 +13,16 @@ # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -"""try to find more bugs in the code using astng inference capabilities +"""try to find more bugs in the code using astroid inference capabilities """ import re import shlex -from logilab import astng -from logilab.astng import InferenceError, NotFoundError, YES, Instance +import astroid +from astroid import InferenceError, NotFoundError, YES, Instance -from pylint.interfaces import IASTNGChecker +from pylint.interfaces import IAstroidChecker from pylint.checkers import BaseChecker from pylint.checkers.utils import safe_infer, is_super, check_messages @@ -37,7 +37,7 @@ MSGS = { 'E1103': ('%s %r has no %r member (but some types could not be inferred)', 'maybe-no-member', 'Used when a variable is accessed for an unexistent member, but \ - astng was not able to interpret all possible types of this \ + astroid was not able to interpret all possible types of this \ variable.'), 'E1111': ('Assigning to function call which doesn\'t return', 'assignment-from-no-return', @@ -74,7 +74,7 @@ class TypeChecker(BaseChecker): """try to find bugs in the code using type inference """ - __implements__ = (IASTNGChecker,) + __implements__ = (IAstroidChecker,) # configuration section name name = 'typecheck' @@ -120,7 +120,7 @@ accessed. Python regular expressions are accepted.'} self.generated_members.extend(('REQUEST', 'acl_users', 'aq_parent')) def visit_assattr(self, node): - if isinstance(node.ass_type(), astng.AugAssign): + if isinstance(node.ass_type(), astroid.AugAssign): self.visit_getattr(node) def visit_delattr(self, node): @@ -162,7 +162,7 @@ accessed. Python regular expressions are accepted.'} inference_failure = True continue # skip None anyway - if isinstance(owner, astng.Const) and owner.value is None: + if isinstance(owner, astroid.Const) and owner.value is None: continue # XXX "super" / metaclass call if is_super(owner) or getattr(owner, 'type', None) == 'metaclass': @@ -174,14 +174,14 @@ accessed. Python regular expressions are accepted.'} continue try: if not [n for n in owner.getattr(node.attrname) - if not isinstance(n.statement(), astng.AugAssign)]: + if not isinstance(n.statement(), astroid.AugAssign)]: missingattr.add((owner, name)) continue except AttributeError: # XXX method / function continue except NotFoundError: - if isinstance(owner, astng.Function) and owner.decorators: + if isinstance(owner, astroid.Function) and owner.decorators: continue if isinstance(owner, Instance) and owner.has_dynamic_getattr(): continue @@ -217,23 +217,23 @@ accessed. Python regular expressions are accepted.'} """check that if assigning to a function call, the function is possibly returning something valuable """ - if not isinstance(node.value, astng.CallFunc): + if not isinstance(node.value, astroid.CallFunc): return function_node = safe_infer(node.value.func) # skip class, generator and incomplete function definition - if not (isinstance(function_node, astng.Function) and + if not (isinstance(function_node, astroid.Function) and function_node.root().fully_defined()): return if function_node.is_generator() \ or function_node.is_abstract(pass_is_abstract=False): return - returns = list(function_node.nodes_of_class(astng.Return, - skip_klass=astng.Function)) + returns = list(function_node.nodes_of_class(astroid.Return, + skip_klass=astroid.Function)) if len(returns) == 0: self.add_message('E1111', node=node) else: for rnode in returns: - if not (isinstance(rnode.value, astng.Const) + if not (isinstance(rnode.value, astroid.Const) and rnode.value.value is None): break else: @@ -250,7 +250,7 @@ accessed. Python regular expressions are accepted.'} keyword_args = set() num_positional_args = 0 for arg in node.args: - if isinstance(arg, astng.Keyword): + if isinstance(arg, astroid.Keyword): keyword = arg.arg if keyword in keyword_args: self.add_message('E1122', node=node, args=keyword) @@ -265,18 +265,18 @@ accessed. Python regular expressions are accepted.'} # Note that BoundMethod is a subclass of UnboundMethod (huh?), so must # come first in this 'if..else'. - if isinstance(called, astng.BoundMethod): + if isinstance(called, astroid.BoundMethod): # Bound methods have an extra implicit 'self' argument. num_positional_args += 1 - elif isinstance(called, astng.UnboundMethod): + elif isinstance(called, astroid.UnboundMethod): if called.decorators is not None: for d in called.decorators.nodes: - if isinstance(d, astng.Name) and (d.name == 'classmethod'): + if isinstance(d, astroid.Name) and (d.name == 'classmethod'): # Class methods have an extra implicit 'cls' argument. num_positional_args += 1 break - elif (isinstance(called, astng.Function) or - isinstance(called, astng.Lambda)): + elif (isinstance(called, astroid.Function) or + isinstance(called, astroid.Lambda)): pass else: return @@ -295,15 +295,15 @@ accessed. Python regular expressions are accepted.'} parameters = [] parameter_name_to_index = {} for i, arg in enumerate(called.args.args): - if isinstance(arg, astng.Tuple): + if isinstance(arg, astroid.Tuple): name = None # Don't store any parameter names within the tuple, since those # are not assignable from keyword arguments. else: - if isinstance(arg, astng.Keyword): + if isinstance(arg, astroid.Keyword): name = arg.arg else: - assert isinstance(arg, astng.AssName) + assert isinstance(arg, astroid.AssName) # This occurs with: # def f( (a), (b) ): pass name = arg.name diff --git a/checkers/utils.py b/checkers/utils.py index da09e86a1..022c7077c 100644 --- a/checkers/utils.py +++ b/checkers/utils.py @@ -21,19 +21,19 @@ import re import string -from logilab import astng -from logilab.astng import scoped_nodes +import astroid +from astroid import scoped_nodes from logilab.common.compat import builtins BUILTINS_NAME = builtins.__name__ -COMP_NODE_TYPES = astng.ListComp, astng.SetComp, astng.DictComp, astng.GenExpr +COMP_NODE_TYPES = astroid.ListComp, astroid.SetComp, astroid.DictComp, astroid.GenExpr def is_inside_except(node): """Returns true if node is inside the name of an except handler.""" current = node - while current and not isinstance(current.parent, astng.ExceptHandler): + while current and not isinstance(current.parent, astroid.ExceptHandler): current = current.parent return current and current is current.parent.name @@ -41,7 +41,7 @@ def is_inside_except(node): def get_all_elements(node): """Recursively returns all atoms in nested lists and tuples.""" - if isinstance(node, (astng.Tuple, astng.List)): + if isinstance(node, (astroid.Tuple, astroid.List)): for child in node.elts: for e in get_all_elements(child): yield e @@ -56,9 +56,9 @@ def clobber_in_except(node): Returns (True, args for W0623) if assignment clobbers an existing variable, (False, None) otherwise. """ - if isinstance(node, astng.AssAttr): + if isinstance(node, astroid.AssAttr): return (True, (node.attrname, 'object %r' % (node.expr.name,))) - elif isinstance(node, astng.AssName): + elif isinstance(node, astroid.AssName): name = node.name if is_builtin(name): return (True, (name, 'builtins')) @@ -66,7 +66,7 @@ def clobber_in_except(node): scope, stmts = node.lookup(name) if (stmts and not isinstance(stmts[0].ass_type(), - (astng.Assign, astng.AugAssign, astng.ExceptHandler))): + (astroid.Assign, astroid.AugAssign, astroid.ExceptHandler))): return (True, (name, 'outer scope (line %s)' % (stmts[0].fromlineno,))) return (False, None) @@ -79,12 +79,12 @@ def safe_infer(node): try: inferit = node.infer() value = inferit.next() - except astng.InferenceError: + except astroid.InferenceError: return try: inferit.next() return # None if there is ambiguity on the inferred node - except astng.InferenceError: + except astroid.InferenceError: return # there is some kind of ambiguity except StopIteration: return value @@ -100,20 +100,20 @@ def is_super(node): def is_error(node): """return true if the function does nothing but raising an exception""" for child_node in node.get_children(): - if isinstance(child_node, astng.Raise): + if isinstance(child_node, astroid.Raise): return True return False def is_raising(body): """return true if the given statement node raise an exception""" for node in body: - if isinstance(node, astng.Raise): + if isinstance(node, astroid.Raise): return True return False def is_empty(body): """return true if the given node does nothing but 'pass'""" - return len(body) == 1 and isinstance(body[0], astng.Pass) + return len(body) == 1 and isinstance(body[0], astroid.Pass) builtins = builtins.__dict__.copy() SPECIAL_BUILTINS = ('__builtins__',) # '__path__', '__file__') @@ -136,20 +136,20 @@ def is_defined_before(var_node): _node = var_node.parent while _node: if isinstance(_node, COMP_NODE_TYPES): - for ass_node in _node.nodes_of_class(astng.AssName): + for ass_node in _node.nodes_of_class(astroid.AssName): if ass_node.name == varname: return True - elif isinstance(_node, astng.For): - for ass_node in _node.target.nodes_of_class(astng.AssName): + elif isinstance(_node, astroid.For): + for ass_node in _node.target.nodes_of_class(astroid.AssName): if ass_node.name == varname: return True - elif isinstance(_node, astng.With): + elif isinstance(_node, astroid.With): if _node.vars is None: # quickfix : case in which 'with' is used without 'as' return False if _node.vars.name == varname: return True - elif isinstance(_node, (astng.Lambda, astng.Function)): + elif isinstance(_node, (astroid.Lambda, astroid.Function)): if _node.args.is_argument(varname): return True if getattr(_node, 'name', None) == varname: @@ -161,10 +161,10 @@ def is_defined_before(var_node): _node = stmt.previous_sibling() lineno = stmt.fromlineno while _node and _node.fromlineno == lineno: - for ass_node in _node.nodes_of_class(astng.AssName): + for ass_node in _node.nodes_of_class(astroid.AssName): if ass_node.name == varname: return True - for imp_node in _node.nodes_of_class( (astng.From, astng.Import)): + for imp_node in _node.nodes_of_class( (astroid.From, astroid.Import)): if varname in [name[1] or name[0] for name in imp_node.names]: return True _node = _node.previous_sibling() @@ -175,9 +175,9 @@ def is_func_default(node): value """ parent = node.scope() - if isinstance(parent, astng.Function): + if isinstance(parent, astroid.Function): for default_node in parent.args.defaults: - for default_name_node in default_node.nodes_of_class(astng.Name): + for default_name_node in default_node.nodes_of_class(astroid.Name): if default_name_node is node: return True return False @@ -186,10 +186,10 @@ def is_func_decorator(node): """return true if the name is used in function decorator""" parent = node.parent while parent is not None: - if isinstance(parent, astng.Decorators): + if isinstance(parent, astroid.Decorators): return True if (parent.is_statement or - isinstance(parent, astng.Lambda) or + isinstance(parent, astroid.Lambda) or isinstance(parent, (scoped_nodes.ComprehensionScope, scoped_nodes.ListComp))): break @@ -197,7 +197,7 @@ def is_func_decorator(node): return False def is_ancestor_name(frame, node): - """return True if `frame` is a astng.Class node with `node` in the + """return True if `frame` is a astroid.Class node with `node` in the subtree of its bases attribute """ try: @@ -205,23 +205,23 @@ def is_ancestor_name(frame, node): except AttributeError: return False for base in bases: - if node in base.nodes_of_class(astng.Name): + if node in base.nodes_of_class(astroid.Name): return True return False def assign_parent(node): """return the higher parent which is not an AssName, Tuple or List node """ - while node and isinstance(node, (astng.AssName, - astng.Tuple, - astng.List)): + while node and isinstance(node, (astroid.AssName, + astroid.Tuple, + astroid.List)): node = node.parent return node def overrides_an_abstract_method(class_node, name): """return True if pnode is a parent of node""" for ancestor in class_node.ancestors(): - if name in ancestor and isinstance(ancestor[name], astng.Function) and \ + if name in ancestor and isinstance(ancestor[name], astroid.Function) and \ ancestor[name].is_abstract(pass_is_abstract=False): return True return False @@ -229,7 +229,7 @@ def overrides_an_abstract_method(class_node, name): def overrides_a_method(class_node, name): """return True if <name> is a method overridden from an ancestor""" for ancestor in class_node.ancestors(): - if name in ancestor and isinstance(ancestor[name], astng.Function): + if name in ancestor and isinstance(ancestor[name], astroid.Function): return True return False @@ -352,7 +352,7 @@ def node_frame_class(node): """ klass = node.frame() - while klass is not None and not isinstance(klass, astng.Class): + while klass is not None and not isinstance(klass, astroid.Class): if klass.parent is None: klass = None else: @@ -364,8 +364,8 @@ def is_super_call(expr): """return True if expression node is a function call and if function name is super. Check before that you're in a method. """ - return (isinstance(expr, astng.CallFunc) and - isinstance(expr.func, astng.Name) and + return (isinstance(expr, astroid.CallFunc) and + isinstance(expr.func, astroid.Name) and expr.func.name == 'super') def is_attr_private(attrname): diff --git a/checkers/variables.py b/checkers/variables.py index ff9a1d954..7f012f762 100644 --- a/checkers/variables.py +++ b/checkers/variables.py @@ -19,10 +19,10 @@ import sys from copy import copy -from logilab import astng -from logilab.astng import are_exclusive, builtin_lookup, ASTNGBuildingException +import astroid +from astroid import are_exclusive, builtin_lookup, AstroidBuildingException -from pylint.interfaces import IASTNGChecker +from pylint.interfaces import IAstroidChecker from pylint.checkers import BaseChecker from pylint.checkers.utils import (PYMETHODS, is_ancestor_name, is_builtin, is_defined_before, is_error, is_func_default, is_func_decorator, @@ -32,7 +32,7 @@ from pylint.checkers.utils import (PYMETHODS, is_ancestor_name, is_builtin, def in_for_else_branch(parent, stmt): """Returns True if stmt in inside the else branch for a parent For stmt.""" - return (isinstance(parent, astng.For) and + return (isinstance(parent, astroid.For) and any(else_stmt.parent_of(stmt) for else_stmt in parent.orelse)) def overridden_method(klass, name): @@ -45,9 +45,9 @@ def overridden_method(klass, name): meth_node = parent[name] except KeyError: # We have found an ancestor defining <name> but it's not in the local - # dictionary. This may happen with astng built from living objects. + # dictionary. This may happen with astroid built from living objects. return None - if isinstance(meth_node, astng.Function): + if isinstance(meth_node, astroid.Function): return meth_node return None @@ -129,7 +129,7 @@ class VariablesChecker(BaseChecker): * __all__ consistency """ - __implements__ = IASTNGChecker + __implements__ = IAstroidChecker name = 'variables' msgs = MSGS @@ -175,14 +175,14 @@ builtins. Remember that you should avoid to define new builtins when possible.' # attempt to check for __all__ if defined if '__all__' in node.locals: assigned = node.igetattr('__all__').next() - if assigned is not astng.YES: + if assigned is not astroid.YES: for elt in getattr(assigned, 'elts', ()): try: elt_name = elt.infer().next() - except astng.InferenceError: + except astroid.InferenceError: continue - if not isinstance(elt_name, astng.Const) or not isinstance(elt_name.value, basestring): + if not isinstance(elt_name, astroid.Const) or not isinstance(elt_name.value, basestring): self.add_message('E0604', args=elt.as_string(), node=elt) continue elt_name = elt.value @@ -197,9 +197,9 @@ builtins. Remember that you should avoid to define new builtins when possible.' return for name, stmts in not_consumed.iteritems(): stmt = stmts[0] - if isinstance(stmt, astng.Import): + if isinstance(stmt, astroid.Import): self.add_message('W0611', args=name, node=stmt) - elif isinstance(stmt, astng.From) and stmt.modname != '__future__': + elif isinstance(stmt, astroid.From) and stmt.modname != '__future__': if stmt.names[0][0] == '*': self.add_message('W0614', args=name, node=stmt) else: @@ -271,7 +271,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' for name, stmt in node.items(): if is_inside_except(stmt): continue - if name in globs and not isinstance(stmt, astng.Global): + if name in globs and not isinstance(stmt, astroid.Global): line = globs[name][0].fromlineno self.add_message('W0621', args=(name, line), node=stmt) elif is_builtin(name): @@ -301,7 +301,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' # ignore names imported by the global statement # FIXME: should only ignore them if it's assigned latter stmt = stmts[0] - if isinstance(stmt, astng.Global): + if isinstance(stmt, astroid.Global): continue # care about functions with unknown argument (builtins) if name in argnames: @@ -328,7 +328,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' def visit_global(self, node): """check names imported exists in the global scope""" frame = node.frame() - if isinstance(frame, astng.Module): + if isinstance(frame, astroid.Module): self.add_message('W0604', node=node) return module = frame.root() @@ -336,7 +336,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' for name in node.names: try: assign_nodes = module.getattr(name) - except astng.NotFoundError: + except astroid.NotFoundError: # unassigned global, skip assign_nodes = [] for anode in assign_nodes: @@ -399,7 +399,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' astmts = _astmts if len(astmts) == 1: ass = astmts[0].ass_type() - if isinstance(ass, (astng.For, astng.Comprehension, astng.GenExpr)) \ + if isinstance(ass, (astroid.For, astroid.Comprehension, astroid.GenExpr)) \ and not ass.statement() is node.statement(): self.add_message('W0631', args=name, node=node) @@ -410,7 +410,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' self.add_message('W0623', args=args, node=name) def visit_assname(self, node): - if isinstance(node.ass_type(), astng.AugAssign): + if isinstance(node.ass_type(), astroid.AugAssign): self.visit_name(node) def visit_delname(self, node): @@ -422,7 +422,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' """ stmt = node.statement() if stmt.fromlineno is None: - # name node from a astng built from live code, skip + # name node from a astroid built from live code, skip assert not stmt.root().file.endswith('.py') return name = node.name @@ -481,13 +481,13 @@ builtins. Remember that you should avoid to define new builtins when possible.' and stmt.fromlineno <= defstmt.fromlineno and not is_defined_before(node) and not are_exclusive(stmt, defstmt, ('NameError', 'Exception', 'BaseException'))): - if defstmt is stmt and isinstance(node, (astng.DelName, - astng.AssName)): + if defstmt is stmt and isinstance(node, (astroid.DelName, + astroid.AssName)): self.add_message('E0602', args=name, node=node) elif self._to_consume[-1][-1] != 'lambda': # E0601 may *not* occurs in lambda scope self.add_message('E0601', args=name, node=node) - if not isinstance(node, astng.AssName): # Aug AssName + if not isinstance(node, astroid.AssName): # Aug AssName del to_consume[name] else: del consumed[name] @@ -497,7 +497,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' else: # we have not found the name, if it isn't a builtin, that's an # undefined name ! - if not (name in astng.Module.scope_attrs or is_builtin(name) + if not (name in astroid.Module.scope_attrs or is_builtin(name) or name in self.config.additional_builtins): self.add_message('E0602', args=name, node=node) @@ -508,7 +508,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' parts = name.split('.') try: module = node.infer_name_module(parts[0]).next() - except astng.ResolveError: + except astroid.ResolveError: continue self._check_module_attrs(node, module, parts[1:]) @@ -519,7 +519,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' level = getattr(node, 'level', None) try: module = node.root().import_module(name_parts[0], level=level) - except ASTNGBuildingException: + except AstroidBuildingException: return except Exception, exc: print 'Unhandled exception in VariablesChecker:', exc @@ -537,7 +537,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' given module if the latest access name corresponds to a module, return it """ - assert isinstance(module, astng.Module), module + assert isinstance(module, astroid.Module), module while module_names: name = module_names.pop(0) if name == '__dict__': @@ -545,12 +545,12 @@ builtins. Remember that you should avoid to define new builtins when possible.' break try: module = module.getattr(name)[0].infer().next() - if module is astng.YES: + if module is astroid.YES: return None - except astng.NotFoundError: + except astroid.NotFoundError: self.add_message('E0611', args=(name, module.name), node=node) return None - except astng.InferenceError: + except astroid.InferenceError: return None if module_names: # FIXME: other message if name is not the latest part of @@ -559,7 +559,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' self.add_message('E0611', node=node, args=('.'.join(module_names), modname)) return None - if isinstance(module, astng.Module): + if isinstance(module, astroid.Module): return module return None |
