summaryrefslogtreecommitdiffstats
path: root/testsuite/ext/exception_messages.py
blob: cd3d7112c6e81b8a7f2d86d096ea5ca49fdcbebd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
try:
    from logilab import astng as ast
    from pylint.interfaces import IASTNGChecker as IChecker
    PYLINT = 0  # pylint 0.something
except ImportError:
    import astroid as ast
    from pylint.interfaces import IAstroidChecker as IChecker
    PYLINT = 1  # pylint 1.something
from pylint.checkers import BaseChecker
from pylint.checkers.utils import safe_infer

if PYLINT == 0:
    # this is not quite correct; later versions of pylint 0.* wanted a
    # three-tuple for messages as well
    msg = ('Exception raised without arguments',
           'Used when an exception is raised without any arguments')
else:
    msg = ('Exception raised without arguments',
           'exception-without-args',
           'Used when an exception is raised without any arguments')
msgs = {'R9901': msg}


class ExceptionMessageChecker(BaseChecker):
    __implements__ = IChecker

    name = 'Exception Messages'
    options = (
        ('exceptions-without-args',
         dict(default=('NotImplementedError',),
              type='csv',
              metavar='<exception names>',
              help='List of exception names that may be raised without arguments')),)
    # this is important so that your checker is executed before others
    priority = -1

    def visit_raise(self, node):
        if node.exc is None:
            return
        if isinstance(node.exc, ast.Name):
            raised = safe_infer(node.exc)
            if (isinstance(raised, ast.Class) and
                raised.name not in self.config.exceptions_without_args):
                self.add_message('R9901', node=node.exc)


def register(linter):
    """required method to auto register this checker"""
    linter.register_checker(ExceptionMessageChecker(linter))