Skip to content

Commit cb1f82b

Browse files
encukouAdamWill
authored andcommitted
Use iscoroutinefunction from inspect not asyncio
Python 3.14 will deprecate asyncio.iscoroutinefunction: python/cpython#122875 inspect.iscoroutinefunction exists since 3.5 and our baseline is 3.8, so we can just use it unconditionally. Using a wrapper with @asyncio.coroutine in __get__ wasn't necessary (the future from asyncio.ensure_future is awaitable, and the wrapper doesn't do anything asynchronous), so the logic can be simplified to just call asyncio.ensure_future (to schedule the task and store the result when it's available).
1 parent c032196 commit cb1f82b

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

cached_property.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
__license__ = "BSD"
55

66
from functools import wraps
7+
from inspect import iscoroutinefunction
78
from time import time
89
import threading
910
import asyncio
@@ -24,21 +25,14 @@ def __get__(self, obj, cls):
2425
if obj is None:
2526
return self
2627

27-
if asyncio.iscoroutinefunction(self.func):
28-
return self._wrap_in_coroutine(obj)
28+
if iscoroutinefunction(self.func):
29+
value = asyncio.ensure_future(self.func(obj))
30+
else:
31+
value = self.func(obj)
2932

30-
value = obj.__dict__[self.func.__name__] = self.func(obj)
33+
obj.__dict__[self.func.__name__] = value
3134
return value
3235

33-
def _wrap_in_coroutine(self, obj):
34-
@wraps(obj)
35-
def wrapper():
36-
future = asyncio.ensure_future(self.func(obj))
37-
obj.__dict__[self.func.__name__] = future
38-
return future
39-
40-
return wrapper()
41-
4236

4337
class threaded_cached_property:
4438
"""

0 commit comments

Comments
 (0)