utils.py 20.1 KB
Newer Older
1 2
# pylint: disable=W0611
#
3
# Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE).
4 5 6 7 8 9 10 11 12 13 14 15 16
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# 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.,
17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19 20
"""some functions that may be useful for various checkers
"""

21 22
import re
import sys
23
import string
24 25 26

import astroid
from astroid import scoped_nodes
27
from logilab.common.compat import builtins
28

29
BUILTINS_NAME = builtins.__name__
30 31
COMP_NODE_TYPES = astroid.ListComp, astroid.SetComp, astroid.DictComp, astroid.GenExpr
PY3K = sys.version_info[0] == 3
32

33 34 35 36
if not PY3K:
    EXCEPTIONS_MODULE = "exceptions"
else:
    EXCEPTIONS_MODULE = "builtins"
37 38
ABC_METHODS = set(('abc.abstractproperty', 'abc.abstractmethod',
                   'abc.abstractclassmethod', 'abc.abstractstaticmethod'))
39

40 41 42

class NoSuchArgumentError(Exception):
    pass
43 44

def is_inside_except(node):
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    """Returns true if node is inside the name of an except handler."""
    current = node
    while current and not isinstance(current.parent, astroid.ExceptHandler):
        current = current.parent

    return current and current is current.parent.name


def get_all_elements(node):
    """Recursively returns all atoms in nested lists and tuples."""
    if isinstance(node, (astroid.Tuple, astroid.List)):
        for child in node.elts:
            for e in get_all_elements(child):
                yield e
    else:
        yield node
61 62 63 64 65 66 67 68 69


def clobber_in_except(node):
    """Checks if an assignment node in an except handler clobbers an existing
    variable.

    Returns (True, args for W0623) if assignment clobbers an existing variable,
    (False, None) otherwise.
    """
70 71 72
    if isinstance(node, astroid.AssAttr):
        return (True, (node.attrname, 'object %r' % (node.expr.as_string(),)))
    elif isinstance(node, astroid.AssName):
73 74 75 76
        name = node.name
        if is_builtin(name):
            return (True, (name, 'builtins'))
        else:
77 78 79 80 81
            stmts = node.lookup(name)[1]
            if (stmts and not isinstance(stmts[0].ass_type(),
                                         (astroid.Assign, astroid.AugAssign,
                                          astroid.ExceptHandler))):
                return (True, (name, 'outer scope (line %s)' % stmts[0].fromlineno))
82 83 84 85 86 87 88 89 90 91
    return (False, None)


def safe_infer(node):
    """return the inferred value for the given node.
    Return None if inference failed or if there is some ambiguity (more than
    one node has been inferred)
    """
    try:
        inferit = node.infer()
92
        value = next(inferit)
93
    except astroid.InferenceError:
94 95
        return
    try:
96
        next(inferit)
97
        return # None if there is ambiguity on the inferred node
98 99
    except astroid.InferenceError:
        return # there is some kind of ambiguity
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    except StopIteration:
        return value

def is_super(node):
    """return True if the node is referencing the "super" builtin function
    """
    if getattr(node, 'name', None) == 'super' and \
           node.root().name == BUILTINS_NAME:
        return True
    return False

def is_error(node):
    """return true if the function does nothing but raising an exception"""
    for child_node in node.get_children():
114
        if isinstance(child_node, astroid.Raise):
115 116 117 118 119 120
            return True
        return False

def is_raising(body):
    """return true if the given statement node raise an exception"""
    for node in body:
121
        if isinstance(node, astroid.Raise):
122 123 124 125 126
            return True
    return False

def is_empty(body):
    """return true if the given node does nothing but 'pass'"""
127
    return len(body) == 1 and isinstance(body[0], astroid.Pass)
128

129
builtins = builtins.__dict__.copy()
130 131
SPECIAL_BUILTINS = ('__builtins__',) # '__path__', '__file__')

132 133 134 135
def is_builtin_object(node):
    """Returns True if the given node is an object from the __builtin__ module."""
    return node and node.root().name == BUILTINS_NAME

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
def is_builtin(name): # was is_native_builtin
    """return true if <name> could be considered as a builtin defined by python
    """
    if name in builtins:
        return True
    if name in SPECIAL_BUILTINS:
        return True
    return False

def is_defined_before(var_node):
    """return True if the variable node is defined by a parent node (list,
    set, dict, or generator comprehension, lambda) or in a previous sibling
    node on the same line (statement_defining ; statement_using)
    """
    varname = var_node.name
    _node = var_node.parent
    while _node:
        if isinstance(_node, COMP_NODE_TYPES):
