Skip to content

Commit

Permalink
Tighten up type checking and type annotations (#609)
Browse files Browse the repository at this point in the history
* Tighten up type checking and type annotations

* Fix type annotation for Python < 3.10

* Loosen overly strict type checking options
  • Loading branch information
brainix authored Jan 30, 2022
1 parent 3d6e014 commit 8f33b93
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
8 changes: 8 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@

[mypy]
files = *.py,pottery/,tests/
disallow_incomplete_defs = True
disallow_untyped_calls = True
disallow_untyped_decorators = True
disallow_any_unimported = True
disallow_subclassing_any = True
warn_unreachable = True
warn_return_any = True
warn_redundant_casts = True



Expand Down
2 changes: 1 addition & 1 deletion pottery/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def cache_clear() -> None:

def _set_expiration(func: F) -> F:
@functools.wraps(func)
def wrapper(self, *args: Any, **kwargs: Any) -> Any:
def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
value = func(self, *args, **kwargs)
if self._timeout:
self._cache.redis.expire(self._cache.key, self._timeout) # Available since Redis 1.0.0
Expand Down
10 changes: 6 additions & 4 deletions pottery/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class RedisList(Base, collections.abc.MutableSequence):

def __slice_to_indices(self, slice_or_index: slice | int) -> range:
if isinstance(slice_or_index, slice):
start, stop, step = cast(slice, slice_or_index).indices(len(self))
start, stop, step = slice_or_index.indices(len(self))
elif isinstance(slice_or_index, int):
start = cast(int, slice_or_index)
stop = cast(int, slice_or_index) + 1
start = slice_or_index
stop = slice_or_index + 1
step = 1
else:
raise TypeError(
Expand Down Expand Up @@ -146,14 +146,16 @@ def __getitem__(self, index: slice | int) -> Any:
return value

@_raise_on_error
def __setitem__(self, index: int, value: JSONTypes) -> None: # type: ignore
def __setitem__(self, index: slice | int, value: JSONTypes) -> None: # type: ignore
'l.__setitem__(index, value) <==> l[index] = value. O(n)'
with self._watch() as pipeline:
if isinstance(index, slice):
warnings.warn(
cast(str, InefficientAccessWarning.__doc__),
InefficientAccessWarning,
)
if not isinstance(value, Iterable):
raise TypeError('can only assign an iterable')
encoded_values = [self._encode(value) for value in value]
indices = self.__slice_to_indices(index)
pipeline.multi() # Available since Redis 1.2.0
Expand Down
3 changes: 2 additions & 1 deletion pottery/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from typing import Callable
from typing import Dict
from typing import List
from typing import Union
from typing import cast

# TODO: When we drop support for Python 3.7, change the following import to:
Expand Down Expand Up @@ -55,7 +56,7 @@ def _default(self: Any, obj: Any) -> Dict[str, Any] | List[Any] | str:
)
method = methods[0] if methods else _default.default # type: ignore
return_value = method(obj) # type: ignore
return return_value
return cast(Union[Dict[str, Any], List[Any], str], return_value)

import json # isort: skip
_default.default = json.JSONEncoder().default # type: ignore
Expand Down
8 changes: 4 additions & 4 deletions pottery/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def contains_many(self, *values: JSONTypes) -> Generator[bool, None, None]:
encoded_values.append(encoded_value)

# Available since Redis 6.2.0:
for is_member in self.redis.smismember(self.key, encoded_values):
for is_member in self.redis.smismember(self.key, encoded_values): # type: ignore
yield bool(is_member)

def __iter__(self) -> Generator[JSONTypes, None, None]:
Expand Down Expand Up @@ -197,7 +197,7 @@ def __set_op(self,
with self._watch(*others):
set_ = self.__to_set()
method = getattr(set_, set_method)
return method(*others)
return cast(Set[Any], method(*others))

# Where does this method come from?
def issubset(self, other: Iterable[Any]) -> bool:
Expand All @@ -222,7 +222,7 @@ def __sub_or_super(self,
if not isinstance(other, collections.abc.Set):
other = frozenset(other)
method = getattr(self, set_method)
return method(other)
return cast(bool, method(other))

# Where does this method come from?
def symmetric_difference(self, other: Iterable[Any]) -> NoReturn:
Expand Down Expand Up @@ -283,7 +283,7 @@ def __update(self,
def symmetric_difference_update(self, other: Iterable[JSONTypes]) -> NoReturn:
raise NotImplementedError

def to_set(self):
def to_set(self) -> Set[JSONTypes]:
'Convert a RedisSet into a plain Python set.'
return set(self)

Expand Down

0 comments on commit 8f33b93

Please sign in to comment.