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 isinstance() to provide a hint to Mypy #589

Merged
merged 1 commit into from
Jan 12, 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
8 changes: 4 additions & 4 deletions pottery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 2 additions & 4 deletions pottery/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


import collections
import contextlib
import functools
import itertools
from typing import Any
Expand All @@ -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


Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions pottery/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions pottery/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


import collections.abc
import contextlib
import itertools
import warnings
from typing import Any
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pottery/nextid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down