Skip to content

Commit

Permalink
Add a bound to the inference tips cache (pylint-dev#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed May 15, 2022
1 parent ce43cb2 commit e49f433
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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:
_cache.popitem(last=False)
_cache[func, node] = None
result = _cache[func, node] = list(func(*args, **kwargs))
assert result
Expand Down

0 comments on commit e49f433

Please sign in to comment.