Skip to content

Commit da32b9e

Browse files
committed
Codestyle changes
1 parent 74499a9 commit da32b9e

File tree

6 files changed

+70
-79
lines changed

6 files changed

+70
-79
lines changed

redis/connection.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from abc import abstractmethod
99
from itertools import chain
1010
from queue import Empty, Full, LifoQueue
11-
from time import time, sleep
11+
from time import sleep, time
1212
from typing import Any, Callable, List, Optional, Type, Union
1313
from urllib.parse import parse_qs, unquote, urlparse
1414

@@ -20,8 +20,8 @@
2020
CacheInterface,
2121
CacheToolsFactory,
2222
)
23-
from . import scheduler
2423

24+
from . import scheduler
2525
from ._parsers import Encoder, _HiredisParser, _RESP2Parser, _RESP3Parser
2626
from .backoff import NoBackoff
2727
from .credentials import CredentialProvider, UsernamePasswordCredentialProvider
@@ -1312,12 +1312,9 @@ def __init__(
13121312
if self.cache is not None and self._scheduler is not None:
13131313
self._hc_cancel_event = threading.Event()
13141314
self._hc_thread = self._scheduler.run_with_interval(
1315-
self._perform_health_check,
1316-
2,
1317-
self._hc_cancel_event
1315+
self._perform_health_check, 2, self._hc_cancel_event
13181316
)
13191317

1320-
13211318
def __repr__(self) -> (str, str):
13221319
return (
13231320
f"<{type(self).__module__}.{type(self).__name__}"

redis/scheduler.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,36 @@ def __init__(self, polling_period: float = 0.1):
1313
self.polling_period = polling_period
1414

1515
def run_with_interval(
16-
self,
17-
func: Callable[[threading.Event, ...], None],
18-
interval: float,
19-
cancel: threading.Event,
20-
args: tuple = (),
16+
self,
17+
func: Callable[[threading.Event, ...], None],
18+
interval: float,
19+
cancel: threading.Event,
20+
args: tuple = (),
2121
) -> threading.Thread:
2222
"""
2323
Run scheduled execution with given interval
2424
in a separate thread until cancel event won't be set.
2525
"""
2626
done = threading.Event()
27-
thread = threading.Thread(target=self._run_timer, args=(func, interval, (done, *args), done, cancel))
27+
thread = threading.Thread(
28+
target=self._run_timer, args=(func, interval, (done, *args), done, cancel)
29+
)
2830
thread.start()
2931
return thread
3032

3133
def _get_timer(
32-
self,
33-
func: Callable[[threading.Event, ...], None],
34-
interval: float,
35-
args: tuple
34+
self, func: Callable[[threading.Event, ...], None], interval: float, args: tuple
3635
) -> threading.Timer:
3736
timer = threading.Timer(interval=interval, function=func, args=args)
3837
return timer
3938

4039
def _run_timer(
41-
self,
42-
func: Callable[[threading.Event, ...], None],
43-
interval: float,
44-
args: tuple,
45-
done: threading.Event,
46-
cancel: threading.Event
40+
self,
41+
func: Callable[[threading.Event, ...], None],
42+
interval: float,
43+
args: tuple,
44+
done: threading.Event,
45+
cancel: threading.Event,
4746
):
4847
timer = self._get_timer(func, interval, args)
4948
timer.start()

tests/conftest.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@
88
from urllib.parse import urlparse
99

1010
import pytest
11-
from _pytest import unittest
12-
1311
import redis
12+
from _pytest import unittest
1413
from packaging.version import Version
1514
from redis import Sentinel
1615
from redis.backoff import NoBackoff
17-
from redis.cache import CacheConfiguration, EvictionPolicy, CacheFactoryInterface, CacheInterface
18-
from redis.connection import Connection, SSLConnection, parse_url, ConnectionPool, ConnectionInterface
16+
from redis.cache import (
17+
CacheConfiguration,
18+
CacheFactoryInterface,
19+
CacheInterface,
20+
EvictionPolicy,
21+
)
22+
from redis.connection import (
23+
Connection,
24+
ConnectionInterface,
25+
ConnectionPool,
26+
SSLConnection,
27+
parse_url,
28+
)
1929
from redis.exceptions import RedisClusterException
2030
from redis.retry import Retry
2131
from tests.ssl_utils import get_ssl_filename
@@ -544,9 +554,7 @@ def master_host(request):
544554
@pytest.fixture()
545555
def cache_conf() -> CacheConfiguration:
546556
return CacheConfiguration(
547-
cache_size=100,
548-
cache_ttl=20,
549-
cache_eviction=EvictionPolicy.TTL
557+
cache_size=100, cache_ttl=20, cache_eviction=EvictionPolicy.TTL
550558
)
551559

552560

tests/test_cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
import redis
66
from cachetools import LFUCache, LRUCache, TTLCache
7-
from redis.cache import CacheToolsAdapter, EvictionPolicy, CacheConfiguration
7+
from redis.cache import CacheConfiguration, CacheToolsAdapter, EvictionPolicy
88
from redis.utils import HIREDIS_AVAILABLE
99
from tests.conftest import _get_client, skip_if_resp_version
1010

@@ -1176,4 +1176,4 @@ def test_is_exceeds_max_size(self, cache_conf: CacheConfiguration):
11761176

11771177
def test_is_allowed_to_cache(self, cache_conf: CacheConfiguration):
11781178
assert cache_conf.is_allowed_to_cache("GET")
1179-
assert not cache_conf.is_allowed_to_cache("SET")
1179+
assert not cache_conf.is_allowed_to_cache("SET")

tests/test_connection.py

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
from unittest.mock import patch
55

66
import pytest
7-
from cachetools import TTLCache, LRUCache
8-
97
import redis
8+
from cachetools import LRUCache, TTLCache
109
from redis import ConnectionPool, Redis
1110
from redis._parsers import _HiredisParser, _RESP2Parser, _RESP3Parser
1211
from redis.backoff import NoBackoff
13-
from redis.cache import EvictionPolicy, CacheInterface, CacheToolsAdapter
12+
from redis.cache import CacheInterface, CacheToolsAdapter, EvictionPolicy
1413
from redis.connection import (
14+
CacheProxyConnection,
1515
Connection,
1616
SSLConnection,
1717
UnixDomainSocketConnection,
18-
parse_url, CacheProxyConnection,
18+
parse_url,
1919
)
20-
from redis.exceptions import ConnectionError, InvalidResponse, TimeoutError, RedisError
20+
from redis.exceptions import ConnectionError, InvalidResponse, RedisError, TimeoutError
2121
from redis.retry import Retry
2222
from redis.utils import HIREDIS_AVAILABLE
2323

@@ -353,48 +353,36 @@ def test_unix_socket_connection_failure():
353353

354354
class TestUnitConnectionPool:
355355

356-
@pytest.mark.parametrize("max_conn", (-1, 'str'), ids=("non-positive", "wrong type"))
356+
@pytest.mark.parametrize(
357+
"max_conn", (-1, "str"), ids=("non-positive", "wrong type")
358+
)
357359
def test_throws_error_on_incorrect_max_connections(self, max_conn):
358360
with pytest.raises(
359-
ValueError,
360-
match='"max_connections" must be a positive integer'
361+
ValueError, match='"max_connections" must be a positive integer'
361362
):
362363
ConnectionPool(
363364
max_connections=max_conn,
364365
)
365366

366367
def test_throws_error_on_cache_enable_in_resp2(self):
367368
with pytest.raises(
368-
RedisError,
369-
match="Client caching is only supported with RESP version 3"
369+
RedisError, match="Client caching is only supported with RESP version 3"
370370
):
371-
ConnectionPool(
372-
protocol=2,
373-
use_cache=True
374-
)
371+
ConnectionPool(protocol=2, use_cache=True)
375372

376373
def test_throws_error_on_incorrect_cache_implementation(self):
377-
with pytest.raises(
378-
ValueError,
379-
match="Cache must implement CacheInterface"
380-
):
381-
ConnectionPool(
382-
protocol=3,
383-
use_cache=True,
384-
cache=TTLCache(100, 20)
385-
)
374+
with pytest.raises(ValueError, match="Cache must implement CacheInterface"):
375+
ConnectionPool(protocol=3, use_cache=True, cache=TTLCache(100, 20))
386376

387377
def test_returns_custom_cache_implementation(self, mock_cache):
388-
connection_pool = ConnectionPool(
389-
protocol=3,
390-
use_cache=True,
391-
cache=mock_cache
392-
)
378+
connection_pool = ConnectionPool(protocol=3, use_cache=True, cache=mock_cache)
393379

394380
assert mock_cache == connection_pool.cache
395381
connection_pool.disconnect()
396382

397-
def test_creates_cache_with_custom_cache_factory(self, mock_cache_factory, mock_cache):
383+
def test_creates_cache_with_custom_cache_factory(
384+
self, mock_cache_factory, mock_cache
385+
):
398386
mock_cache_factory.get_cache.return_value = mock_cache
399387

400388
connection_pool = ConnectionPool(
@@ -403,7 +391,7 @@ def test_creates_cache_with_custom_cache_factory(self, mock_cache_factory, mock_
403391
cache_size=100,
404392
cache_ttl=20,
405393
cache_eviction=EvictionPolicy.TTL,
406-
cache_factory=mock_cache_factory
394+
cache_factory=mock_cache_factory,
407395
)
408396

409397
assert connection_pool.cache == mock_cache
@@ -415,7 +403,7 @@ def test_creates_cache_with_given_configuration(self, mock_cache):
415403
use_cache=True,
416404
cache_size=100,
417405
cache_ttl=20,
418-
cache_eviction=EvictionPolicy.TTL
406+
cache_eviction=EvictionPolicy.TTL,
419407
)
420408

421409
assert isinstance(connection_pool.cache, CacheInterface)
@@ -424,10 +412,7 @@ def test_creates_cache_with_given_configuration(self, mock_cache):
424412
connection_pool.disconnect()
425413

426414
def test_make_connection_proxy_connection_on_given_cache(self):
427-
connection_pool = ConnectionPool(
428-
protocol=3,
429-
use_cache=True
430-
)
415+
connection_pool = ConnectionPool(protocol=3, use_cache=True)
431416

432417
assert isinstance(connection_pool.make_connection(), CacheProxyConnection)
433418
connection_pool.disconnect()
@@ -436,16 +421,17 @@ def test_make_connection_proxy_connection_on_given_cache(self):
436421
class TestUnitCacheProxyConnection:
437422
def test_clears_cache_on_disconnect(self, mock_connection, cache_conf):
438423
cache = LRUCache(100)
439-
cache['key'] = 'value'
440-
assert cache['key'] == 'value'
424+
cache["key"] = "value"
425+
assert cache["key"] == "value"
441426

442427
mock_connection.disconnect.return_value = None
443-
mock_connection.retry = 'mock'
444-
mock_connection.host = 'mock'
445-
mock_connection.port = 'mock'
428+
mock_connection.retry = "mock"
429+
mock_connection.host = "mock"
430+
mock_connection.port = "mock"
446431

447-
proxy_connection = CacheProxyConnection(mock_connection, CacheToolsAdapter(cache), cache_conf)
432+
proxy_connection = CacheProxyConnection(
433+
mock_connection, CacheToolsAdapter(cache), cache_conf
434+
)
448435
proxy_connection.disconnect()
449436

450437
assert cache.currsize == 0
451-

tests/test_scheduler.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import time
33

44
import pytest
5-
65
from redis.scheduler import Scheduler
76

87

@@ -15,10 +14,10 @@ class TestScheduler:
1514
(0.1, 2, (0, 0)),
1615
],
1716
ids=[
18-
'small polling period (0.001s)',
19-
'large polling period (0.1s)',
20-
'interval larger than timeout - no execution',
21-
]
17+
"small polling period (0.001s)",
18+
"large polling period (0.1s)",
19+
"interval larger than timeout - no execution",
20+
],
2221
)
2322
def test_run_with_interval(self, polling_period, interval, expected_count):
2423
scheduler = Scheduler(polling_period=polling_period)
@@ -30,7 +29,9 @@ def callback(done: threading.Event):
3029
counter += 1
3130
done.set()
3231

33-
scheduler.run_with_interval(func=callback, interval=interval, cancel=cancel_event)
32+
scheduler.run_with_interval(
33+
func=callback, interval=interval, cancel=cancel_event
34+
)
3435
time.sleep(1)
3536
cancel_event.set()
3637
cancel_event.wait()

0 commit comments

Comments
 (0)