Skip to content

Remove cachedproperty decorator #2140

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 1 commit into from
Apr 23, 2023
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
5 changes: 3 additions & 2 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
astroid's ChangeLog
===================

What's New in astroid 2.16.0?
What's New in astroid 3.0.0?
=============================
Release date: TBA

* Remove support for Python 3.7.

Refs #2137

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

Closes #1780
Refs #2140

* Reduce file system access in ``ast_from_file()``.

Expand Down
46 changes: 0 additions & 46 deletions astroid/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,6 @@
_P = ParamSpec("_P")


# TODO: Remove for astroid 3.0
class cachedproperty:
"""Provides a cached property equivalent to the stacking of
@cached and @property, but more efficient.

After first usage, the <property_name> becomes part of the object's
__dict__. Doing:

del obj.<property_name> empties the cache.

Idea taken from the pyramid_ framework and the mercurial_ project.

.. _pyramid: http://pypi.python.org/pypi/pyramid
.. _mercurial: http://pypi.python.org/pypi/Mercurial
"""

__slots__ = ("wrapped",)

def __init__(self, wrapped):
warnings.warn(
"cachedproperty has been deprecated and will be removed in astroid 3.0"
"Use functools.cached_property instead.",
DeprecationWarning,
stacklevel=2,
)
try:
wrapped.__name__ # noqa[B018]
except AttributeError as exc:
raise TypeError(f"{wrapped} must have a __name__ attribute") from exc
self.wrapped = wrapped

@property
def __doc__(self):
doc = getattr(self.wrapped, "__doc__", None)
return "<wrapped by the cachedproperty decorator>%s" % (
"\n%s" % doc if doc else ""
)

def __get__(self, inst, objtype=None):
if inst is None:
return self
val = self.wrapped(inst)
setattr(inst, self.wrapped.__name__, val)
return val


def path_wrapper(func):
"""Return the given infer function wrapped to handle the path.

Expand Down
18 changes: 1 addition & 17 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import pytest
from _pytest.recwarn import WarningsRecorder

from astroid.const import PY38_PLUS
from astroid.decorators import cachedproperty, deprecate_default_argument_values
from astroid.decorators import deprecate_default_argument_values


class SomeClass:
Expand Down Expand Up @@ -102,18 +101,3 @@ def test_deprecated_default_argument_values_ok(recwarn: WarningsRecorder) -> Non
instance = SomeClass(name="some_name")
instance.func(name="", var=42)
assert len(recwarn) == 0


@pytest.mark.skipif(not PY38_PLUS, reason="Requires Python 3.8 or higher")
def test_deprecation_warning_on_cachedproperty() -> None:
"""Check the DeprecationWarning on cachedproperty."""

with pytest.warns(DeprecationWarning) as records:

class MyClass: # pylint: disable=unused-variable
@cachedproperty
def my_property(self):
return 1

assert len(records) == 1
assert "functools.cached_property" in records[0].message.args[0]