Skip to content

Commit 95bbd5e

Browse files
Add a bound to the inference tips cache (#1565)
1 parent 826f6ef commit 95bbd5e

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Release date: TBA
2020
* Rename ``ModuleSpec`` -> ``module_type`` constructor parameter to match attribute
2121
name and improve typing. Use ``type`` instead.
2222

23+
* Add a bound to the inference tips cache.
24+
25+
Closes #1150
26+
2327
* Infer the return value of the ``.copy()`` method on ``dict``, ``list``, ``set``,
2428
and ``frozenset``.
2529

astroid/inference_tip.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
import typing
10+
from collections import OrderedDict
1011
from collections.abc import Iterator
1112

1213
import wrapt
@@ -20,7 +21,7 @@
2021
NodeNG, bases.Instance, bases.UnboundMethod, typing.Type[util.Uninferable]
2122
]
2223

23-
_cache: dict[tuple[InferFn, NodeNG], list[InferOptions] | None] = {}
24+
_cache: OrderedDict[tuple[InferFn, NodeNG], list[InferOptions] | None] = OrderedDict()
2425

2526

2627
def clear_inference_tip_cache():
@@ -36,11 +37,14 @@ def _inference_tip_cached(
3637
node = args[0]
3738
try:
3839
result = _cache[func, node]
40+
_cache.move_to_end((func, node))
3941
# If through recursion we end up trying to infer the same
4042
# func + node we raise here.
4143
if result is None:
4244
raise UseInferenceDefault()
4345
except KeyError:
46+
if len(_cache) > 127:
47+
_cache.popitem(last=False)
4448
_cache[func, node] = None
4549
result = _cache[func, node] = list(func(*args, **kwargs))
4650
assert result

0 commit comments

Comments
 (0)