pylintext.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 02 Sep 2015 15:31:18 +0200
changeset 10634 06a43f727601
parent 9311 8833ead6f3e4
child 10663 54b8a1f249fb
permissions -rw-r--r--
[web/views] avoid propagation of NoSelectableObject in some case of inlined relations / permissions When selecting an inlined creation form, we should catch the NoSelectable exception that will be raised if the user cannot add entities of the target type (this is not and cannot be verified earlier) or if some other custom selector prevents the form from being selected. Closes #6510921
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
"""https://pastebin.logilab.fr/show/860/"""
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
9311
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
     3
from astroid import MANAGER, InferenceError, nodes, scoped_nodes
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
     4
from astroid.builder import AstroidBuilder
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
def turn_function_to_class(node):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
    """turn a Function node into a Class node (in-place)"""
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
    node.__class__ = scoped_nodes.Class
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
    node.bases = ()
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
    # 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
    11
    # function' by pylint
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
    for rnode in node.nodes_of_class(nodes.Return):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
        rnode.parent.body.remove(rnode)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
    # that seems to be enough :)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
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
    18
    # 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
    19
    # is kept). Only look at module level functions, should be enough.
8696
0bb18407c053 [toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8216
diff changeset
    20
    for assnodes in module.locals.itervalues():
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
        for node in assnodes:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
            if isinstance(node, scoped_nodes.Function) and node.decorators:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
                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
    24
                    try:
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    25
                        for infered in decorator.infer():
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    26
                            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
    27
                                turn_function_to_class(node)
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    28
                                break
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    29
                        else:
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    30
                            continue
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    31
                        break
8833ead6f3e4 update pylint extension to astroid API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
    32
                    except InferenceError:
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
                        continue
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
    # 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
    35
    # magic in there
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
    if module.name == 'yams.buildobjs':
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
        from yams import BASE_TYPES
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
        for etype in BASE_TYPES:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
            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
    40
    # 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
    41
    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
    42
        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
    43
def data(string):
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    44
  return u''
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    45
''')
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    46
        module.locals['data'] = fake.locals['data']
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
def register(linter):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
    """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
    50
    MANAGER.register_transform(nodes.Module, cubicweb_transform)
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    51