Skip to content

Commit

Permalink
fix: ruff config
Browse files Browse the repository at this point in the history
  • Loading branch information
Krukov committed Mar 10, 2024
1 parent 2139229 commit 052a775
Show file tree
Hide file tree
Showing 23 changed files with 70 additions and 94 deletions.
15 changes: 7 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: debug-statements

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
- id: prettier
stages: [commit]
#
# - repo: https://github.com/pre-commit/mirrors-prettier
# rev: v4.0.0-alpha.8
# hooks:
# - id: prettier
# stages: [commit]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.2
hooks:
- id: ruff
args: [--fix, --line-length=119]
args: [--fix]
- id: ruff-format
args: [--line-length=119]

- repo: local
hooks:
Expand Down
6 changes: 1 addition & 5 deletions cashews/_typing.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from __future__ import annotations

from datetime import timedelta
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Iterable, Type, TypeVar, Union
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Iterable, Protocol, Type, TypeVar, Union

if TYPE_CHECKING: # pragma: no cover
from . import Command
from .backends.interface import Backend

try:
from typing import Protocol
except ImportError: # 3.7 python
from typing_extensions import Protocol # type: ignore[assignment]

_TTLTypes = Union[int, float, str, timedelta, None]
TTL = Union[_TTLTypes, Callable[..., _TTLTypes]]
Expand Down
5 changes: 2 additions & 3 deletions cashews/backends/diskcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ async def set(
return await future

def _set(self, key: Key, value: Value, expire=None, exist=None):
if exist is not None:
if self._exists(key) is not exist:
return False
if exist is not None and self._exists(key) is not exist:
return False
if expire is None:
expire = self._get_expire(key)
expire = expire if expire not in [UNLIMITED, NOT_EXIST] else None
Expand Down
5 changes: 1 addition & 4 deletions cashews/backends/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ def is_disable(self, *cmds: Command) -> bool:
_disable = self._disable
if not cmds and _disable:
return True
for cmd in cmds:
if cmd in _disable:
return True
return False
return any(cmd in _disable for cmd in cmds)

def is_enable(self, *cmds: Command) -> bool:
return not self.is_disable(*cmds)
Expand Down
5 changes: 2 additions & 3 deletions cashews/backends/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ async def set(
expire: float | None = None,
exist: bool | None = None,
) -> bool:
if exist is not None:
if (key in self.store) is not exist:
return False
if exist is not None and (key in self.store) is not exist:
return False
self._set(key, value, expire)
return True

Expand Down
3 changes: 2 additions & 1 deletion cashews/backends/redis/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(
warnings.warn(
"`safe` property was renamed to `suppress` and will be removed in next release",
DeprecationWarning,
stacklevel=2,
)
suppress = safe

Expand Down Expand Up @@ -224,7 +225,7 @@ async def get(self, key: Key, default: Value | None = None) -> Value:

async def get_many(self, *keys: Key, default: Value | None = None) -> tuple[Value | None, ...]:
if not keys:
return tuple()
return ()
values = await self._client.mget(*keys)
if values is None:
return tuple([default] * len(keys))
Expand Down
14 changes: 7 additions & 7 deletions cashews/backends/redis/client_side.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
GET:
-> IN mem cache -> Y -> return
-> N -> in redis cache -> Y -> store in mem cache -> return
-> N -> compete -> store in mem and in redis -> notify others by channel to invalidate # noqa: E501
-> N -> compete -> store in mem and in redis
-> notify others by channel to invalidate
INVALIDATE:
Expand Down Expand Up @@ -82,9 +83,9 @@ async def init(self):
self._listen_task = asyncio.create_task(self._listen_invalidate_forever())
try:
await asyncio.wait_for(self._listen_started.wait(), timeout=self._kwargs["socket_timeout"])
except (TimeoutError, asyncio.TimeoutError):
except (TimeoutError, asyncio.TimeoutError) as exc:
if self._listen_task.done():
raise self._listen_task.exception()
raise self._listen_task.exception() from exc
self._listen_task.cancel()
raise
self.__is_init = True
Expand Down Expand Up @@ -176,7 +177,7 @@ async def set(

async def set_many(self, pairs: Mapping[Key, Value], expire: Optional[float] = None):
await self._local_cache.set_many(pairs, expire)
for key in pairs.keys():
for key in pairs:
await self._mark_as_recently_updated(key)
return await super().set_many(
{self._add_prefix(key): value for key, value in pairs.items()},
Expand Down Expand Up @@ -255,9 +256,8 @@ async def expire(self, key: Key, timeout: float):
return result

async def get_expire(self, key: Key) -> int:
if await self._local_cache.get_expire(key) > 0:
if self._listen_started.is_set():
return await self._local_cache.get_expire(key)
if await self._local_cache.get_expire(key) > 0 and self._listen_started.is_set():
return await self._local_cache.get_expire(key)
expire = await super().get_expire(self._add_prefix(key))
await self._local_cache.expire(key, expire)
return expire
Expand Down
12 changes: 7 additions & 5 deletions cashews/backends/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ async def set(
expire: Optional[float] = None,
exist: Optional[bool] = None,
) -> bool:
if exist is not None:
if await self._backend.exists(key) is not exist:
if await self._local_cache.exists(key) is not exist:
return False
if (
exist is not None
and await self._backend.exists(key) is not exist
and await self._local_cache.exists(key) is not exist
):
return False
self._to_delete.discard(key)
return await self._local_cache.set(key, value, expire, exist)

Expand Down Expand Up @@ -313,7 +315,7 @@ async def set(
return await super().set(key, value, expire=expire, exist=exist)

async def set_many(self, pairs: Mapping[Key, Value], expire: Optional[float] = None):
for key in pairs.keys():
for key in pairs:
await self._lock_updates(key)
res = await super().set_many(pairs, expire=expire)
return res
Expand Down
7 changes: 2 additions & 5 deletions cashews/contrib/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _to_disable(self, cache_control_value: str | None) -> tuple[Command, ...]:
return Command.GET, Command.SET
if cache_control_value and self._get_max_age(cache_control_value) == 0:
return Command.GET, Command.SET
return tuple()
return ()

@staticmethod
def _get_max_age(cache_control_value: str) -> int | None:
Expand Down Expand Up @@ -156,10 +156,7 @@ async def _get_etag(self, key: str) -> str:
data = await self._cache.get_raw(key)
expire = await self._cache.get_expire(key)
if not isinstance(data, bytes):
if isinstance(data, Response):
data = data.body
else:
data = DEFAULT_PICKLER.dumps(data)
data = data.body if isinstance(data, Response) else DEFAULT_PICKLER.dumps(data)
etag = blake2s(data).hexdigest()
await self._cache.set(etag, True, expire=expire)
return etag
Expand Down
5 changes: 1 addition & 4 deletions cashews/contrib/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ async def metrics_middleware(call, cmd: Command, backend: Backend, *args, **kwar
metric.labels(operation=cmd.value, backend_class=backend.__class__.__name__, tag=tag)
result = await call(*args, **kwargs)
if cmd is Command.GET:
if result is not kwargs["default"]:
op_result = "hit"
else:
op_result = "miss"
op_result = "hit" if result is not kwargs["default"] else "miss"
_HIT_MISS.labels(result=op_result, backend_class=backend.__class__.__name__, tag=tag).inc()
return result

Expand Down
5 changes: 2 additions & 3 deletions cashews/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ def _get_call_values(func: Callable, args: Args, kwargs: Kwargs):
if len(args) == 0:
_kwargs = {**kwargs}
for name, parameter in _get_func_signature(func).parameters.items():
if parameter.kind != inspect.Parameter.VAR_KEYWORD:
if name in _kwargs:
del _kwargs[name]
if parameter.kind != inspect.Parameter.VAR_KEYWORD and name in _kwargs:
del _kwargs[name]
return {**kwargs, _KWARGS: _kwargs}

signature = _get_func_signature(func).bind(*args, **kwargs)
Expand Down
8 changes: 2 additions & 6 deletions cashews/utils/_bitarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ def __init__(self, value, base=10):

def get(self, index: int, size: int = 1) -> int:
value = 0
bit_index = 0
for i in range(index * size, (index + 1) * size):
for bit_index, i in enumerate(range(index * size, (index + 1) * size)):
value |= ((self._value >> i) & 1) << bit_index
bit_index += 1
return value
Expand All @@ -26,10 +25,7 @@ def _set_bit_0(self, index):
self._value &= ~(1 << index)

def incr(self, index: int, size: int = 1, by: int = 1):
if by > 0:
by = min(by, 2**size - 1)
else:
by = max(by, -(2**size) - 1)
by = min(by, 2**size - 1) if by > 0 else max(by, -(2**size) - 1)
value = self.get(index, size)
value += by
value = min(max(0, value), 2**size - 1)
Expand Down
5 changes: 1 addition & 4 deletions cashews/utils/_bitarray_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ def _set_bit_0(self, index):
self._value[len(self._value) - index] = 0

def incr(self, index: int, size: int = 1, by: int = 1):
if by > 0:
by = min(by, 2**size - 1)
else:
by = max(by, -(2**size) - 1)
by = min(by, 2**size - 1) if by > 0 else max(by, -(2**size) - 1)
value = self.get(index, size)
value += by
value = min(max(0, value), 2**size - 1)
Expand Down
2 changes: 1 addition & 1 deletion cashews/wrapper/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async def set_many(self, pairs: Mapping[Key, Value], expire: TTL = None):
for key in pairs:
backend = self._get_backend(key)
backends.setdefault(backend, []).append(key)
for backend, keys in backends.items():
for keys in backends.values():
data = {key: pairs[key] for key in keys}
await self._with_middlewares(Command.SET_MANY, keys[0])(
pairs=data,
Expand Down
2 changes: 1 addition & 1 deletion cashews/wrapper/disable_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ def is_enable(self, *cmds: Command, prefix: str = "") -> bool:

@property
def is_full_disable(self) -> bool:
return all([backend.is_full_disable for backend, _ in self._backends.values()])
return all(backend.is_full_disable for backend, _ in self._backends.values())
10 changes: 2 additions & 8 deletions cashews/wrapper/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ def setup(
backend_class, params = settings_url_parse(settings_url)
params.update(kwargs)

if "disable" in params:
disable = params.pop("disable")
else:
disable = not params.pop("enable", True)
disable = params.pop("disable") if "disable" in params else not params.pop("enable", True)

backend = backend_class(**params)
if disable:
Expand Down Expand Up @@ -93,10 +90,7 @@ async def init(self, *args, **kwargs) -> None:

@property
def is_init(self) -> bool:
for backend, _ in self._backends.values():
if not backend.is_init:
return False
return True
return all(backend.is_init for backend, _ in self._backends.values())

async def close(self) -> None:
for backend, _ in self._backends.values():
Expand Down
5 changes: 2 additions & 3 deletions perf/bench.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import contextlib
import random
import time
import uuid
Expand Down Expand Up @@ -33,10 +34,8 @@ async def _get_latency(func, *args, **kwargs) -> float:


async def run(target, test, iters=1000):
try:
with contextlib.suppress(AttributeError, TypeError):
await target.init()
except (AttributeError, TypeError):
pass

method, key_gen, _options = test
_options = dict(_options)
Expand Down
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[tool.ruff]
exclude = ["venv/*","tox/*","specs/*",".venv/*"]
line-length = 119

[tool.ruff.lint]
select = ["E", "F", "B", "I", "SIM", "UP", "C4"]

[tool.ruff.lint.per-file-ignores]
"tests/**/*.py" = [
"B011", # asserts allowed in tests...
"B007", # asserts allowed in tests...
"B017",
"SIM105",
]
13 changes: 2 additions & 11 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = cashews
version = 7.0.1
version = 7.0.2
url = https://github.com/Krukov/cashews/
author = Dmitry Kryukov
author_email = [email protected]
Expand Down Expand Up @@ -29,8 +29,6 @@ python_requires = >=3.8
packages = find:
include_package_data = true
zip_safe = false
install_requires =
typing_extensions;python_version=='3.8'

[options.packages.find]
exclude =
Expand All @@ -56,12 +54,5 @@ lint =
types-redis
tests =
pytest
pytest-asyncio==0.23.4
pytest-asyncio==0.21.1
hypothesis

[tool.ruff]
exclude = venv/*,tox/*,specs/*
tool.ruff = 119

[tool.ruff.lint]
select = ["E", "F", "B", "I", "SIM"]
13 changes: 6 additions & 7 deletions tests/test_client_side_cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import contextlib

import pytest

Expand Down Expand Up @@ -133,14 +134,14 @@ async def test_simple_cmd_bcast_many(create_cache):
assert key in ("key:1", "key:2")
break
else:
assert False
raise AssertionError()

async for key, value in cache.get_match("key:*"):
assert key in ("key:1", "key:2")
assert value in ("test", "test2")
break
else:
assert False
raise AssertionError()

await local.clear()

Expand All @@ -160,10 +161,10 @@ async def test_simple_cmd_bcast_many(create_cache):
assert await local.get("key:1") is _empty_in_redis

async for _ in cache.scan("key:*"):
assert False
raise AssertionError()

async for _ in cache.get_match("key:*"):
assert False
raise AssertionError()

await cache.close()

Expand All @@ -176,10 +177,8 @@ async def test_unsafe_redis_down():
with pytest.raises(asyncio.TimeoutError):
await cache.init()

try:
with contextlib.suppress(asyncio.CancelledError):
await cache.close()
except asyncio.CancelledError:
pass


async def test_set_get_mem_overload(create_cache):
Expand Down
Loading

0 comments on commit 052a775

Please sign in to comment.