Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a bound to the inference tips cache #1565

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Release date: TBA
* Rename ``ModuleSpec`` -> ``module_type`` constructor parameter to match attribute
name and improve typing. Use ``type`` instead.

* Add a bound to the inference tips cache.

Closes #1150

* Infer the return value of the ``.copy()`` method on ``dict``, ``list``, ``set``,
and ``frozenset``.

Expand Down
6 changes: 5 additions & 1 deletion astroid/inference_tip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

import typing
from collections import OrderedDict
from collections.abc import Iterator

import wrapt
Expand All @@ -20,7 +21,7 @@
NodeNG, bases.Instance, bases.UnboundMethod, typing.Type[util.Uninferable]
]

_cache: dict[tuple[InferFn, NodeNG], list[InferOptions] | None] = {}
_cache: OrderedDict[tuple[InferFn, NodeNG], list[InferOptions] | None] = OrderedDict()


def clear_inference_tip_cache():
Expand All @@ -36,11 +37,14 @@ def _inference_tip_cached(
node = args[0]
try:
result = _cache[func, node]
_cache.move_to_end((func, node))
# If through recursion we end up trying to infer the same
# func + node we raise here.
if result is None:
raise UseInferenceDefault()
except KeyError:
if len(_cache) > 127:
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
_cache.popitem(last=False)
_cache[func, node] = None
result = _cache[func, node] = list(func(*args, **kwargs))
assert result
Expand Down