add types

This commit is contained in:
pablodanswer
2024-10-19 16:55:08 -07:00
parent 842d4ab2a8
commit 82a9fda846

View File

@@ -1,5 +1,7 @@
import functools import functools
import threading import threading
from collections.abc import Callable
from typing import Any
from typing import Optional from typing import Optional
import redis import redis
@@ -18,12 +20,12 @@ from danswer.configs.constants import REDIS_SOCKET_KEEPALIVE_OPTIONS
class TenantRedis(redis.Redis): class TenantRedis(redis.Redis):
def __init__(self, tenant_id: str, *args, **kwargs): def __init__(self, tenant_id: str, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.tenant_id = tenant_id self.tenant_id: str = tenant_id
def _prefixed(self, key): def _prefixed(self, key: str | bytes | memoryview) -> str | bytes | memoryview:
prefix = f"{self.tenant_id}:" prefix: str = f"{self.tenant_id}:"
if isinstance(key, str): if isinstance(key, str):
return prefix + key return prefix + key
elif isinstance(key, bytes): elif isinstance(key, bytes):
@@ -33,9 +35,9 @@ class TenantRedis(redis.Redis):
else: else:
raise TypeError(f"Unsupported key type: {type(key)}") raise TypeError(f"Unsupported key type: {type(key)}")
def _prefix_method(self, method): def _prefix_method(self, method: Callable) -> Callable:
@functools.wraps(method) @functools.wraps(method)
def wrapper(*args, **kwargs): def wrapper(*args: Any, **kwargs: Any) -> Any:
if "name" in kwargs: if "name" in kwargs:
kwargs["name"] = self._prefixed(kwargs["name"]) kwargs["name"] = self._prefixed(kwargs["name"])
elif len(args) > 0: elif len(args) > 0:
@@ -44,7 +46,7 @@ class TenantRedis(redis.Redis):
return wrapper return wrapper
def __getattribute__(self, item): def __getattribute__(self, item: str) -> Any:
original_attr = super().__getattribute__(item) original_attr = super().__getattribute__(item)
methods_to_wrap = [ methods_to_wrap = [
"get", "get",