Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use protocol instead of making _Clearable an ABC #615

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pottery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
from redis.client import Pipeline
# TODO: When we drop support for Python 3.7, change the following imports to:
# from typing import Final
# from typing import Protocol
# from typing import final
from typing_extensions import Final
from typing_extensions import Protocol
from typing_extensions import final

from .annotations import JSONTypes
Expand Down Expand Up @@ -163,20 +165,19 @@ def _decode(encoded_value: AnyStr) -> JSONTypes:
return decoded_value


class _Clearable(abc.ABC):
'Mixin class that implements clearing (emptying) a Redis-backed collection.'

class _HasRedisClientAndKey(Protocol):
@property
@abc.abstractmethod
def redis(self) -> Redis:
'Redis client.'
...

@property
@abc.abstractmethod
def key(self) -> str:
'Redis key.'
...

def clear(self) -> None:

class _Clearable:
'Mixin class that implements clearing (emptying) a Redis-backed collection.'
def clear(self: _HasRedisClientAndKey) -> None:
'Remove the elements in a Redis-backed container. O(n)'
self.redis.unlink(self.key) # Available since Redis 4.0.0

Expand Down
7 changes: 0 additions & 7 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from pottery import RedisDict
from pottery.base import Iterable_
from pottery.base import Primitive
from pottery.base import _Clearable
from pottery.base import _Comparable
from pottery.base import _Pipelined
from pottery.base import random_key
Expand Down Expand Up @@ -147,12 +146,6 @@ def test_decoded_responses(self):
repr(tel)


class ClearableTests(TestCase):
def test_abc_cant_be_instantiated(self):
with self.assertRaises(TypeError):
_Clearable()


class PipelinedTests(TestCase):
def test_abc_cant_be_instantiated(self):
with self.assertRaises(TypeError):
Expand Down