From d4bdf6b75dad236cccf98377e1e54c3c2b640c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 15 Sep 2022 11:50:58 +0200 Subject: [PATCH 1/3] Create a ``CacheManager`` class to manage storing and clearing caches --- ChangeLog | 2 ++ astroid/_cache.py | 22 ++++++++++++++++++++++ astroid/decorators.py | 3 ++- astroid/manager.py | 3 +++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 astroid/_cache.py diff --git a/ChangeLog b/ChangeLog index 142a9a7e0..8c6803eb4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,7 +17,9 @@ What's New in astroid 2.12.10? ============================== Release date: TBA +* ``decorators.cached`` now gets its cache cleared by calling ``AstroidManager.clear_cache``. + Refs #1780 What's New in astroid 2.12.9? ============================= diff --git a/astroid/_cache.py b/astroid/_cache.py new file mode 100644 index 000000000..d1b916838 --- /dev/null +++ b/astroid/_cache.py @@ -0,0 +1,22 @@ +from __future__ import annotations + +from typing import Any + + +class CacheManager: + """Manager of caches, to be used as a singleton.""" + + def __init__(self) -> None: + self.dict_caches: list[dict[Any, Any]] = [] + + def clear_all_caches(self) -> None: + """Clear all caches.""" + for dict_cache in self.dict_caches: + dict_cache.clear() + + def add_dict_cache(self, cache: dict[Any, Any]) -> None: + """Add a dictionary cache to the manager.""" + self.dict_caches.append(cache) + + +CACHE_MANAGER = CacheManager() diff --git a/astroid/decorators.py b/astroid/decorators.py index c4f44dcd2..e9cc3292c 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -15,7 +15,7 @@ import wrapt -from astroid import util +from astroid import _cache, util from astroid.context import InferenceContext from astroid.exceptions import InferenceError @@ -34,6 +34,7 @@ def cached(func, instance, args, kwargs): cache = getattr(instance, "__cache", None) if cache is None: instance.__cache = cache = {} + _cache.CACHE_MANAGER.add_dict_cache(cache) try: return cache[func] except KeyError: diff --git a/astroid/manager.py b/astroid/manager.py index 77d22503c..f0932d54d 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -16,6 +16,7 @@ from importlib.util import find_spec, module_from_spec from typing import TYPE_CHECKING, ClassVar +from astroid._cache import CACHE_MANAGER from astroid.const import BRAIN_MODULES_DIRECTORY from astroid.exceptions import AstroidBuildingError, AstroidImportError from astroid.interpreter._import import spec, util @@ -391,6 +392,8 @@ def clear_cache(self) -> None: # NB: not a new TransformVisitor() AstroidManager.brain["_transform"].transforms = collections.defaultdict(list) + CACHE_MANAGER.clear_all_caches() + for lru_cache in ( LookupMixIn.lookup, _cache_normalize_path_, From 00793195e50dab6c45496eac35a8b87cbec7b9eb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 15 Sep 2022 09:52:48 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- astroid/_cache.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/astroid/_cache.py b/astroid/_cache.py index d1b916838..e4d319ad4 100644 --- a/astroid/_cache.py +++ b/astroid/_cache.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE +# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt from __future__ import annotations from typing import Any From fc8e437739f8102a4270b2ab484a17d8c72644e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 15 Sep 2022 11:53:29 +0200 Subject: [PATCH 3/3] Update astroid/_cache.py --- astroid/_cache.py | 1 + 1 file changed, 1 insertion(+) diff --git a/astroid/_cache.py b/astroid/_cache.py index e4d319ad4..fc4ddc205 100644 --- a/astroid/_cache.py +++ b/astroid/_cache.py @@ -1,6 +1,7 @@ # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE # Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt + from __future__ import annotations from typing import Any