1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
From cb1f82b160e053819f41c87fcb41a48d4d44b0ea Mon Sep 17 00:00:00 2001
From: Petr Viktorin <encukou@gmail.com>
Date: Thu, 2 Dec 2021 11:26:20 +0100
Subject: [PATCH] Use iscoroutinefunction from inspect not asyncio
Python 3.14 will deprecate asyncio.iscoroutinefunction:
https://github.com/python/cpython/pull/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).
---
cached_property.py | 18 ++++++------------
1 file changed, 6 insertions(+), 12 deletions(-)
diff --git a/cached_property.py b/cached_property.py
index e83ecca..fe06b34 100644
--- a/cached_property.py
+++ b/cached_property.py
@@ -4,6 +4,7 @@
__license__ = "BSD"
from functools import wraps
+from inspect import iscoroutinefunction
from time import time
import threading
import asyncio
@@ -24,21 +25,14 @@ def __get__(self, obj, cls):
if obj is None:
return self
- if asyncio.iscoroutinefunction(self.func):
- return self._wrap_in_coroutine(obj)
+ if iscoroutinefunction(self.func):
+ value = asyncio.ensure_future(self.func(obj))
+ else:
+ value = self.func(obj)
- value = obj.__dict__[self.func.__name__] = self.func(obj)
+ obj.__dict__[self.func.__name__] = value
return value
- def _wrap_in_coroutine(self, obj):
- @wraps(obj)
- def wrapper():
- future = asyncio.ensure_future(self.func(obj))
- obj.__dict__[self.func.__name__] = future
- return future
-
- return wrapper()
-
class threaded_cached_property:
"""
|