154
            for ass_node in _node.nodes_of_class(astroid.AssName):
155 156
                if ass_node.name == varname:
                    return True
157 158
        elif isinstance(_node, astroid.For):
            for ass_node in _node.target.nodes_of_class(astroid.AssName):
159 160
                if ass_node.name == varname:
                    return True
161
        elif isinstance(_node, astroid.With):
162
            for expr, ids in _node.items:
163 164
                if expr.parent_of(var_node):
                    break
165 166 167
                if (ids and
                        isinstance(ids, astroid.AssName) and
                        ids.name == varname):
168 169
                    return True
        elif isinstance(_node, (astroid.Lambda, astroid.Function)):
170 171 172 173 174
            if _node.args.is_argument(varname):
                return True
            if getattr(_node, 'name', None) == varname:
                return True
            break
175 176 177 178 179
        elif isinstance(_node, astroid.ExceptHandler):
            if isinstance(_node.name, astroid.AssName):
                ass_node = _node.name
                if ass_node.name == varname:
                    return True
180 181 182 183 184 185
        _node = _node.parent
    # possibly multiple statements on the same line using semi colon separator
    stmt = var_node.statement()
    _node = stmt.previous_sibling()
    lineno = stmt.fromlineno
    while _node and _node.fromlineno == lineno:
186
        for ass_node in _node.nodes_of_class(astroid.AssName):
187 188
            if ass_node.name == varname:
                return True
189
        for imp_node in _node.nodes_of_class((astroid.From, astroid.Import)):
190 191 192 193 194 195 196 197 198 199
            if varname in [name[1] or name[0] for name in imp_node.names]:
                return True
        _node = _node.previous_sibling()
    return False

def is_func_default(node):
    """return true if the given Name node is used in function default argument's
    value
    """
    parent = node.scope()
200
    if isinstance(parent, astroid.Function):
201
        for default_node in parent.args.defaults:
202
            for default_name_node in default_node.nodes_of_class(astroid.Name):
203 204 205 206 207 208 209 210
                if default_name_node is node:
                    return True
    return False

def is_func_decorator(node):
    """return true if the name is used in function decorator"""
    parent = node.parent
    while parent is not None:
211
        if isinstance(parent, astroid.Decorators):
212
            return True
213 214 215 216
        if (parent.is_statement or
                isinstance(parent, astroid.Lambda) or
                isinstance(parent, (scoped_nodes.ComprehensionScope,
                                    scoped_nodes.ListComp))):
217 218 219 220 221
            break
        parent = parent.parent
    return False

def is_ancestor_name(frame, node):
222
    """return True if `frame` is a astroid.Class node with `node` in the
223 224 225 226 227 228 229
    subtree of its bases attribute
    """
    try:
        bases = frame.bases
    except AttributeError:
        return False
    for base in bases:
230
        if node in base.nodes_of_class(astroid.Name):
231 232 233 234 235 236
            return True
    return False

def assign_parent(node):
    """return the higher parent which is not an AssName, Tuple or List node
    """
237 238 239
    while node and isinstance(node, (astroid.AssName,
                                     astroid.Tuple,
                                     astroid.List)):
240 241 242 243 244 245
        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():
246
        if name in ancestor and isinstance(ancestor[name], astroid.Function) and \
247 248 249 250 251 252 253
               ancestor[name].is_abstract(pass_is_abstract=False):
            return True
    return False

def overrides_a_method(class_node, name):
    """return True if <name> is a method overridden from an ancestor"""
    for ancestor in class_node.ancestors():
254
        if name in ancestor and isinstance(ancestor[name], astroid.Function):
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
            return True
    return False

PYMETHODS = set(('__new__', '__init__', '__del__', '__hash__',
                 '__str__', '__repr__',
                 '__len__', '__iter__',
                 '__delete__', '__get__', '__set__',
                 '__getitem__', '__setitem__', '__delitem__', '__contains__',
                 '__getattribute__', '__getattr__', '__setattr__', '__delattr__',
                 '__call__',
                 '__enter__', '__exit__',
                 '__cmp__', '__ge__', '__gt__', '__le__', '__lt__', '__eq__',
                 '__nonzero__', '__neg__', '__invert__',
                 '__mul__', '__imul__', '__rmul__',
                 '__div__', '__idiv__', '__rdiv__',
                 '__add__', '__iadd__', '__radd__',
                 '__sub__', '__isub__', '__rsub__',
                 '__pow__', '__ipow__', '__rpow__',
                 '__mod__', '__imod__', '__rmod__',
                 '__and__', '__iand__', '__rand__',
                 '__or__', '__ior__', '__ror__',
                 '__xor__', '__ixor__', '__rxor__',
                 # XXX To be continued
278
                ))
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

