Skip to content

Commit

Permalink
Removed mutable default value in _inference_tip_cache
Browse files Browse the repository at this point in the history
The mutable default is problematic when astroid is used as a library,
because it effectively becomes a memory leak, see pylint-dev#792.

This commit moves the cache to the global namespace and adds a public
API entry point to clear it.
  • Loading branch information
superbobry committed Aug 19, 2021
1 parent 7603614 commit aad8c8e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Expand Up @@ -13,6 +13,8 @@ What's New in astroid 2.7.2?
Release date: TBA

* ``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``.


What's New in astroid 2.7.1?
Expand Down
20 changes: 13 additions & 7 deletions astroid/inference_tip.py
Expand Up @@ -8,13 +8,21 @@

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:
Expand All @@ -27,12 +35,10 @@ def _inference_tip_cached(func, instance, args, kwargs, _cache={}): # noqa:B006
return original


# pylint: enable=dangerous-default-value


def inference_tip(
infer_function: typing.Callable, raise_on_overwrite: bool = False
) -> typing.Callable:
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 @@ -55,7 +61,7 @@ def inference_tip(
"""

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

0 comments on commit aad8c8e

Please sign in to comment.