Skip to content

Commit ce6749d

Browse files
committed
Accept health_check_interval constructor arg
More generally, try to use introspection on redis.Redis to gracefully support whatever arguments it does. It still need a list of which arguments to pass through to the connection pool.
1 parent 9be200e commit ce6749d

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

fakeredis/_server.py

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import hashlib
3+
import inspect
34
import itertools
45
import logging
56
import math
@@ -2740,58 +2741,50 @@ def repr_pieces(self):
27402741

27412742

27422743
class FakeRedisMixin:
2743-
def __init__(self, host='localhost', port=6379,
2744-
db=0, password=None, socket_timeout=None,
2745-
socket_connect_timeout=None,
2746-
socket_keepalive=None, socket_keepalive_options=None,
2747-
connection_pool=None, unix_socket_path=None,
2748-
encoding='utf-8', encoding_errors='strict',
2749-
charset=None, errors=None,
2750-
decode_responses=False, retry_on_timeout=False,
2751-
ssl=False, ssl_keyfile=None, ssl_certfile=None,
2752-
ssl_cert_reqs=None, ssl_ca_certs=None,
2753-
max_connections=None, server=None,
2754-
connected=True):
2755-
if not connection_pool:
2744+
def __init__(self, *args, server=None, connected=True, **kwargs):
2745+
# Interpret the positional and keyword arguments according to the
2746+
# version of redis in use.
2747+
sig = inspect.signature(redis.Redis)
2748+
bound = sig.bind(*args, **kwargs)
2749+
bound.apply_defaults()
2750+
if not bound.arguments['connection_pool']:
2751+
charset = bound.arguments['charset']
2752+
errors = bound.arguments['errors']
27562753
# Adapted from redis-py
27572754
if charset is not None:
27582755
warnings.warn(DeprecationWarning(
27592756
'"charset" is deprecated. Use "encoding" instead'))
2760-
encoding = charset
2757+
bound.arguments['encoding'] = charset
27612758
if errors is not None:
27622759
warnings.warn(DeprecationWarning(
27632760
'"errors" is deprecated. Use "encoding_errors" instead'))
2764-
encoding_errors = errors
2761+
bound.arguments['encoding_errors'] = errors
27652762

27662763
if server is None:
27672764
server = FakeServer()
27682765
server.connected = connected
27692766
kwargs = {
2770-
'db': db,
2771-
'password': password,
2772-
'encoding': encoding,
2773-
'encoding_errors': encoding_errors,
2774-
'decode_responses': decode_responses,
2775-
'max_connections': max_connections,
27762767
'connection_class': FakeConnection,
27772768
'server': server
27782769
}
2779-
connection_pool = redis.connection.ConnectionPool(**kwargs)
2780-
# These need to be passed by name due to
2781-
# https://github.com/andymccurdy/redis-py/issues/1276
2782-
super().__init__(
2783-
host=host, port=port, db=db, password=password, socket_timeout=socket_timeout,
2784-
socket_connect_timeout=socket_connect_timeout,
2785-
socket_keepalive=socket_keepalive,
2786-
socket_keepalive_options=socket_keepalive_options,
2787-
connection_pool=connection_pool,
2788-
unix_socket_path=unix_socket_path,
2789-
encoding=encoding, encoding_errors=encoding_errors,
2790-
charset=charset, errors=errors,
2791-
decode_responses=decode_responses, retry_on_timeout=retry_on_timeout,
2792-
ssl=ssl, ssl_keyfile=ssl_keyfile, ssl_certfile=ssl_certfile,
2793-
ssl_cert_reqs=ssl_cert_reqs, ssl_ca_certs=ssl_ca_certs,
2794-
max_connections=max_connections)
2770+
conn_pool_args = [
2771+
'db',
2772+
'username',
2773+
'password',
2774+
'socket_timeout',
2775+
'encoding',
2776+
'encoding_errors',
2777+
'decode_responses',
2778+
'retry_on_timeout',
2779+
'max_connections',
2780+
'health_check_interval',
2781+
'client_name'
2782+
]
2783+
for arg in conn_pool_args:
2784+
if arg in bound.arguments:
2785+
kwargs[arg] = bound.arguments[arg]
2786+
bound.arguments['connection_pool'] = redis.connection.ConnectionPool(**kwargs)
2787+
super().__init__(*bound.args, **bound.kwargs)
27952788

27962789
@classmethod
27972790
def from_url(cls, url, db=None, **kwargs):

0 commit comments

Comments
 (0)