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

Write union types as X | Y #614

Merged
merged 14 commits into from
Feb 1, 2022
14 changes: 8 additions & 6 deletions pottery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
# --------------------------------------------------------------------------- #


# TODO: When we drop support for Python 3.9, remove the following import. We
# only need it for X | Y union type annotations as of 2022-01-29.
from __future__ import annotations

import abc
import collections
import contextlib
Expand All @@ -31,8 +35,6 @@
from typing import Generator
from typing import Iterable
from typing import List
from typing import Mapping
from typing import Optional
from typing import Tuple
from typing import cast

Expand Down Expand Up @@ -95,8 +97,8 @@ class _Common:

def __init__(self,
*,
redis: Optional[Redis] = None,
key: Optional[str] = None,
redis: Redis | None = None,
key: str | None = None,
) -> None:
self.redis = cast(Redis, redis)
self.key = cast(str, key)
Expand All @@ -118,7 +120,7 @@ def redis(self) -> Redis:
return self.__redis

@redis.setter
def redis(self, value: Optional[Redis]) -> None:
def redis(self, value: Redis | None) -> None:
self.__redis = _default_redis if value is None else value

@property
Expand Down Expand Up @@ -329,7 +331,7 @@ def key(self, value: str) -> None:
self.__key = f'{self.KEY_PREFIX}:{value}'

def _check_enough_masters_up(self,
raise_on_redis_errors: Optional[bool],
raise_on_redis_errors: bool | None,
redis_errors: List[RedisError],
) -> None:
if raise_on_redis_errors is None:
Expand Down
15 changes: 7 additions & 8 deletions pottery/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from typing import Hashable
from typing import Iterable
from typing import NamedTuple
from typing import Optional
from typing import TypeVar
from typing import cast

Expand Down Expand Up @@ -60,7 +59,7 @@ class CacheInfo(NamedTuple):
'''
hits: int = 0
misses: int = 0
maxsize: Optional[int] = None
maxsize: int | None = None
currsize: int = 0


Expand All @@ -73,9 +72,9 @@ def _arg_hash(*args: Hashable, **kwargs: Hashable) -> int:


def redis_cache(*, # NoQA: C901
redis: Optional[Redis] = None,
key: Optional[str] = None,
timeout: Optional[int] = _DEFAULT_TIMEOUT,
redis: Redis | None = None,
key: str | None = None,
timeout: int | None = _DEFAULT_TIMEOUT,
) -> Callable[[F], F]:
'''Redis-backed caching decorator with an API like functools.lru_cache().

Expand Down Expand Up @@ -219,11 +218,11 @@ class CachedOrderedDict(collections.OrderedDict):
@_set_expiration
def __init__(self,
*,
redis_client: Optional[Redis] = None,
redis_key: Optional[str] = None,
redis_client: Redis | None = None,
redis_key: str | None = None,
dict_keys: Iterable[JSONTypes] = tuple(),
num_tries: int = _NUM_TRIES,
timeout: Optional[int] = _DEFAULT_TIMEOUT,
timeout: int | None = _DEFAULT_TIMEOUT,
) -> None:
self._num_tries = num_tries
self._timeout = timeout
Expand Down
3 changes: 1 addition & 2 deletions pottery/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from typing import Callable
from typing import Iterable
from typing import List
from typing import Optional
from typing import Tuple
from typing import Union
from typing import cast
Expand Down Expand Up @@ -251,7 +250,7 @@ def __iand__(self, other: Counter[JSONTypes]) -> Counter[JSONTypes]:
return self.__iset_op(other, method=int.__lt__)

def most_common(self,
n: Optional[int] = None,
n: int | None = None,
) -> List[Tuple[JSONTypes, int]]:
counter = self.__to_counter()
return counter.most_common(n=n)
Expand Down
9 changes: 4 additions & 5 deletions pottery/deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import collections
import warnings
from typing import Iterable
from typing import Optional
from typing import Tuple
from typing import cast

Expand All @@ -44,10 +43,10 @@ class RedisDeque(RedisList, collections.deque): # type: ignore

