Skip to content

Commit

Permalink
decorators.cache: use Generic type. drop py38
Browse files Browse the repository at this point in the history
  • Loading branch information
cybergrind committed Feb 20, 2023
1 parent a9cc009 commit 237f307
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion fan_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.13.0'
__version__ = '4.0.0'
14 changes: 7 additions & 7 deletions fan_tools/python/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools
import logging
from pathlib import Path
from typing import Awaitable, Callable, Protocol, Type, TypeVar, Union
from typing import Awaitable, Callable, Generic, Protocol, Type, TypeVar, Union


try:
Expand Down Expand Up @@ -77,34 +77,34 @@ def json(self) -> str:
...


FuncType = Callable[P, Awaitable[PydanticBaseModel]]
ModelType = TypeVar('ModelType', bound=PydanticBaseModel)


class cache_async:
class cache_async(Generic[ModelType]):
"""
file cache for async functions that returns pydantic models
NB: it doesn't use parameters to generate the cache file name
"""

def __init__(self, fname: Path, model: PydanticBaseModel, default: PydanticBaseModel):
def __init__(self, fname: Path, model: ModelType, default: ModelType):
self.fname = fname
self._default = default
self.cache = default
# load from file
if self.fname.exists():
self.cache = model.parse_file(self.fname)

def __call__(self, func: FuncType[P]) -> FuncType[P]:
def __call__(self, func: Callable[P, Awaitable[ModelType]]):
@functools.wraps(func)
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> PydanticBaseModel:
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> ModelType:
if self.cache:
return self.cache
value = await func(*args, **kwargs)
self.cache = value
self.fname.write_text(self.cache.json())
return value

wrapper.reset_cache = self.reset_cache
wrapper.reset_cache = self.reset_cache # type: ignore

return wrapper

Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ classifiers = [
'Operating System :: POSIX :: Linux',
'Operating System :: Unix',
'Programming Language :: Python',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: Implementation',
'Programming Language :: Python :: Implementation :: CPython',
'Topic :: Software Development',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async def test_01_simplest(self, tmp_path):

model.json.return_value = '{"a": "b"}'

@cache_async(fname, model, {})
@cache_async[type(dict)](fname, model, {})
async def func():
return model

Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tox]
min_version = 4.3
isolated_build = True
envlist = py{38,39,310}-django{32,40} #,mypy
envlist = py{39,310,311}-django{32,40} #,mypy

[testenv]
extras =
Expand All @@ -26,7 +26,7 @@ deps =
six
starlette==0.14.2
uvicorn[standart]==0.15.0
uvloop==0.16.0
uvloop
django32: djangorestframework==3.12.*
django32: Django==3.2.*
django40: Django==4.0.*
Expand Down

0 comments on commit 237f307

Please sign in to comment.