Skip to content

Commit 14f01e2

Browse files
authored
fix: python 3.10 support: StrEnum compatibility (#16)
* fix: python 3.10 support: StrEnum compatibility
1 parent 60cb99a commit 14f01e2

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/yapcache/__init__.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import sys
12
import asyncio
23
from dataclasses import dataclass
3-
from enum import StrEnum
44
from functools import wraps
55
from typing import Generic
66

7+
if sys.version_info >= (3, 11):
8+
from enum import StrEnum
9+
else:
10+
from enum import Enum
11+
12+
class StrEnum(str, Enum):
13+
def __str__(self):
14+
return str(self.value)
15+
16+
717
from typing_extensions import (
818
Any,
919
Awaitable,
@@ -39,7 +49,9 @@ def memoize(
3949
cache: Cache,
4050
cache_key: Callable[P, str],
4151
ttl: float | Callable[Concatenate[R, P], float],
42-
best_before: Callable[Concatenate[R, P], Optional[float]] = lambda _, *a, **kw: None,
52+
best_before: Callable[Concatenate[R, P], Optional[float]] = lambda _,
53+
*a,
54+
**kw: None,
4355
lock: Callable[[str], DistLock] = lambda *a, **kw: NullLock(),
4456
process_result: Callable[[MemoizeResult[R]], R] = lambda r, *a, **kw: r.result,
4557
) -> Callable[
@@ -54,9 +66,13 @@ async def wrapper(*args, **kwargs):
5466

5567
async def _call_with_lock():
5668
async with lock(key + ":lock"):
57-
found = await cache.get(key) # did someone populate the cache while I was waiting for the lock?
69+
found = await cache.get(
70+
key
71+
) # did someone populate the cache while I was waiting for the lock?
5872
if isinstance(found, CacheItem) and not found.is_stale:
59-
return MemoizeResult(result=found.value, cache_status=CacheStatus.HIT)
73+
return MemoizeResult(
74+
result=found.value, cache_status=CacheStatus.HIT
75+
)
6076

6177
result = await fn(*args, **kwargs)
6278

@@ -78,7 +94,9 @@ async def _call_with_lock():
7894
task.add_done_callback(lambda _: update_tasks.pop(key))
7995
status = CacheStatus.STALE
8096
return process_result(
81-
MemoizeResult(result=found.value, cache_status=status), *args, **kwargs
97+
MemoizeResult(result=found.value, cache_status=status),
98+
*args,
99+
**kwargs,
82100
)
83101

84102
result = await _call_with_lock()

0 commit comments

Comments
 (0)