This has been discussed in #514, #282 and #88.

Using an explicit type check via `isinstance`, rather than duck typing, is typically considered unpythonic and breaks compatibility with mock objects such as FakeRedis. This patches removes the type check, and instead looks for a common method that should be present on the object as a hint on whether it's compatible or not.
main
Cal Leeming 9 years ago
parent 8bbd833855
commit d80f9f8ba0

@ -22,9 +22,6 @@ PATCHED_METHODS = ['_setex', '_lrem', '_zadd', '_pipeline', '_ttl']
def patch_connection(connection): def patch_connection(connection):
if not isinstance(connection, StrictRedis):
raise ValueError('A StrictRedis or Redis connection is required.')
# Don't patch already patches objects # Don't patch already patches objects
if all([hasattr(connection, attr) for attr in PATCHED_METHODS]): if all([hasattr(connection, attr) for attr in PATCHED_METHODS]):
return connection return connection
@ -38,7 +35,8 @@ def patch_connection(connection):
if hasattr(connection, 'pttl'): if hasattr(connection, 'pttl'):
connection._pttl = fix_return_type(partial(StrictRedis.pttl, connection)) connection._pttl = fix_return_type(partial(StrictRedis.pttl, connection))
elif isinstance(connection, StrictRedis): # add support for mock redis objects
elif hasattr(connection, 'setex'):
connection._setex = connection.setex connection._setex = connection.setex
connection._lrem = connection.lrem connection._lrem = connection.lrem
connection._zadd = connection.zadd connection._zadd = connection.zadd

Loading…
Cancel
Save