Skip to content

Commit 8c6b054

Browse files
committed
Deprecate cached_property
1 parent d629e64 commit 8c6b054

1 file changed

Lines changed: 12 additions & 26 deletions

File tree

isimip_utils/utils.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Additional utility functions for ISIMIP tools."""
2-
from collections.abc import Callable
2+
import warnings
3+
from functools import cached_property as _cached_property
34
from pathlib import Path
45
from typing import Any, Literal
56

@@ -21,32 +22,17 @@ def __new__(cls) -> 'Singleton':
2122
return cls._instance
2223

2324

24-
class cached_property:
25-
"""Decorator that converts a method into a cached property.
25+
class cached_property(_cached_property):
26+
def __init_subclass__(cls, **kwargs):
27+
super().__init_subclass__(**kwargs)
2628

27-
The property value is computed once and then cached as an instance attribute.
28-
Subsequent accesses return the cached value without re-computing.
29-
30-
Simplified version of
31-
[Django's cached_property](https://github.com/django/django/blob/main/django/utils/functional.py).
32-
"""
33-
34-
name: str | None = None
35-
36-
def __init__(self, func: Callable) -> None:
37-
self.func = func
38-
39-
def __set_name__(self, owner: type, name: str) -> None:
40-
if self.name is None:
41-
self.name = name
42-
else:
43-
raise TypeError("Cannot assign the same cached_property to two different names")
44-
45-
def __get__(self, instance: Any, cls: type | None = None) -> Any:
46-
if instance is None:
47-
return self
48-
value = instance.__dict__[self.name] = self.func(instance)
49-
return value
29+
def __get__(self, instance, cls=None):
30+
warnings.warn(
31+
"isimip-utils.utils.cached_property is deprecated, use functools.cached_property instead.",
32+
DeprecationWarning,
33+
stacklevel=2,
34+
)
35+
return super().__get__(instance, cls)
5036

5137

5238
def exclude_path(exclude: list[str] | None, path: Path | str, match: Literal['any', 'all'] = 'any') -> bool:

0 commit comments

Comments
 (0)