feat(storage-api): remove default param for get_state methods.

This commit is contained in:
mpa 2020-08-16 23:16:02 +04:00
parent 7f8004f5a1
commit 00e3543cc3
No known key found for this signature in database
GPG key ID: BCCFBFCCC9B754A8
3 changed files with 6 additions and 7 deletions

View file

@ -33,8 +33,8 @@ class CurrentUserContext(Generic[StorageDataT]):
self.storage = storage
self.key = key_maker(chat_id, user_id)
async def get_state(self, default: Optional[str] = None) -> Optional[str]:
return await self.storage.get_state(self.key, default=default)
async def get_state(self) -> Optional[str]:
return await self.storage.get_state(self.key)
async def get_data(self) -> StorageDataT:
return await self.storage.get_data(self.key)

View file

@ -4,7 +4,7 @@ _DataT = TypeVar("_DataT")
class BaseStorage(Generic[_DataT]):
async def get_state(self, key: str, default: Optional[str] = None) -> Optional[str]:
async def get_state(self, key: str) -> Optional[str]:
raise NotImplementedError
async def set_state(self, key: str, state: Optional[str]) -> None:

View file

@ -13,19 +13,18 @@ class _UserStorageMetaData(TypedDict):
class DictStorage(BaseStorage[Dict[str, Any]]):
"""
In-memory based states storage.
This type of storage is not recommended for usage in bots, because you will lost all states after restarting.
Python dictionary data structure based state storage.
Not the most persistent storage, not recommended for in-production environments.
"""
def __init__(self) -> None:
# note: we can use TypedDict for Dict flat value
self.data: Dict[str, _UserStorageMetaData] = {}
def resolve_address(self, key: str) -> None:
if key not in self.data:
self.data[key] = {"state": None, "data": {}}
async def get_state(self, key: str, default: Optional[str] = None) -> Optional[str]:
async def get_state(self, key: str) -> Optional[str]:
self.resolve_address(key)
return self.data[key]["state"]