Skip to content

Commit 79a4ab0

Browse files
committed
Apply ruff format fixes
1 parent 2542275 commit 79a4ab0

File tree

11 files changed

+147
-50
lines changed

11 files changed

+147
-50
lines changed

django_redis/cache.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414

1515
def omit_exception(
16-
method: Optional[Callable] = None, return_value: Optional[Any] = None
16+
method: Optional[Callable] = None,
17+
return_value: Optional[Any] = None,
1718
):
1819
"""
1920
Simple decorator that intercepts connection
@@ -44,12 +45,15 @@ def __init__(self, server: str, params: dict[str, Any]) -> None:
4445
self._server = server
4546
self._params = params
4647
self._default_scan_itersize = getattr(
47-
settings, "DJANGO_REDIS_SCAN_ITERSIZE", 10
48+
settings,
49+
"DJANGO_REDIS_SCAN_ITERSIZE",
50+
10,
4851
)
4952

5053
options = params.get("OPTIONS", {})
5154
self._client_cls = options.get(
52-
"CLIENT_CLASS", "django_redis.client.DefaultClient"
55+
"CLIENT_CLASS",
56+
"django_redis.client.DefaultClient",
5357
)
5458
self._client_cls = import_string(self._client_cls)
5559
self._client = None
@@ -59,7 +63,9 @@ def __init__(self, server: str, params: dict[str, Any]) -> None:
5963
getattr(settings, "DJANGO_REDIS_IGNORE_EXCEPTIONS", False),
6064
)
6165
self._log_ignored_exceptions = getattr(
62-
settings, "DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS", False
66+
settings,
67+
"DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS",
68+
False,
6369
)
6470
self.logger = (
6571
logging.getLogger(getattr(settings, "DJANGO_REDIS_LOGGER", __name__))

django_redis/client/default.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(self, server, params: dict[str, Any], backend: BaseCache) -> None:
4848

4949
self.reverse_key = get_key_func(
5050
params.get("REVERSE_KEY_FUNCTION")
51-
or "django_redis.util.default_reverse_key"
51+
or "django_redis.util.default_reverse_key",
5252
)
5353

5454
if not self._server:
@@ -63,12 +63,14 @@ def __init__(self, server, params: dict[str, Any], backend: BaseCache) -> None:
6363
self._replica_read_only = self._options.get("REPLICA_READ_ONLY", True)
6464

6565
serializer_path = self._options.get(
66-
"SERIALIZER", "django_redis.serializers.pickle.PickleSerializer"
66+
"SERIALIZER",
67+
"django_redis.serializers.pickle.PickleSerializer",
6768
)
6869
serializer_cls = import_string(serializer_path)
6970

7071
compressor_path = self._options.get(
71-
"COMPRESSOR", "django_redis.compressors.identity.IdentityCompressor"
72+
"COMPRESSOR",
73+
"django_redis.compressors.identity.IdentityCompressor",
7274
)
7375
compressor_cls = import_string(compressor_path)
7476

@@ -83,13 +85,16 @@ def __contains__(self, key: KeyT) -> bool:
8385
def _has_compression_enabled(self) -> bool:
8486
return (
8587
self._options.get(
86-
"COMPRESSOR", "django_redis.compressors.identity.IdentityCompressor"
88+
"COMPRESSOR",
89+
"django_redis.compressors.identity.IdentityCompressor",
8790
)
8891
!= "django_redis.compressors.identity.IdentityCompressor"
8992
)
9093

9194
def get_next_client_index(
92-
self, write: bool = True, tried: Optional[list[int]] = None
95+
self,
96+
write: bool = True,
97+
tried: Optional[list[int]] = None,
9398
) -> int:
9499
"""
95100
Return a next index for read client. This function implements a default
@@ -305,7 +310,10 @@ def get(
305310
return self.decode(value)
306311

307312
def persist(
308-
self, key: KeyT, version: Optional[int] = None, client: Optional[Redis] = None
313+
self,
314+
key: KeyT,
315+
version: Optional[int] = None,
316+
client: Optional[Redis] = None,
309317
) -> bool:
310318
if client is None:
311319
client = self.get_client(write=True)
@@ -517,7 +525,9 @@ def encode(self, value: EncodableT) -> Union[bytes, int]:
517525
return value
518526

519527
def _decode_iterable_result(
520-
self, result: Any, covert_to_set: bool = True
528+
self,
529+
result: Any,
530+
covert_to_set: bool = True,
521531
) -> Union[list[Any], None, Any]:
522532
if result is None:
523533
return None
@@ -672,7 +682,10 @@ def decr(
672682
return self._incr(key=key, delta=-delta, version=version, client=client)
673683

674684
def ttl(
675-
self, key: KeyT, version: Optional[int] = None, client: Optional[Redis] = None
685+
self,
686+
key: KeyT,
687+
version: Optional[int] = None,
688+
client: Optional[Redis] = None,
676689
) -> Optional[int]:
677690
"""
678691
Executes TTL redis command and return the "time-to-live" of specified key.
@@ -698,7 +711,10 @@ def ttl(
698711
return None
699712

700713
def pttl(
701-
self, key: KeyT, version: Optional[int] = None, client: Optional[Redis] = None
714+
self,
715+
key: KeyT,
716+
version: Optional[int] = None,
717+
client: Optional[Redis] = None,
702718
) -> Optional[int]:
703719
"""
704720
Executes PTTL redis command and return the "time-to-live" of specified key.
@@ -724,7 +740,10 @@ def pttl(
724740
return None
725741

726742
def has_key(
727-
self, key: KeyT, version: Optional[int] = None, client: Optional[Redis] = None
743+
self,
744+
key: KeyT,
745+
version: Optional[int] = None,
746+
client: Optional[Redis] = None,
728747
) -> bool:
729748
"""
730749
Test if key exists.
@@ -759,7 +778,10 @@ def iter_keys(
759778
yield self.reverse_key(item.decode())
760779

761780
def keys(
762-
self, search: str, version: Optional[int] = None, client: Optional[Redis] = None
781+
self,
782+
search: str,
783+
version: Optional[int] = None,
784+
client: Optional[Redis] = None,
763785
) -> list[Any]:
764786
"""
765787
Execute KEYS command and return matched results.
@@ -778,7 +800,10 @@ def keys(
778800
raise ConnectionInterrupted(connection=client) from e
779801

780802
def make_key(
781-
self, key: KeyT, version: Optional[int] = None, prefix: Optional[str] = None
803+
self,
804+
key: KeyT,
805+
version: Optional[int] = None,
806+
prefix: Optional[str] = None,
782807
) -> KeyT:
783808
if isinstance(key, CacheKey):
784809
return key
@@ -792,7 +817,10 @@ def make_key(
792817
return CacheKey(self._backend.key_func(key, prefix, version))
793818

794819
def make_pattern(
795-
self, pattern: str, version: Optional[int] = None, prefix: Optional[str] = None
820+
self,
821+
pattern: str,
822+
version: Optional[int] = None,
823+
prefix: Optional[str] = None,
796824
) -> str:
797825
if isinstance(pattern, CacheKey):
798826
return pattern

django_redis/client/herd.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ def set(
9090
real_timeout = timeout + self._herd_timeout
9191

9292
return super().set(
93-
key, packed, timeout=real_timeout, version=version, client=client, nx=nx
93+
key,
94+
packed,
95+
timeout=real_timeout,
96+
version=version,
97+
client=client,
98+
nx=nx,
9499
)
95100

96101
def get(self, key, default=None, version=None, client=None):
@@ -129,7 +134,12 @@ def get_many(self, keys, version=None, client=None):
129134
return recovered_data
130135

131136
def set_many(
132-
self, data, timeout=DEFAULT_TIMEOUT, version=None, client=None, herd=True
137+
self,
138+
data,
139+
timeout=DEFAULT_TIMEOUT,
140+
version=None,
141+
client=None,
142+
herd=True,
133143
):
134144
"""
135145
Set a bunch of values in the cache at once from a dict of key/value

django_redis/client/sharded.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None, client=None):
5353
client = self.get_server(key)
5454

5555
return super().add(
56-
key=key, value=value, version=version, client=client, timeout=timeout
56+
key=key,
57+
value=value,
58+
version=version,
59+
client=client,
60+
timeout=timeout,
5761
)
5862

5963
def get(self, key, default=None, version=None, client=None):
@@ -306,7 +310,12 @@ def keys(self, search, version=None):
306310
return [self.reverse_key(k.decode()) for k in keys]
307311

308312
def delete_pattern(
309-
self, pattern, version=None, client=None, itersize=None, prefix=None
313+
self,
314+
pattern,
315+
version=None,
316+
client=None,
317+
itersize=None,
318+
prefix=None,
310319
):
311320
"""
312321
Remove all keys matching pattern.
@@ -420,7 +429,11 @@ def sscan(
420429
key = self.make_key(key, version=version)
421430
client = self.get_server(key)
422431
return super().sscan(
423-
key=key, match=match, count=count, version=version, client=client
432+
key=key,
433+
match=match,
434+
count=count,
435+
version=version,
436+
client=client,
424437
)
425438

426439
def sscan_iter(
@@ -435,7 +448,11 @@ def sscan_iter(
435448
key = self.make_key(key, version=version)
436449
client = self.get_server(key)
437450
return super().sscan_iter(
438-
key=key, match=match, count=count, version=version, client=client
451+
key=key,
452+
match=match,
453+
count=count,
454+
version=version,
455+
client=client,
439456
)
440457

441458
def srandmember(

django_redis/pool.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class ConnectionFactory:
1919

2020
def __init__(self, options):
2121
pool_cls_path = options.get(
22-
"CONNECTION_POOL_CLASS", "redis.connection.ConnectionPool"
22+
"CONNECTION_POOL_CLASS",
23+
"redis.connection.ConnectionPool",
2324
)
2425
self.pool_cls = import_string(pool_cls_path)
2526
self.pool_cls_kwargs = options.get("CONNECTION_POOL_KWARGS", {})
@@ -87,7 +88,8 @@ def get_connection(self, params):
8788
"""
8889
pool = self.get_or_create_connection_pool(params)
8990
return self.redis_client_cls(
90-
connection_pool=pool, **self.redis_client_cls_kwargs
91+
connection_pool=pool,
92+
**self.redis_client_cls_kwargs,
9193
)
9294

9395
def get_parser_cls(self):
@@ -132,7 +134,8 @@ class SentinelConnectionFactory(ConnectionFactory):
132134
def __init__(self, options):
133135
# allow overriding the default SentinelConnectionPool class
134136
options.setdefault(
135-
"CONNECTION_POOL_CLASS", "redis.sentinel.SentinelConnectionPool"
137+
"CONNECTION_POOL_CLASS",
138+
"redis.sentinel.SentinelConnectionPool",
136139
)
137140
super().__init__(options)
138141

@@ -175,11 +178,13 @@ def get_connection_pool(self, params):
175178
new_query = urlencode(query_params, doseq=True)
176179

177180
new_url = urlunparse(
178-
(url.scheme, url.netloc, url.path, url.params, new_query, url.fragment)
181+
(url.scheme, url.netloc, url.path, url.params, new_query, url.fragment),
179182
)
180183

181184
cp_params.update(
182-
service_name=url.hostname, sentinel_manager=self._sentinel, url=new_url
185+
service_name=url.hostname,
186+
sentinel_manager=self._sentinel,
187+
url=new_url,
183188
)
184189

185190
return super().get_connection_pool(cp_params)

tests/test_backend.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ def test_delete_pattern_with_settings_default_scan_count(
523523
cache.delete_pattern("*foo-a*")
524524

525525
client_mock.delete_pattern.assert_called_once_with(
526-
"*foo-a*", itersize=expected_count
526+
"*foo-a*",
527+
itersize=expected_count,
527528
)
528529

529530
def test_close(self, cache: RedisCache, settings: SettingsWrapper):
@@ -905,7 +906,8 @@ def test_sdiffstore_with_keys_version(self, cache: RedisCache):
905906
assert cache.smembers("foo3") == {"bar1"}
906907

907908
def test_sdiffstore_with_different_keys_versions_without_initial_set_in_version(
908-
self, cache: RedisCache
909+
self,
910+
cache: RedisCache,
909911
):
910912
if isinstance(cache.client, ShardClient):
911913
pytest.skip("ShardClient doesn't support get_client")
@@ -915,7 +917,8 @@ def test_sdiffstore_with_different_keys_versions_without_initial_set_in_version(
915917
assert cache.sdiffstore("foo3", "foo1", "foo2", version_keys=2) == 0
916918

917919
def test_sdiffstore_with_different_keys_versions_with_initial_set_in_version(
918-
self, cache: RedisCache
920+
self,
921+
cache: RedisCache,
919922
):
920923
if isinstance(cache.client, ShardClient):
921924
pytest.skip("ShardClient doesn't support get_client")
@@ -1000,7 +1003,7 @@ def test_sscan_iter(self, cache: RedisCache):
10001003
def test_sscan_iter_with_match(self, cache: RedisCache):
10011004
if cache.client._has_compression_enabled():
10021005
pytest.skip(
1003-
"Compression is enabled, sscan_iter with match is not supported"
1006+
"Compression is enabled, sscan_iter with match is not supported",
10041007
)
10051008
cache.sadd("foo", "bar1", "bar2", "zoo")
10061009
items = cache.sscan_iter("foo", match="bar*")

tests/test_cache_options.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def test_get_django_omit_exceptions_many_returns_default_arg(
3838

3939

4040
def test_get_django_omit_exceptions(
41-
caplog: LogCaptureFixture, ignore_exceptions_cache: RedisCache
41+
caplog: LogCaptureFixture,
42+
ignore_exceptions_cache: RedisCache,
4243
):
4344
assert ignore_exceptions_cache._ignore_exceptions is True
4445
assert ignore_exceptions_cache._log_ignored_exceptions is True
@@ -92,7 +93,9 @@ def with_prefix_cache() -> Iterable[RedisCache]:
9293

9394
class TestDjangoRedisCacheEscapePrefix:
9495
def test_delete_pattern(
95-
self, key_prefix_cache: RedisCache, with_prefix_cache: RedisCache
96+
self,
97+
key_prefix_cache: RedisCache,
98+
with_prefix_cache: RedisCache,
9699
):
97100
key_prefix_cache.set("a", "1")
98101
with_prefix_cache.set("b", "2")
@@ -101,7 +104,9 @@ def test_delete_pattern(
101104
assert with_prefix_cache.get("b") == "2"
102105

103106
def test_iter_keys(
104-
self, key_prefix_cache: RedisCache, with_prefix_cache: RedisCache
107+
self,
108+
key_prefix_cache: RedisCache,
109+
with_prefix_cache: RedisCache,
105110
):
106111
if isinstance(key_prefix_cache.client, ShardClient):
107112
pytest.skip("ShardClient doesn't support iter_keys")

0 commit comments

Comments
 (0)