cubicweb/pylintext.py
author Laura Médioni <laura.medioni@logilab.fr>
Tue, 19 Jan 2016 14:07:59 +0100
changeset 11218 3fea2f3b25c1
parent 11217 1f686f55ef3d
child 11219 0796b6191cea
permissions -rw-r--r--
[pylint] remove abstract-method warning This kind of message is targeted: "Method 'entity_call' is abstract in class 'EntityView' but is not overridden (abstract-method)" For now, only "entity_call" and "render_body" are dealt with, the list can be extended.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11217
1f686f55ef3d [pylint] upgrade pylintext plugin
Laura Médioni <laura.medioni@logilab.fr>
parents: 11057
diff changeset
     1
"""Pylint plugin for cubicweb"""
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
11217
1f686f55ef3d [pylint] upgrade pylintext plugin
Laura Médioni <laura.medioni@logilab.fr>
parents: 11057
diff changeset
     3
from astroid import MANAGER, InferenceError, nodes, scoped_nodes, ClassDef, FunctionDef
9311
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
     4
from astroid.builder import AstroidBuilder
11218
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
     5
from pylint.checkers.utils import unimplemented_abstract_methods, class_is_abstract
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
11217
1f686f55ef3d [pylint] upgrade pylintext plugin
Laura Médioni <laura.medioni@logilab.fr>
parents: 11057
diff changeset
     7
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
def turn_function_to_class(node):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
    """turn a Function node into a Class node (in-place)"""
11217
1f686f55ef3d [pylint] upgrade pylintext plugin
Laura Médioni <laura.medioni@logilab.fr>
parents: 11057
diff changeset
    10
    node.__class__ = ClassDef
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
    node.bases = ()
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
    # remove return nodes so that we don't get warned about 'return outside
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
    # function' by pylint
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
    for rnode in node.nodes_of_class(nodes.Return):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
        rnode.parent.body.remove(rnode)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
    # that seems to be enough :)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
def cubicweb_transform(module):
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8061
diff changeset
    20
    # handle objectify_predicate decorator (and its former name until bw compat
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8061
diff changeset
    21
    # is kept). Only look at module level functions, should be enough.
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9311
diff changeset
    22
    for assnodes in module.locals.values():
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
        for node in assnodes:
11217
1f686f55ef3d [pylint] upgrade pylintext plugin
Laura Médioni <laura.medioni@logilab.fr>
parents: 11057
diff changeset
    24
            if isinstance(node, FunctionDef) and node.decorators:
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
                for decorator in node.decorators.nodes:
9311
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    26
                    try:
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    27
                        for infered in decorator.infer():
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    28
                            if infered.name in ('objectify_predicate', 'objectify_selector'):
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    29
                                turn_function_to_class(node)
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    30
                                break
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    31
                        else:
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    32
                            continue
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    33
                        break
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    34
                    except InferenceError:
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
                        continue
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
    # add yams base types into 'yams.buildobjs', astng doesn't grasp globals()
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
    # magic in there
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
    if module.name == 'yams.buildobjs':
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
        from yams import BASE_TYPES
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
        for etype in BASE_TYPES:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
            module.locals[etype] = [scoped_nodes.Class(etype, None)]
8061
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    42
    # add data() to uiprops module
8198
ce9c6ae03c2d [pylint] correctly detect uiprops module when it's not in a package
David Douard <david.douard@logilab.fr>
parents: 8061
diff changeset
    43
    if module.name.split('.')[-1] == 'uiprops':
9311
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    44
        fake = AstroidBuilder(MANAGER).string_build('''
8061
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    45
def data(string):
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    46
  return u''
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    47
''')
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    48
        module.locals['data'] = fake.locals['data']
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
11217
1f686f55ef3d [pylint] upgrade pylintext plugin
Laura Médioni <laura.medioni@logilab.fr>
parents: 11057
diff changeset
    50
11218
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    51
def cubicweb_abstractmethods_transform(classdef):
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    52
    if class_is_abstract(classdef):
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    53
        return
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    54
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    55
    def is_abstract(method):
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    56
        return method.is_abstract(pass_is_abstract=False)
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    57
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    58
    methods = sorted(
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    59
        unimplemented_abstract_methods(classdef, is_abstract).items(),
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    60
        key=lambda item: item[0],
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    61
    )
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    62
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    63
    def dummy_method():
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    64
        """"""
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    65
    for name, method in methods:
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    66
        owner = method.parent.frame()
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    67
        if owner is classdef:
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    68
            continue
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    69
        if name not in classdef.locals:
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    70
            if name in ('entity_call', 'render_body'):
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    71
                classdef.set_local(name, dummy_method)
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    72
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    73
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    74
def register(linter):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    75
    """called when loaded by pylint --load-plugins, nothing to do here"""
9311
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    76
    MANAGER.register_transform(nodes.Module, cubicweb_transform)
11218
3fea2f3b25c1 [pylint] remove abstract-method warning
Laura Médioni <laura.medioni@logilab.fr>
parents: 11217
diff changeset
    77
    MANAGER.register_transform(nodes.ClassDef, cubicweb_abstractmethods_transform)