projects/guix·blob python-team · refs

201 lines · 6.8 KiB

1From d89186b47dc25c5ef5907c146edf3f792d50774b Mon Sep 17 00:00:00 2001
2Message-ID: <d89186b47dc25c5ef5907c146edf3f792d50774b.1743608515.git.ngraves@ngraves.fr>
3From: Nicolas Graves <[email protected]>
4Date: Wed, 2 Apr 2025 17:29:45 +0200
5Subject: [PATCH] Update to python 3.11
6
7---
8 property_cached/__init__.py | 21 +++-------
9 tests/test_async_cached_property.py | 3 +-
10 tests/test_coroutine_cached_property.py | 51 ++++++++++---------------
11 3 files changed, 27 insertions(+), 48 deletions(-)
12
13diff --git a/property_cached/__init__.py b/property_cached/__init__.py
14index 3353048..c033542 100644
15--- a/property_cached/__init__.py
16+++ b/property_cached/__init__.py
17@@ -3,6 +3,7 @@ import functools
18 import pkg_resources
19 import threading
20 import weakref
21+from inspect import iscoroutinefunction
22 from time import time
23
24
25@@ -31,12 +32,12 @@ class cached_property(property):
26 if obj is None:
27 return self
28
29- if asyncio and asyncio.iscoroutinefunction(self.func):
30- return self._wrap_in_coroutine(obj)
31-
32 value = self.cache.get(obj, self._sentinel)
33 if value is self._sentinel:
34- value = self.cache[obj] = self.func(obj)
35+ if iscoroutinefunction(self.func):
36+ self.cache[obj] = value = asyncio.ensure_future(self.func(obj))
37+ else:
38+ self.cache[obj] = value = self.func(obj)
39
40 return value
41
42@@ -49,18 +50,6 @@ class cached_property(property):
43 def __delete__(self, obj):
44 del self.cache[obj]
45
46- def _wrap_in_coroutine(self, obj):
47-
48- @functools.wraps(obj)
49- @asyncio.coroutine
50- def wrapper():
51- value = self.cache.get(obj, self._sentinel)
52- if value is self._sentinel:
53- self.cache[obj] = value = asyncio.ensure_future(self.func(obj))
54- return value
55-
56- return wrapper()
57-
58
59 class threaded_cached_property(cached_property):
60 """
61diff --git a/tests/test_async_cached_property.py b/tests/test_async_cached_property.py
62index 1af139d..32b3410 100644
63--- a/tests/test_async_cached_property.py
64+++ b/tests/test_async_cached_property.py
65@@ -9,8 +9,7 @@ import property_cached as cached_property
66
67 def unittest_run_loop(f):
68 def wrapper(*args, **kwargs):
69- coro = asyncio.coroutine(f)
70- future = coro(*args, **kwargs)
71+ future = f(*args, **kwargs)
72 loop = asyncio.get_event_loop()
73 loop.run_until_complete(future)
74
75diff --git a/tests/test_coroutine_cached_property.py b/tests/test_coroutine_cached_property.py
76index 40e443b..5864301 100644
77--- a/tests/test_coroutine_cached_property.py
78+++ b/tests/test_coroutine_cached_property.py
79@@ -14,8 +14,7 @@ import property_cached as cached_property
80
81 def unittest_run_loop(f):
82 def wrapper(*args, **kwargs):
83- coro = asyncio.coroutine(f)
84- future = coro(*args, **kwargs)
85+ future = f(*args, **kwargs)
86 loop = asyncio.get_event_loop()
87 loop.run_until_complete(future)
88
89@@ -33,14 +32,12 @@ def CheckFactory(cached_property_decorator):
90 self.control_total = 0
91 self.cached_total = 0
92
93- @asyncio.coroutine
94- def add_control(self):
95+ async def add_control(self):
96 self.control_total += 1
97 return self.control_total
98
99 @cached_property_decorator
100- @asyncio.coroutine
101- def add_cached(self):
102+ async def add_cached(self):
103 self.cached_total += 1
104 return self.cached_total
105
106@@ -52,74 +49,68 @@ class TestCachedProperty(unittest.TestCase):
107
108 cached_property_factory = cached_property.cached_property
109
110- @asyncio.coroutine
111- def assert_control(self, check, expected):
112+ async def assert_control(self, check, expected):
113 """
114 Assert that both `add_control` and 'control_total` equal `expected`
115 """
116- value = yield from check.add_control()
117+ value = await check.add_control()
118 self.assertEqual(value, expected)
119 self.assertEqual(check.control_total, expected)
120
121- @asyncio.coroutine
122- def assert_cached(self, check, expected):
123+ async def assert_cached(self, check, expected):
124 """
125 Assert that both `add_cached` and 'cached_total` equal `expected`
126 """
127 print("assert_cached", check.add_cached)
128- value = yield from check.add_cached
129+ value = await check.add_cached
130 self.assertEqual(value, expected)
131 self.assertEqual(check.cached_total, expected)
132
133 @unittest_run_loop
134- @asyncio.coroutine
135- def test_cached_property(self):
136+ async def test_cached_property(self):
137 Check = CheckFactory(self.cached_property_factory)
138 check = Check()
139
140 # The control shows that we can continue to add 1
141- yield from self.assert_control(check, 1)
142- yield from self.assert_control(check, 2)
143+ await self.assert_control(check, 1)
144+ await self.assert_control(check, 2)
145
146 # The cached version demonstrates how nothing is added after the first
147- yield from self.assert_cached(check, 1)
148- yield from self.assert_cached(check, 1)
149+ await self.assert_cached(check, 1)
150+ await self.assert_cached(check, 1)
151
152 # The cache does not expire
153 with freeze_time("9999-01-01"):
154- yield from self.assert_cached(check, 1)
155+ await self.assert_cached(check, 1)
156
157 # Typically descriptors return themselves if accessed though the class
158 # rather than through an instance.
159 self.assertTrue(isinstance(Check.add_cached, self.cached_property_factory))
160
161 @unittest_run_loop
162- @asyncio.coroutine
163- def test_reset_cached_property(self):
164+ async def test_reset_cached_property(self):
165 Check = CheckFactory(self.cached_property_factory)
166 check = Check()
167
168 # Run standard cache assertion
169- yield from self.assert_cached(check, 1)
170- yield from self.assert_cached(check, 1)
171+ await self.assert_cached(check, 1)
172+ await self.assert_cached(check, 1)
173
174 # Clear the cache
175 del check.add_cached
176
177 # Value is cached again after the next access
178- yield from self.assert_cached(check, 2)
179- yield from self.assert_cached(check, 2)
180+ await self.assert_cached(check, 2)
181+ await self.assert_cached(check, 2)
182
183 @unittest_run_loop
184- @asyncio.coroutine
185- def test_none_cached_property(self):
186+ async def test_none_cached_property(self):
187 class Check(object):
188 def __init__(self):
189 self.cached_total = None
190
191 @self.cached_property_factory
192- @asyncio.coroutine
193- def add_cached(self):
194+ async def add_cached(self):
195 return self.cached_total
196
197- yield from self.assert_cached(Check(), None)
198+ await self.assert_cached(Check(), None)
199--
2002.49.0
201