Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport maintenance/2.17.x] Clear LRU caches on pylint utilities #8427

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/8361.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``--clear-cache-post-run`` now also clears LRU caches for pylint utilities
holding references to AST nodes.

Closes #8361
21 changes: 20 additions & 1 deletion pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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()
2 changes: 2 additions & 0 deletions pylint/lint/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -222,6 +223,7 @@ def __init__(
exit = do_exit

if linter.config.clear_cache_post_run:
clear_lru_caches()
MANAGER.clear_cache()

if exit:
Expand Down