def check_messages(*messages):
    """decorator to store messages that are handled by a checker method"""

    def store_messages(func):
        func.checks_msgs = messages
        return func
    return store_messages

class IncompleteFormatString(Exception):
    """A format string ended in the middle of a format specifier."""
    pass

class UnsupportedFormatCharacter(Exception):
    """A format character in a format string is not one of the supported
    format characters."""
    def __init__(self, index):
        Exception.__init__(self, index)
        self.index = index

def parse_format_string(format_string):
    """Parses a format string, returning a tuple of (keys, num_args), where keys
    is the set of mapping keys in the format string, and num_args is the number
    of arguments required by the format string.  Raises
    IncompleteFormatString or UnsupportedFormatCharacter if a
    parse error occurs."""
    keys = set()
    num_args = 0
    def next_char(i):
        i += 1
        if i == len(format_string):
            raise IncompleteFormatString
        return (i, format_string[i])
    i = 0
    while i < len(format_string):
314 315 316
        char = format_string[i]
        if char == '%':
            i, char = next_char(i)
317 318
            # Parse the mapping key (optional).
            key = None
319
            if char == '(':
320
                depth = 1
321
                i, char = next_char(i)
322 323
                key_start = i
                while depth != 0:
324
                    if char == '(':
325
                        depth += 1
326
                    elif char == ')':
327
                        depth -= 1
328
                    i, char = next_char(i)
329 330 331 332
                key_end = i - 1
                key = format_string[key_start:key_end]

            # Parse the conversion flags (optional).
333 334
            while char in '#0- +':
                i, char = next_char(i)
335
            # Parse the minimum field width (optional).
336
            if char == '*':
337
                num_args += 1
338
                i, char = next_char(i)
339
            else:
340 341
                while char in string.digits:
                    i, char = next_char(i)
342
            # Parse the precision (optional).
343 344 345
            if char == '.':
                i, char = next_char(i)
                if char == '*':
346
                    num_args += 1
347
                    i, char = next_char(i)
348
                else:
349 350
                    while char in string.digits:
                        i, char = next_char(i)
351
            # Parse the length modifier (optional).
352 353
            if char in 'hlL':
                i, char = next_char(i)
354
            # Parse the conversion type (mandatory).
355 356 357 358 359
            if PY3K:
                flags = 'diouxXeEfFgGcrs%a'
            else:
                flags = 'diouxXeEfFgGcrs%'
            if char not in flags:
360 361 362
                raise UnsupportedFormatCharacter(i)
            if key:
                keys.add(key)
363
            elif char != '%':
364 365 366
                num_args += 1
        i += 1
    return keys, num_args
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421


def is_attr_protected(attrname):
    """return True if attribute name is protected (start with _ and some other
    details), False otherwise.
    """
    return attrname[0] == '_' and not attrname == '_' and not (
        attrname.startswith('__') and attrname.endswith('__'))

def node_frame_class(node):
    """return klass node for a method node (or a staticmethod or a
    classmethod), return null otherwise
    """
    klass = node.frame()

    while klass is not None and not isinstance(klass, astroid.Class):
        if klass.parent is None:
            klass = None
        else:
            klass = klass.parent.frame()

    return klass

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, astroid.CallFunc) and
            isinstance(expr.func, astroid.Name) and
            expr.func.name == 'super')

def is_attr_private(attrname):
    """Check that attribute name is private (at least two leading underscores,
    at most one trailing underscore)
    """
    regex = re.compile('^_{2,}.*[^_]+_?$')
    return regex.match(attrname)

def get_argument_from_call(callfunc_node, position=None, keyword=None):
    """Returns the specified argument from a function call.

    :param callfunc_node: Node representing a function call to check.
    :param int position: position of the argument.
    :param str keyword: the keyword of the argument.

    :returns: The node representing the argument, None if the argument is not found.
    :raises ValueError: if both position and keyword are None.
    :raises NoSuchArgumentError: if no argument at the provided position or with
    the provided keyword.
    """
    if position is None and keyword is None:
        raise ValueError('Must specify at least one of: position or keyword.')
    try:
        if position is not None and not isinstance(callfunc_node.args[position], astroid.Keyword):
            return callfunc_node.args[position]
