summaryrefslogtreecommitdiff
path: root/astroid/brain/brain_functools.py
blob: 7fff071bbe03d4f107863b6b09cba8227a4fd697 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Astroid hooks for understanding functools library module."""

import astroid
from astroid.interpreter import util as interpreter_util
from astroid.interpreter import objects
from astroid.interpreter import objectmodel
from astroid.test_utils import extract_node
from astroid import MANAGER


LRU_CACHE = 'functools.lru_cache'


class LruWrappedModel(objectmodel.FunctionModel):
    """Special attribute model for functions decorated with functools.lru_cache.

    The said decorators patches at decoration time some functions onto
    the decorated function.
    """

    @property
    def py__wrapped__(self):
        return self._instance

    @property
    def pycache_info(self):
        cache_info = extract_node('''
        from functools import _CacheInfo
        _CacheInfo(0, 0, 0, 0)
        ''')
        class CacheInfoBoundMethod(objects.BoundMethod):
            def infer_call_result(self, caller, context=None):
                yield interpreter_util.safe_infer(cache_info)

        return CacheInfoBoundMethod(proxy=self._instance, bound=self._instance)

    @property
    def pycache_clear(self):
        node = extract_node('''def cache_clear(): pass''')
        return objects.BoundMethod(proxy=node, bound=self._instance.parent.scope())


class LruWrappedFunctionDef(astroid.FunctionDef):
    special_attributes = LruWrappedModel()


def _transform_lru_cache(node, context=None):
    # TODO: this needs the zipper, because the new node's attributes
    # will still point to the old node.
    new_func = LruWrappedFunctionDef(name=node.name, doc=node.name,
                                     lineno=node.lineno, col_offset=node.col_offset,
                                     parent=node.parent)
    new_func.postinit(node.args, node.body, node.decorators, node.returns)
    return new_func


def _looks_like_lru_cache(node):
    """Check if the given function node is decorated with lru_cache."""
    if not node.decorators:
        return False

    for decorator in node.decorators.nodes:
        if not isinstance(decorator, astroid.Call):
            continue

        func = interpreter_util.safe_infer(decorator.func)
        if func in (None, astroid.Uninferable):
            continue

        if isinstance(func, astroid.FunctionDef) and func.qname() == LRU_CACHE:
            return True
    return False


MANAGER.register_transform(astroid.FunctionDef, _transform_lru_cache,
                           _looks_like_lru_cache)