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 964aa93
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions astroid/inference_tip.py
Original file line number Diff line number Diff line change
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 global 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 964aa93

Please sign in to comment.