Skip to content

Remove @cached decorator and CACHE_MANAGER #2072

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

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
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ What's New in astroid 2.16.0?
=============================
Release date: TBA

* Remove ``@cached`` decorator (just use ``@cached_property`` from the stdlib).

Closes #1780


What's New in astroid 2.15.2?
Expand Down
26 changes: 0 additions & 26 deletions astroid/_cache.py

This file was deleted.

3 changes: 1 addition & 2 deletions astroid/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, ClassVar

from astroid import decorators, nodes
from astroid import nodes
from astroid.const import PY310_PLUS
from astroid.context import (
CallContext,
Expand Down Expand Up @@ -634,7 +634,6 @@ def __init__(
self.parent = parent
self._call_context = copy_context(generator_initial_context)

@decorators.cached
def infer_yield_types(self):
yield from self.parent.infer_yield_result(self._call_context)

Expand Down
16 changes: 1 addition & 15 deletions astroid/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import wrapt

from astroid import _cache, util
from astroid import util
from astroid.context import InferenceContext
from astroid.exceptions import InferenceError

Expand All @@ -28,20 +28,6 @@
_P = ParamSpec("_P")


@wrapt.decorator
def cached(func, instance, args, kwargs):
"""Simple decorator to cache result of method calls without args."""
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:
cache[func] = result = func(*args, **kwargs)
return result


# TODO: Remove when support for 3.7 is dropped
# TODO: astroid 3.0 -> move class behind sys.version_info < (3, 8) guard
class cachedproperty:
Expand Down
3 changes: 0 additions & 3 deletions astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from typing import Any, ClassVar

from astroid import nodes
from astroid._cache import CACHE_MANAGER
from astroid.const import BRAIN_MODULES_DIRECTORY
from astroid.context import InferenceContext, _invalidate_cache
from astroid.exceptions import AstroidBuildingError, AstroidImportError
Expand Down Expand Up @@ -424,8 +423,6 @@ 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_,
Expand Down
7 changes: 5 additions & 2 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2937,8 +2937,8 @@ def _slots(self):
return [first, *slots]

# Cached, because inferring them all the time is expensive
@decorators_mod.cached
def slots(self):
@cached_property
def _all_slots(self):
"""Get all the slots for this node.

:returns: The names of slots for this class.
Expand Down Expand Up @@ -2982,6 +2982,9 @@ def grouped_slots(

return sorted(set(slots), key=lambda item: item.value)

def slots(self):
return self._all_slots

def _inferred_bases(self, context: InferenceContext | None = None):
# Similar with .ancestors, but the difference is when one base is inferred,
# only the first object is wanted. That's because
Expand Down