Skip to content

Commit

Permalink
Resolve type issues in redis cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgerrits committed Mar 5, 2024
1 parent 0a79512 commit d074ef5
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions autogen/cache/redis_cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import pickle
from types import TracebackType
from typing import Any, Optional, Type
import redis
import sys
from .abstract_cache_base import AbstractCache

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


class RedisCache(AbstractCache):
"""
Expand All @@ -24,7 +32,7 @@ class RedisCache(AbstractCache):
__exit__(self, exc_type, exc_value, traceback): Context management exit.
"""

def __init__(self, seed, redis_url):
def __init__(self, seed: str, redis_url: str):
"""
Initialize the RedisCache instance.
Expand All @@ -36,7 +44,7 @@ def __init__(self, seed, redis_url):
self.seed = seed
self.cache = redis.Redis.from_url(redis_url)

def _prefixed_key(self, key):
def _prefixed_key(self, key: str) -> str:
"""
Get a namespaced key for the cache.
Expand All @@ -48,7 +56,7 @@ def _prefixed_key(self, key):
"""
return f"autogen:{self.seed}:{key}"

def get(self, key, default=None):
def get(self, key: str, default: Optional[Any] = None) -> Any:
"""
Retrieve an item from the Redis cache.
Expand All @@ -65,7 +73,7 @@ def get(self, key, default=None):
return default
return pickle.loads(result)

def set(self, key, value):
def set(self, key: str, value: Any) -> None:
"""
Set an item in the Redis cache.
Expand All @@ -79,15 +87,15 @@ def set(self, key, value):
serialized_value = pickle.dumps(value)
self.cache.set(self._prefixed_key(key), serialized_value)

def close(self):
def close(self) -> None:
"""
Close the Redis client.
Perform any necessary cleanup, such as closing network connections.
"""
self.cache.close()

def __enter__(self):
def __enter__(self) -> Self:
"""
Enter the runtime context related to the object.
Expand All @@ -96,7 +104,9 @@ def __enter__(self):
"""
return self

def __exit__(self, exc_type, exc_value, traceback):
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> None:
"""
Exit the runtime context related to the object.
Expand Down

0 comments on commit d074ef5

Please sign in to comment.