From 8c6b054f3fe4adc4e1fc33c581a1bd4309eba027 Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Thu, 16 Apr 2026 15:19:16 +0200 Subject: [PATCH 1/2] Deprecate cached_property --- isimip_utils/utils.py | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/isimip_utils/utils.py b/isimip_utils/utils.py index b4b3720..7492aba 100644 --- a/isimip_utils/utils.py +++ b/isimip_utils/utils.py @@ -1,5 +1,6 @@ """Additional utility functions for ISIMIP tools.""" -from collections.abc import Callable +import warnings +from functools import cached_property as _cached_property from pathlib import Path from typing import Any, Literal @@ -21,32 +22,17 @@ def __new__(cls) -> 'Singleton': return cls._instance -class cached_property: - """Decorator that converts a method into a cached property. +class cached_property(_cached_property): + def __init_subclass__(cls, **kwargs): + super().__init_subclass__(**kwargs) - The property value is computed once and then cached as an instance attribute. - Subsequent accesses return the cached value without re-computing. - - Simplified version of - [Django's cached_property](https://github.com/django/django/blob/main/django/utils/functional.py). - """ - - name: str | None = None - - def __init__(self, func: Callable) -> None: - self.func = func - - def __set_name__(self, owner: type, name: str) -> None: - if self.name is None: - self.name = name - else: - raise TypeError("Cannot assign the same cached_property to two different names") - - def __get__(self, instance: Any, cls: type | None = None) -> Any: - if instance is None: - return self - value = instance.__dict__[self.name] = self.func(instance) - return value + def __get__(self, instance, cls=None): + warnings.warn( + "isimip-utils.utils.cached_property is deprecated, use functools.cached_property instead.", + DeprecationWarning, + stacklevel=2, + ) + return super().__get__(instance, cls) def exclude_path(exclude: list[str] | None, path: Path | str, match: Literal['any', 'all'] = 'any') -> bool: From 481dd57425eec12c31ca01ddd811f81dc49f17af Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Fri, 17 Apr 2026 11:26:19 +0200 Subject: [PATCH 2/2] Update deprecation message --- isimip_utils/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/isimip_utils/utils.py b/isimip_utils/utils.py index 7492aba..39aa61b 100644 --- a/isimip_utils/utils.py +++ b/isimip_utils/utils.py @@ -28,7 +28,8 @@ def __init_subclass__(cls, **kwargs): def __get__(self, instance, cls=None): warnings.warn( - "isimip-utils.utils.cached_property is deprecated, use functools.cached_property instead.", + 'isimip-utils.utils.cached_property is deprecated and will be removed in 2.1.0, ' + 'use functools.cached_property instead.', DeprecationWarning, stacklevel=2, )