diff --git a/doc/whatsnew/fragments/8361.bugfix b/doc/whatsnew/fragments/8361.bugfix new file mode 100644 index 0000000000..0f2b56707b --- /dev/null +++ b/doc/whatsnew/fragments/8361.bugfix @@ -0,0 +1,4 @@ +``--clear-cache-post-run`` now also clears LRU caches for pylint utilities +holding references to AST nodes. + +Closes #8361 diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index 9f1ce93fed..0cee91f17b 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -17,7 +17,7 @@ from collections.abc import Iterable, Iterator from functools import lru_cache, partial from re import Match -from typing import TYPE_CHECKING, Callable, TypeVar +from typing import TYPE_CHECKING, Any, Callable, TypeVar import _string import astroid.objects @@ -28,6 +28,8 @@ from astroid.typing import InferenceResult, SuccessfulInferenceResult if TYPE_CHECKING: + from functools import _lru_cache_wrapper + from pylint.checkers import BaseChecker _NodeT = TypeVar("_NodeT", bound=nodes.NodeNG) @@ -2295,3 +2297,20 @@ def not_condition_as_string( ) msg = f"{lhs} {get_inverse_comparator(ops)} {rhs}" return msg + + +def clear_lru_caches() -> None: + """Clear caches holding references to AST nodes.""" + # pylint: disable-next=import-outside-toplevel + from pylint.checkers.variables import overridden_method + + caches_holding_node_references: list[_lru_cache_wrapper[Any]] = [ + in_for_else_branch, + infer_all, + is_overload_stub, + overridden_method, + unimplemented_abstract_methods, + safe_infer, + ] + for lru in caches_holding_node_references: + lru.cache_clear() diff --git a/pylint/lint/run.py b/pylint/lint/run.py index 2232c41a3e..49b807f876 100644 --- a/pylint/lint/run.py +++ b/pylint/lint/run.py @@ -12,6 +12,7 @@ from typing import Any, ClassVar from pylint import config +from pylint.checkers.utils import clear_lru_caches from pylint.config._pylint_config import ( _handle_pylint_config_commands, _register_generate_config_options, @@ -222,6 +223,7 @@ def __init__( exit = do_exit if linter.config.clear_cache_post_run: + clear_lru_caches() MANAGER.clear_cache() if exit: