From 57d40c959def285ca1abaea5f13f65d8d217cf48 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 10 Mar 2023 08:07:19 -0500 Subject: [PATCH 1/3] Clear LRU caches on pylint utilities --- doc/whatsnew/fragments/8361.bugfix | 4 ++++ pylint/checkers/utils.py | 21 +++++++++++++++++++-- pylint/lint/run.py | 2 ++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 doc/whatsnew/fragments/8361.bugfix 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 d1d77dc0a8..48e31b2a33 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -14,9 +14,9 @@ import string from collections import deque from collections.abc import Iterable, Iterator -from functools import lru_cache, partial +from functools import _lru_cache_wrapper, 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 @@ -2243,3 +2243,20 @@ def not_condition_as_string( ) msg = f"{lhs} {get_inverse_comparator(ops)} {rhs}" return msg + + +def clear_lru_caches() -> None: + """Clear LRU 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: From 202f2820ea146007e0f6bf14c5bab23e655d3d2e Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 10 Mar 2023 08:34:50 -0500 Subject: [PATCH 2/3] fixup! Clear Guard _lru_cache_wrapper under TYPE_CHECKING --- pylint/checkers/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index 48e31b2a33..0b2737609c 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -14,7 +14,7 @@ import string from collections import deque from collections.abc import Iterable, Iterator -from functools import _lru_cache_wrapper, lru_cache, partial +from functools import lru_cache, partial from re import Match from typing import TYPE_CHECKING, Any, Callable, TypeVar @@ -27,6 +27,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) From f8206be7d72ba26636acfb800b3745195d0c192c Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 10 Mar 2023 13:40:25 -0500 Subject: [PATCH 3/3] fixup! satisfy spelling check --- pylint/checkers/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index 0b2737609c..4fdb3ad22b 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -2248,7 +2248,7 @@ def not_condition_as_string( def clear_lru_caches() -> None: - """Clear LRU caches holding references to AST nodes.""" + """Clear caches holding references to AST nodes.""" # pylint: disable-next=import-outside-toplevel from pylint.checkers.variables import overridden_method