summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>2023-03-10 21:23:28 +0100
committerGitHub <noreply@github.com>2023-03-10 21:23:28 +0100
commit8c3652a6a4c97d0ed0e92484d4ce15e669bd69cf (patch)
tree9cfc575c1b36b11798f703ba51d50c3cc2e1a58b
parent2e78c414d24c9819936351c133b3edcad970abaa (diff)
downloadpylint-git-8c3652a6a4c97d0ed0e92484d4ce15e669bd69cf.tar.gz
Clear LRU caches on pylint utilities (#8420) (#8427)
(cherry picked from commit ab20812ad0e9ca17cc3bc32dbf34be575dcea502) Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
-rw-r--r--doc/whatsnew/fragments/8361.bugfix4
-rw-r--r--pylint/checkers/utils.py21
-rw-r--r--pylint/lint/run.py2
3 files changed, 26 insertions, 1 deletions
diff --git a/doc/whatsnew/fragments/8361.bugfix b/doc/whatsnew/fragments/8361.bugfix
new file mode 100644
index 000000000..0f2b56707
--- /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 9f1ce93fe..0cee91f17 100644
--- a/pylint/checkers/utils.py
+++ b/pylint/checkers/utils.py
@@ -17,7 +17,7 @@ from collections import deque
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.nodes._base_nodes import ImportNode
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 2232c41a3..49b807f87 100644
--- a/pylint/lint/run.py
+++ b/pylint/lint/run.py
@@ -12,6 +12,7 @@ from pathlib import Path
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 @@ group are mutually exclusive.",
exit = do_exit
if linter.config.clear_cache_post_run:
+ clear_lru_caches()
MANAGER.clear_cache()
if exit: