Skip to content

Commit

Permalink
Removed mutable default value in _inference_tip_cache (#1139)
Browse files Browse the repository at this point in the history
* Removed mutable default value in _inference_tip_cache

The mutable default is problematic when astroid is used as a library,
because it effectively becomes a memory leak, see #792.

This commit moves the cache to the global namespace and adds a public
API entry point to clear it.

* Removed the itertools.tee call from _inference_tip_cached

This commit is not expected to affect the behavior, and if anything, should
improve memory usage, because result is only materilized once (before it was
also stored in-full inside itertools.tee).
  • Loading branch information
superbobry committed Aug 27, 2021
1 parent 9f52949 commit 3d0a025
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Expand Up @@ -23,6 +23,8 @@ What's New in astroid 2.7.2?
Release date: 2021-08-20

* ``BaseContainer`` is now public, and will replace ``_BaseContainer`` completely in astroid 3.0.
* The call cache used by inference functions produced by ``inference_tip``
can now be cleared via ``clear_inference_tip_cache``.

* ``astroid.const.BUILTINS`` and ``astroid.bases.BUILTINS`` are not used internally anymore
and will be removed in astroid 3.0. Simply replace this by the string 'builtins' for better
Expand Down
33 changes: 15 additions & 18 deletions astroid/inference_tip.py
Expand Up @@ -3,36 +3,35 @@

"""Transform utilities (filters and decorator)"""

import itertools
import typing

import wrapt

# pylint: disable=dangerous-default-value
from astroid.exceptions import InferenceOverwriteError
from astroid.nodes import NodeNG

InferFn = typing.Callable[..., typing.Any]

_cache: typing.Dict[typing.Tuple[InferFn, NodeNG], typing.Any] = {}


def clear_inference_tip_cache():
"""Clear the inference tips cache."""
_cache.clear()


@wrapt.decorator
def _inference_tip_cached(func, instance, args, kwargs, _cache={}): # noqa:B006
def _inference_tip_cached(func, instance, args, kwargs):
"""Cache decorator used for inference tips"""
node = args[0]
try:
return iter(_cache[func, node])
result = _cache[func, node]
except KeyError:
result = func(*args, **kwargs)
# Need to keep an iterator around
original, copy = itertools.tee(result)
_cache[func, node] = list(copy)
return original


# pylint: enable=dangerous-default-value
result = _cache[func, node] = list(func(*args, **kwargs))
return iter(result)


def inference_tip(
infer_function: typing.Callable, raise_on_overwrite: bool = False
) -> typing.Callable:
def inference_tip(infer_function: InferFn, raise_on_overwrite: bool = False) -> InferFn:
"""Given an instance specific inference function, return a function to be
given to AstroidManager().register_transform to set this inference function.
Expand All @@ -54,9 +53,7 @@ def inference_tip(
excess overwrites.
"""

def transform(
node: NodeNG, infer_function: typing.Callable = infer_function
) -> NodeNG:
def transform(node: NodeNG, infer_function: InferFn = infer_function) -> NodeNG:
if (
raise_on_overwrite
and node._explicit_inference is not None
Expand Down

0 comments on commit 3d0a025

Please sign in to comment.