Skip to content

Commit 70a59cc

Browse files
Maintenance: cached property nesting (#49)
* simplified cached_property
1 parent 2b7038d commit 70a59cc

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

asyncstdlib/functools.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,9 @@ def __get__(self, instance, owner):
116116
return self._get_attribute(instance)
117117

118118
async def _get_attribute(self, instance) -> T:
119-
attributes = instance.__dict__
120-
try:
121-
return attributes[self._name]
122-
except KeyError:
123-
value = await self.__wrapped__(instance)
124-
if self._name not in attributes:
125-
attributes[self._name] = AwaitableValue(value)
126-
return value
119+
value = await self.__wrapped__(instance)
120+
instance.__dict__[self._name] = AwaitableValue(value)
121+
return value
127122

128123

129124
cached_property = CachedProperty

unittests/test_functools.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ class Pair:
3535
__slots__ = "a", "b"
3636

3737
def __init__(self, a, b):
38-
self.a = a
39-
self.b = b
38+
pass # pragma: no cover
4039

4140
@a.cached_property
4241
async def total(self):
43-
return self.a + self.b
42+
pass # pragma: no cover
4443

4544

4645
@multi_sync
@@ -62,7 +61,7 @@ async def check_increment(to):
6261
val = Value(0)
6362
await Schedule(check_increment(5), check_increment(12), check_increment(1337))
6463
assert (await val.cached) != 0
65-
assert (await val.cached) == 5 # first value fetched
64+
assert (await val.cached) == 1337 # last value fetched
6665

6766

6867
@sync

0 commit comments

Comments
 (0)