Skip to content

Commit 8c3652a

Browse files
Clear LRU caches on pylint utilities (#8420) (#8427)
(cherry picked from commit ab20812) Co-authored-by: Jacob Walls <[email protected]>
1 parent 2e78c41 commit 8c3652a

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/whatsnew/fragments/8361.bugfix

+4
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

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from collections.abc import Iterable, Iterator
1818
from functools import lru_cache, partial
1919
from re import Match
20-
from typing import TYPE_CHECKING, Callable, TypeVar
20+
from typing import TYPE_CHECKING, Any, Callable, TypeVar
2121

2222
import _string
2323
import astroid.objects
@@ -28,6 +28,8 @@
2828
from astroid.typing import InferenceResult, SuccessfulInferenceResult
2929

3030
if TYPE_CHECKING:
31+
from functools import _lru_cache_wrapper
32+
3133
from pylint.checkers import BaseChecker
3234

3335
_NodeT = TypeVar("_NodeT", bound=nodes.NodeNG)
@@ -2295,3 +2297,20 @@ def not_condition_as_string(
22952297
)
22962298
msg = f"{lhs} {get_inverse_comparator(ops)} {rhs}"
22972299
return msg
2300+
2301+
2302+
def clear_lru_caches() -> None:
2303+
"""Clear caches holding references to AST nodes."""
2304+
# pylint: disable-next=import-outside-toplevel
2305+
from pylint.checkers.variables import overridden_method
2306+
2307+
caches_holding_node_references: list[_lru_cache_wrapper[Any]] = [
2308+
in_for_else_branch,
2309+
infer_all,
2310+
is_overload_stub,
2311+
overridden_method,
2312+
unimplemented_abstract_methods,
2313+
safe_infer,
2314+
]
2315+
for lru in caches_holding_node_references:
2316+
lru.cache_clear()

pylint/lint/run.py

+2
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)