From 4b6d93328a091a64450344b56dc0456bf0a86ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=BChn?= Date: Tue, 25 Feb 2025 08:24:31 +0100 Subject: [PATCH 1/2] use positive logic --- asyncstdlib/functools.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/asyncstdlib/functools.py b/asyncstdlib/functools.py index 2f8b83c..07ff6fd 100644 --- a/asyncstdlib/functools.py +++ b/asyncstdlib/functools.py @@ -254,7 +254,9 @@ async def data(self): Instances on which a value is to be cached must have a ``__dict__`` attribute that is a mutable mapping. """ - if isinstance(type_or_getter, type) and issubclass( + if iscoroutinefunction(type_or_getter): + return CachedProperty(type_or_getter) + elif isinstance(type_or_getter, type) and issubclass( type_or_getter, AsyncContextManager ): @@ -269,14 +271,9 @@ def decorator( ) return decorator - - if not iscoroutinefunction( - type_or_getter # pyright: ignore[reportUnknownArgumentType] - ): + else: raise ValueError("cached_property can only be used with a coroutine function") - return CachedProperty(type_or_getter) - __REDUCE_SENTINEL = Sentinel("") From c73d463bf902c6bafa61faee0d46cddaa1a578ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=BChn?= Date: Tue, 25 Feb 2025 16:38:09 +0100 Subject: [PATCH 2/2] PyRight uses CoroutineType --- asyncstdlib/_lrucache.pyi | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/asyncstdlib/_lrucache.pyi b/asyncstdlib/_lrucache.pyi index c5fc40e..ad00bce 100644 --- a/asyncstdlib/_lrucache.pyi +++ b/asyncstdlib/_lrucache.pyi @@ -9,6 +9,7 @@ from typing import ( overload, Protocol, ) +from types import CoroutineType from typing_extensions import ParamSpec, Concatenate from ._typing import AC, TypedDict @@ -42,6 +43,12 @@ class LRUAsyncCallable(Protocol[AC]): owner: type | None = ..., ) -> LRUAsyncBoundCallable[S, P, R]: ... @overload + def __get__( + self: LRUAsyncCallable[Callable[Concatenate[S, P], CoroutineType[Any, Any, R]]], + instance: S, + owner: type | None = ..., + ) -> LRUAsyncBoundCallable[S, P, R]: ... + @overload def __get__( self: LRUAsyncCallable[Callable[Concatenate[S, P], Awaitable[R]]], instance: S,