Skip to content

Commit c7be6cc

Browse files
committed
Enabled more rules and restored blind exceptino for lz4 compressor
1 parent 0abd91c commit c7be6cc

File tree

7 files changed

+46
-44
lines changed

7 files changed

+46
-44
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ repos:
99
- id: check-symlinks
1010
- id: debug-statements
1111

12-
- repo: https://github.com/psf/black
13-
rev: 23.9.1
14-
hooks:
15-
- id: black
16-
12+
# from readme - ruff with autofix must run before
13+
# other formatters, such as black
1714
- repo: https://github.com/astral-sh/ruff-pre-commit
1815
rev: v0.0.292
1916
hooks:
2017
- id: ruff
2118
args: [ --fix, --exit-non-zero-on-fix , --show-fixes]
19+
20+
- repo: https://github.com/psf/black
21+
rev: 23.9.1
22+
hooks:
23+
- id: black

.ruff.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ select = [
4949
"T10",
5050

5151
# rules from flake8-django
52-
# TODO: "DJ",
52+
"DJ",
5353

5454
# rules from flake8-errmsg
5555
"EM",
5656

5757
# rules from flake8-executable
58-
# TODO: "EXE",
58+
"EXE",
5959

6060
# rules from flake8-implicit-str-concat
6161
"ISC",
@@ -85,7 +85,7 @@ select = [
8585
"RSE",
8686

8787
# rules from flake8-return
88-
# TODO: "RET",
88+
"RET",
8989

9090
# rules from flake8-self
9191
# TODO: "SLF",
@@ -106,8 +106,7 @@ select = [
106106
# TODO: "ARG",
107107

108108
# rules from flake8-use-pathlib
109-
# could be enabled easily
110-
# TODO: "PTH",
109+
"PTH",
111110

112111
# removes unused noqa comments
113112
"RUF100",

django_redis/client/default.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ def get_client(
108108

109109
if show_index:
110110
return self._clients[index], index
111-
else:
112-
return self._clients[index]
111+
112+
return self._clients[index]
113113

114114
def connect(self, index: int = 0) -> Redis:
115115
"""
@@ -166,13 +166,13 @@ def set( # noqa: A003
166166
# not expire (in our case delete) the value if it exists.
167167
# Obviously expire not existent value is noop.
168168
return not self.has_key(key, version=version, client=client)
169-
else:
170-
# redis doesn't support negative timeouts in ex flags
171-
# so it seems that it's better to just delete the key
172-
# than to set it and than expire in a pipeline
173-
return bool(
174-
self.delete(key, client=client, version=version)
175-
)
169+
170+
# redis doesn't support negative timeouts in ex flags
171+
# so it seems that it's better to just delete the key
172+
# than to set it and than expire in a pipeline
173+
return bool(
174+
self.delete(key, client=client, version=version)
175+
)
176176

177177
return bool(client.set(nkey, nvalue, nx=nx, px=timeout, xx=xx))
178178
except _main_exceptions as e:
@@ -419,7 +419,7 @@ def delete_many(
419419
keys = [self.make_key(k, version=version) for k in keys]
420420

421421
if not keys:
422-
return
422+
return None
423423

424424
try:
425425
return client.delete(*keys)
@@ -459,8 +459,7 @@ def encode(self, value: Any) -> Union[bytes, Any]:
459459

460460
if isinstance(value, bool) or not isinstance(value, int):
461461
value = self._serializer.dumps(value)
462-
value = self._compressor.compress(value)
463-
return value
462+
return self._compressor.compress(value)
464463

465464
return value
466465

@@ -623,13 +622,13 @@ def ttl(
623622

624623
if t >= 0:
625624
return t
626-
elif t == -1:
625+
if t == -1:
627626
return None
628-
elif t == -2:
627+
if t == -2:
629628
return 0
630-
else:
631-
# Should never reach here
632-
return None
629+
630+
# Should never reach here
631+
return None
633632

634633
def pttl(self, key, version=None, client=None):
635634
"""
@@ -647,13 +646,13 @@ def pttl(self, key, version=None, client=None):
647646

648647
if t >= 0:
649648
return t
650-
elif t == -1:
649+
if t == -1:
651650
return None
652-
elif t == -2:
651+
if t == -2:
653652
return 0
654-
else:
655-
# Should never reach here
656-
return None
653+
654+
# Should never reach here
655+
return None
657656

658657
def has_key(
659658
self, key: Any, version: Optional[int] = None, client: Optional[Redis] = None
@@ -739,7 +738,7 @@ def make_pattern(
739738

740739
return CacheKey(self._backend.key_func(pattern, prefix, version_str))
741740

742-
def close(self, **kwargs):
741+
def close(self):
743742
close_flag = self._options.get(
744743
"CLOSE_CONNECTION",
745744
getattr(settings, "DJANGO_REDIS_CLOSE_CONNECTION", False),
@@ -774,7 +773,7 @@ def touch(
774773
key = self.make_key(key, version=version)
775774
if timeout is None:
776775
return bool(client.persist(key))
777-
else:
778-
# Convert to milliseconds
779-
timeout = int(timeout * 1000)
780-
return bool(client.pexpire(key, timeout))
776+
777+
# Convert to milliseconds
778+
timeout = int(timeout * 1000)
779+
return bool(client.pexpire(key, timeout))

django_redis/client/sharded.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def get_server_name(self, _key):
3737
g = self._findhash.match(key)
3838
if g is not None and len(g.groups()) > 0:
3939
key = g.groups()[0]
40-
name = self._ring.get_node(key)
41-
return name
40+
return self._ring.get_node(key)
4241

4342
def get_server(self, key):
4443
name = self.get_server_name(key)

django_redis/compressors/lz4.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from lz4.frame import compress as _compress
22
from lz4.frame import decompress as _decompress
33

4-
from .base import BaseCompressor
4+
from django_redis.compressors.base import BaseCompressor
5+
from django_redis.exceptions import CompressorError
56

67

78
class Lz4Compressor(BaseCompressor):
@@ -13,4 +14,7 @@ def compress(self, value: bytes) -> bytes:
1314
return value
1415

1516
def decompress(self, value: bytes) -> bytes:
16-
return _decompress(value)
17+
try:
18+
return _decompress(value)
19+
except Exception as e: # noqa: BLE001
20+
raise CompressorError from e

django_redis/pool.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ def connect(self, url: str) -> Redis:
6868
return a new connection.
6969
"""
7070
params = self.make_connection_params(url)
71-
connection = self.get_connection(params)
72-
return connection
71+
return self.get_connection(params)
7372

7473
def disconnect(self, connection):
7574
"""

django_redis/serializers/pickle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def setup_pickle_version(self, options) -> None:
2323
f" {pickle.HIGHEST_PROTOCOL}"
2424
)
2525
raise ImproperlyConfigured(error_message)
26-
except (ValueError, TypeError):
26+
except (ValueError, TypeError) as e:
2727
error_message = "PICKLE_VERSION value must be an integer"
2828
raise ImproperlyConfigured(error_message) from e
2929

0 commit comments

Comments
 (0)