422
    except IndexError as error:
423 424 425 426 427 428
        raise NoSuchArgumentError(error)
    if keyword:
        for arg in callfunc_node.args:
            if isinstance(arg, astroid.Keyword) and arg.arg == keyword:
                return arg.value
    raise NoSuchArgumentError
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

def inherit_from_std_ex(node):
    """
    Return true if the given class node is subclass of
    exceptions.Exception.
    """
    if node.name in ('Exception', 'BaseException') \
            and node.root().name == EXCEPTIONS_MODULE:
        return True
    return any(inherit_from_std_ex(parent)
               for parent in node.ancestors(recurs=False))

def is_import_error(handler):
    """
    Check if the given exception handler catches
    ImportError.

    :param handler: A node, representing an ExceptHandler node.
    :returns: True if the handler catches ImportError, False otherwise.
    """
    names = None
    if isinstance(handler.type, astroid.Tuple):
        names = [name for name in handler.type.elts
                 if isinstance(name, astroid.Name)]
    elif isinstance(handler.type, astroid.Name):
        names = [handler.type]
    else:
        # Don't try to infer that.
        return
    for name in names:
        try:
            for infered in name.infer():
                if (isinstance(infered, astroid.Class) and
                        inherit_from_std_ex(infered) and
                        infered.name == 'ImportError'):
                    return True
        except astroid.InferenceError:
            continue

def has_known_bases(klass):
    """Returns true if all base classes of a class could be inferred."""
    try:
        return klass._all_bases_known
    except AttributeError:
        pass
    for base in klass.bases:
        result = safe_infer(base)
        # TODO: check for A->B->A->B pattern in class structure too?
        if (not isinstance(result, astroid.Class) or
                result is klass or
                not has_known_bases(result)):
            klass._all_bases_known = False
            return False
    klass._all_bases_known = True
    return True

def decorated_with_property(node):
    """ Detect if the given function node is decorated with a property. """
    if not node.decorators:
        return False
    for decorator in node.decorators.nodes:
        if not isinstance(decorator, astroid.Name):
            continue
        try:
            for infered in decorator.infer():
                if isinstance(infered, astroid.Class):
                    if (infered.root().name == BUILTINS_NAME and
                            infered.name == 'property'):
                        return True
                    for ancestor in infered.ancestors():
                        if (ancestor.name == 'property' and
                                ancestor.root().name == BUILTINS_NAME):
                            return True
        except astroid.InferenceError:
            pass
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564


def decorated_with_abc(func):
    """Determine if the `func` node is decorated with `abc` decorators."""
    if func.decorators:
        for node in func.decorators.nodes:
            try:
                infered = next(node.infer())
            except astroid.InferenceError:
                continue
            if infered and infered.qname() in ABC_METHODS:
                return True


def unimplemented_abstract_methods(node, is_abstract_cb=decorated_with_abc):
    """
    Get the unimplemented abstract methods for the given *node*.

    A method can be considered abstract if the callback *is_abstract_cb*
    returns a ``True`` value. The check defaults to verifying that
    a method is decorated with abstract methods.
    The function will work only for new-style classes. For old-style
    classes, it will simply return an empty dictionary.
    For the rest of them, it will return a dictionary of abstract method
    names and their inferred objects.
    """
    visited = {}
    try:
        mro = reversed(node.mro())
    except NotImplementedError:
        # Old style class, it will not have a mro.
        return {}
    except astroid.ResolveError:
        # Probably inconsistent hierarchy, don'try
        # to figure this out here.
        return {}
    for ancestor in mro:
        for obj in ancestor.values():
            infered = obj
            if isinstance(obj, astroid.AssName):
                infered = safe_infer(obj)
                if not infered:
                    continue
                if not isinstance(infered, astroid.Function):
                    if obj.name in visited:
                        del visited[obj.name]
            if isinstance(infered, astroid.Function):
                # It's critical to use the original name,
                # since after inferring, an object can be something
                # else than expected, as in the case of the
                # following assignment.
                #
                # class A:
                #     def keys(self): pass
                #     __iter__ = keys
                abstract = is_abstract_cb(infered)
                if abstract:
                    visited[obj.name] = infered
                elif not abstract and obj.name in visited:
                    del visited[obj.name]
    return visited