From 3d2988fea92e08ed7009a32189efd93e76ab8012 Mon Sep 17 00:00:00 2001 From: dmitry krokhin Date: Sat, 23 Sep 2023 14:58:18 +0300 Subject: [PATCH] serializer protocol fix --- sharded_queue/drivers.py | 8 +++----- sharded_queue/protocols.py | 10 ++++------ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/sharded_queue/drivers.py b/sharded_queue/drivers.py index a7115b2..0878050 100644 --- a/sharded_queue/drivers.py +++ b/sharded_queue/drivers.py @@ -1,13 +1,11 @@ from json import dumps, loads -from typing import Any, List, Sequence, TypeVar +from typing import Any, List, Sequence from redis.asyncio import Redis from sharded_queue.protocols import Lock, Serializer, Storage from sharded_queue.settings import settings -T = TypeVar('T') - class JsonTupleSerializer(Serializer): def get_values(self, request) -> list[Any]: @@ -15,10 +13,10 @@ def get_values(self, request) -> list[Any]: return [k for k in request] return list(request.__dict__.values()) - def serialize(self, request: T) -> str: + def serialize(self, request: Any) -> str: return dumps(self.get_values(request)) - def deserialize(self, cls: type[T], source: str) -> T: + def deserialize(self, cls: type[Any], source: str) -> Any: values = loads(source) if hasattr(cls, 'model_fields'): return cls(**dict(zip(cls.model_fields, values))) diff --git a/sharded_queue/protocols.py b/sharded_queue/protocols.py index b18d7b7..54e220c 100644 --- a/sharded_queue/protocols.py +++ b/sharded_queue/protocols.py @@ -1,6 +1,4 @@ -from typing import Any, Protocol, TypeVar - -T = TypeVar('T') +from typing import Any, Protocol class Lock(Protocol): @@ -10,10 +8,10 @@ async def release(self, key: str) -> None: ... async def ttl(self, key: str, ttl: int) -> bool: ... -class Serializer(Protocol[T]): +class Serializer(Protocol): def get_values(self, request) -> list[Any]: ... - def serialize(self, request: T) -> str: ... - def deserialize(self, cls: type[T], source: str) -> T: ... + def serialize(self, request: Any) -> str: ... + def deserialize(self, cls: type[Any], source: str) -> Any: ... class Storage(Protocol):