Skip to content

Commit ab20812

Browse files
Clear LRU caches on pylint utilities (#8420)
1 parent 958ff85 commit ab20812

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/whatsnew/fragments/8361.bugfix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``--clear-cache-post-run`` now also clears LRU caches for pylint utilities
2+
holding references to AST nodes.
3+
4+
Closes #8361

pylint/checkers/utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from collections.abc import Iterable, Iterator
1717
from functools import lru_cache, partial
1818
from re import Match
19-
from typing import TYPE_CHECKING, Callable, TypeVar
19+
from typing import TYPE_CHECKING, Any, Callable, TypeVar
2020

2121
import _string
2222
import astroid.objects
@@ -27,6 +27,8 @@
2727
from astroid.typing import InferenceResult, SuccessfulInferenceResult
2828

2929
if TYPE_CHECKING:
30+
from functools import _lru_cache_wrapper
31+
3032
from pylint.checkers import BaseChecker
3133

3234
_NodeT = TypeVar("_NodeT", bound=nodes.NodeNG)
@@ -2243,3 +2245,20 @@ def not_condition_as_string(
22432245
)
22442246
msg = f"{lhs} {get_inverse_comparator(ops)} {rhs}"
22452247
return msg
2248+
2249+
2250+
def clear_lru_caches() -> None:
2251+
"""Clear caches holding references to AST nodes."""
2252+
# pylint: disable-next=import-outside-toplevel
2253+
from pylint.checkers.variables import overridden_method
2254+
2255+
caches_holding_node_references: list[_lru_cache_wrapper[Any]] = [
2256+
in_for_else_branch,
2257+
infer_all,
2258+
is_overload_stub,
2259+
overridden_method,
2260+
unimplemented_abstract_methods,
2261+
safe_infer,
2262+
]
2263+
for lru in caches_holding_node_references:
2264+
lru.cache_clear()

pylint/lint/run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typing import Any, ClassVar
1313

1414
from pylint import config
15+
from pylint.checkers.utils import clear_lru_caches
1516
from pylint.config._pylint_config import (
1617
_handle_pylint_config_commands,
1718
_register_generate_config_options,
@@ -222,6 +223,7 @@ def __init__(
222223
exit = do_exit
223224

224225
if linter.config.clear_cache_post_run:
226+
clear_lru_caches()
225227
MANAGER.clear_cache()
226228

227229
if exit:

0 commit comments

Comments
 (0)