From 28d386cf3994b16f5845f2b495401011239cf393 Mon Sep 17 00:00:00 2001 From: Rajiv Bakulesh Shah Date: Mon, 31 Jan 2022 23:42:29 -0800 Subject: [PATCH] Use protocol instead of making _Clearable an ABC https://www.python.org/dev/peps/pep-0544/#defining-a-protocol --- pottery/base.py | 17 +++++++++-------- tests/test_base.py | 7 ------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/pottery/base.py b/pottery/base.py index ae046fd5..405905be 100644 --- a/pottery/base.py +++ b/pottery/base.py @@ -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 @@ -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 diff --git a/tests/test_base.py b/tests/test_base.py index 94baa884..55fbcf17 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -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 @@ -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):