From 0cd494f938ff2e41a94863a744597c843cf10a0c Mon Sep 17 00:00:00 2001 From: Rajiv Bakulesh Shah Date: Tue, 11 Jan 2022 19:59:56 -0800 Subject: [PATCH] Use isinstance() to provide a hint to Mypy This is cleaner than traditional duck typing as it provides a hint to Mypy, and prevents us having to use ugly `cast()`s everywhere. --- pottery/base.py | 8 ++++---- pottery/cache.py | 6 ++---- pottery/counter.py | 10 +++++----- pottery/dict.py | 7 +++---- pottery/nextid.py | 2 +- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/pottery/base.py b/pottery/base.py index c2aad28e..353240dd 100644 --- a/pottery/base.py +++ b/pottery/base.py @@ -156,10 +156,10 @@ def _encode(decoded_value: JSONTypes) -> str: @final @staticmethod def _decode(encoded_value: AnyStr) -> JSONTypes: - try: - string = cast(bytes, encoded_value).decode('utf-8') - except AttributeError: - string = cast(str, encoded_value) + if isinstance(encoded_value, bytes): + string = encoded_value.decode('utf-8') + else: + string = encoded_value decoded_value: JSONTypes = json.loads(string) return decoded_value diff --git a/pottery/cache.py b/pottery/cache.py index 14b14cf6..ee7a2c39 100644 --- a/pottery/cache.py +++ b/pottery/cache.py @@ -17,7 +17,6 @@ import collections -import contextlib import functools import itertools from typing import Any @@ -44,7 +43,6 @@ from .base import random_key from .dict import InitArg from .dict import InitIter -from .dict import InitMap from .dict import RedisDict @@ -330,8 +328,8 @@ def update(self, arg: InitArg = tuple(), **kwargs: JSONTypes) -> None: # type: makes a single bulk call to Redis. ''' to_cache = {} - with contextlib.suppress(AttributeError): - arg = cast(InitMap, arg).items() + if isinstance(arg, collections.abc.Mapping): + arg = arg.items() items = itertools.chain(cast(InitIter, arg), kwargs.items()) for dict_key, value in items: if value is not self._SENTINEL: diff --git a/pottery/counter.py b/pottery/counter.py index 5fdadfc2..1d55fad9 100644 --- a/pottery/counter.py +++ b/pottery/counter.py @@ -58,11 +58,11 @@ def _populate(self, # type: ignore **kwargs: int, ) -> None: dict_ = {} - try: - items = cast(Counter[JSONTypes], arg).items() + if isinstance(arg, collections.abc.Mapping): + items = arg.items() for key, value in items: dict_[key] = sign * value - except AttributeError: + else: for key in arg: value = dict_.get(key, self[key]) dict_[key] = value + sign @@ -180,7 +180,7 @@ def __imath_op(self, ) -> RedisCounter: with self._watch(other) as pipeline: try: - other_items = cast('RedisCounter', other).to_counter().items() + other_items = cast(RedisCounter, other).to_counter().items() except AttributeError: other_items = other.items() to_set = {k: self[k] + sign * v for k, v in other_items} @@ -217,7 +217,7 @@ def __iset_op(self, with self._watch(other) as pipeline: self_counter = self.__to_counter() try: - other_counter = cast('RedisCounter', other).to_counter() + other_counter = cast(RedisCounter, other).to_counter() except AttributeError: other_counter = other to_set, to_del = {}, set() diff --git a/pottery/dict.py b/pottery/dict.py index 803187c5..20119dbb 100644 --- a/pottery/dict.py +++ b/pottery/dict.py @@ -17,7 +17,6 @@ import collections.abc -import contextlib import itertools import warnings from typing import Any @@ -69,9 +68,9 @@ def _populate(self, arg: InitArg = tuple(), **kwargs: JSONTypes, ) -> None: - with contextlib.suppress(AttributeError): - arg = cast(InitMap, arg).items() - items = itertools.chain(cast(InitIter, arg), kwargs.items()) + if isinstance(arg, collections.abc.Mapping): + arg = arg.items() + items = itertools.chain(arg, kwargs.items()) dict_ = dict(items) encoded_dict = self.__encode_dict(dict_) if encoded_dict: diff --git a/pottery/nextid.py b/pottery/nextid.py index 0b997135..03284c28 100644 --- a/pottery/nextid.py +++ b/pottery/nextid.py @@ -180,7 +180,7 @@ def __current_id(self) -> int: current_ids, redis_errors = [], [] for future in concurrent.futures.as_completed(futures): try: - current_id = int(cast(bytes, future.result() or b'0')) + current_id = int(future.result() or b'0') except RedisError as error: redis_errors.append(error) logger.exception(