def __init__(self,
iterable: Iterable[JSONTypes] = tuple(),
maxlen: Optional[int] = None,
maxlen: int | None = None,
*,
redis: Optional[Redis] = None,
key: Optional[str] = None,
redis: Redis | None = None,
key: str | None = None,
) -> None:
'Initialize the RedisDeque. O(n)'
if maxlen is not None and not isinstance(maxlen, int):
Expand All @@ -73,7 +72,7 @@ def _populate(self,
super()._populate(pipeline, iterable)

@property
def maxlen(self) -> Optional[int]:
def maxlen(self) -> int | None:
return self._maxlen

@maxlen.setter
Expand Down
9 changes: 6 additions & 3 deletions pottery/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
# --------------------------------------------------------------------------- #


# TODO: When we drop support for Python 3.9, remove the following import. We
# only need it for X | Y union type annotations as of 2022-01-29.
from __future__ import annotations

import collections.abc
import itertools
import warnings
Expand All @@ -24,7 +28,6 @@
from typing import Generator
from typing import Iterable
from typing import Mapping
from typing import Optional
from typing import Tuple
from typing import Union
from typing import cast
Expand All @@ -51,8 +54,8 @@ class RedisDict(Base, Iterable_, collections.abc.MutableMapping):
def __init__(self,
arg: InitArg = tuple(),
*,
redis: Optional[Redis] = None,
key: Optional[str] = None,
redis: Redis | None = None,
key: str | None = None,
**kwargs: JSONTypes,
) -> None:
'Initialize the RedisDict. O(n)'
Expand Down
7 changes: 5 additions & 2 deletions pottery/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
# --------------------------------------------------------------------------- #


# TODO: When we drop support for Python 3.9, remove the following import. We
# only need it for X | Y union type annotations as of 2022-01-29.
from __future__ import annotations

from dataclasses import dataclass
from queue import Empty
from typing import Iterable
from typing import Optional

from redis import Redis
from redis import RedisError
Expand All @@ -30,7 +33,7 @@ class PotteryError(Exception):
'Base exception class for Pottery containers.'

redis: Redis
key: Optional[str] = None
key: str | None = None

class KeyExistsError(PotteryError):
'Initializing a container on a Redis key that already exists.'
Expand Down
11 changes: 7 additions & 4 deletions pottery/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
# --------------------------------------------------------------------------- #


# TODO: When we drop support for Python 3.9, remove the following import. We
# only need it for X | Y union type annotations as of 2022-01-29.
from __future__ import annotations

import concurrent.futures
from types import TracebackType
from typing import Optional
from typing import Type
from typing import overload

Expand Down Expand Up @@ -69,9 +72,9 @@ def __exit__(self,
raise NotImplementedError

def __exit__(self,
exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
exc_traceback: Optional[TracebackType],
exc_type: Type[BaseException] | None,
exc_value: BaseException | None,
exc_traceback: TracebackType | None,
) -> Literal[False]:
self.shutdown(wait=False)
return False
9 changes: 4 additions & 5 deletions pottery/hyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from typing import Generator
from typing import Iterable
from typing import List
from typing import Optional
from typing import cast

from redis import Redis
Expand Down Expand Up @@ -107,8 +106,8 @@ class HyperLogLog(Base):
def __init__(self,
iterable: Iterable[RedisValues] = frozenset(),
*,
redis: Optional[Redis] = None,
key: Optional[str] = None,
redis: Redis | None = None,
key: str | None = None,
) -> None:
'''Initialize the HyperLogLog. O(n)

Expand Down Expand Up @@ -149,8 +148,8 @@ def update(self, *objs: HyperLogLog | Iterable[RedisValues]) -> None:

def union(self,
*objs: Iterable[RedisValues],
redis: Optional[Redis] = None,
key: Optional[str] = None,
redis: Redis | None = None,
key: str | None = None,
) -> HyperLogLog:
new_hll = self.__class__(redis=redis, key=key)
new_hll.update(self, *objs)
Expand Down
9 changes: 4 additions & 5 deletions pottery/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from typing import Callable
from typing import Iterable
from typing import List
from typing import Optional
from typing import cast

from redis import Redis
Expand Down Expand Up @@ -82,8 +81,8 @@ def __slice_to_indices(self, slice_or_index: slice | int) -> range:
def __init__(self,
iterable: Iterable[JSONTypes] = tuple(),
*,
redis: Optional[Redis] = None,
key: Optional[str] = None,
redis: Redis | None = None,
key: str | None = None,
) -> None:
'Initialize the RedisList. O(n)'
super().__init__(redis=redis, key=key)
Expand Down Expand Up @@ -251,7 +250,7 @@ def _insert(self,

# Methods required for Raj's sanity:

def sort(self, *, key: Optional[str] = None, reverse: bool = False) -> None:
def sort(self, *, key: str | None = None, reverse: bool = False) -> None:
'Sort the RedisList in place. O(n)'
if key is not None:
raise NotImplementedError('sorting by key not implemented')
Expand Down Expand Up @@ -321,7 +320,7 @@ def extend(self, values: Iterable[JSONTypes]) -> None:
__extend = extend

# From collections.abc.MutableSequence:
def pop(self, index: Optional[int] = None) -> JSONTypes:
def pop(self, index: int | None = None) -> JSONTypes:
with self._watch() as pipeline:
len_ = len(self)
if index and index >= len_:
Expand Down
3 changes: 1 addition & 2 deletions pottery/nextid.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from typing import ClassVar
from typing import Iterable
from typing import List
from typing import Optional
from typing import Tuple
from typing import Type
from typing import cast
Expand All @@ -61,7 +60,7 @@ class _Scripts(Primitive):

__slots__: Tuple[str, ...] = tuple()

_set_id_script: ClassVar[Optional[Script]] = None
_set_id_script: ClassVar[Script | None] = None

def __init__(self,
*,
Expand Down
9 changes: 6 additions & 3 deletions pottery/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
# --------------------------------------------------------------------------- #


# TODO: When we drop support for Python 3.9, remove the following import. We
# only need it for X | Y union type annotations as of 2022-01-29.
from __future__ import annotations

import math
import random
import time
from typing import ClassVar
from typing import Optional
from typing import Tuple
from typing import cast

Expand Down Expand Up @@ -61,7 +64,7 @@ def empty(self) -> bool:
def put(self,
item: JSONTypes,
block: bool = True,
timeout: Optional[float] = None,
timeout: float | None = None,
) -> None:
'''Put the item on the queue. O(1)

Expand All @@ -84,7 +87,7 @@ def put_nowait(self, item: JSONTypes) -> None:

def get(self,
block: bool = True,
timeout: Optional[float] = None,
timeout: float | None = None,
) -> JSONTypes:
'''Remove and return an item from the queue. O(1)

Expand Down
Loading