From 68ef94f2dbe03802af791040095b36f1b7f2ae68 Mon Sep 17 00:00:00 2001 From: Ivan Moiseev Date: Sat, 22 Oct 2022 21:05:43 +0400 Subject: [PATCH] feat: add more asserts for FastAPICache init --- fastapi_cache/__init__.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/fastapi_cache/__init__.py b/fastapi_cache/__init__.py index eaa65f4..d9acd9e 100644 --- a/fastapi_cache/__init__.py +++ b/fastapi_cache/__init__.py @@ -40,7 +40,8 @@ class FastAPICache: return cls._backend @classmethod - def get_prefix(cls) -> Optional[str]: + def get_prefix(cls) -> str: + assert cls._prefix, "You must call init first!" # nosec: B101 return cls._prefix @classmethod @@ -48,11 +49,13 @@ class FastAPICache: return cls._expire @classmethod - def get_coder(cls) -> Optional[Type[Coder]]: + def get_coder(cls) -> Type[Coder]: + assert cls._coder, "You must call init first!" # nosec: B101 return cls._coder @classmethod - def get_key_builder(cls) -> Optional[Callable]: + def get_key_builder(cls) -> Callable: + assert cls._key_builder, "You must call init first!" # nosec: B101 return cls._key_builder @classmethod @@ -61,5 +64,6 @@ class FastAPICache: @classmethod async def clear(cls, namespace: Optional[str] = None, key: Optional[str] = None) -> int: + assert cls._backend and cls._prefix, "You must call init first!" # nosec: B101 namespace = cls._prefix + (":" + namespace if namespace else "") return await cls._backend.clear(namespace, key)