diff --git a/fakeredis/_server.py b/fakeredis/_server.py index e1e74c1..1b7ad32 100644 --- a/fakeredis/_server.py +++ b/fakeredis/_server.py @@ -69,13 +69,13 @@ FLAG_NO_SCRIPT = 's' # Command not allowed in scripts -class SimpleString(object): +class SimpleString: def __init__(self, value): assert isinstance(value, bytes) self.value = value -class NoResponse(object): +class NoResponse: """Returned by pub/sub commands to indicate that no response should be returned""" pass @@ -169,7 +169,7 @@ def compile_pattern(pattern): return re.compile(regex, re.S) -class Item(object): +class Item: """An item stored in the database""" __slots__ = ['value', 'expireat'] @@ -179,7 +179,7 @@ def __init__(self, value): self.expireat = None -class CommandItem(object): +class CommandItem: """An item referenced by a command. It wraps an Item but has extra fields to manage updates and notifications. @@ -315,7 +315,7 @@ class Hash(dict): redis_type = b'hash' -class Int(object): +class Int: """Argument converter for 64-bit signed integers""" DECODE_ERROR = INVALID_INT_MSG @@ -374,7 +374,7 @@ class Timeout(Int): MIN_VALUE = 0 -class Float(object): +class Float: """Argument converter for floating-point values. Redis uses long double for some cases (INCRBYFLOAT, HINCRBYFLOAT) @@ -432,11 +432,11 @@ class SortFloat(Float): @classmethod def decode(cls, value): - return super(SortFloat, cls).decode( + return super().decode( value, allow_leading_whitespace=True, allow_empty=True, crop_null=True) -class ScoreTest(object): +class ScoreTest: """Argument converter for sorted set score endpoints.""" def __init__(self, value, exclusive=False): self.value = value @@ -471,7 +471,7 @@ def upper_bound(self): return (self.value, BeforeAny() if self.exclusive else AfterAny()) -class StringTest(object): +class StringTest: """Argument converter for sorted set LEX endpoints.""" def __init__(self, value, exclusive): self.value = value @@ -492,7 +492,7 @@ def decode(cls, value): @functools.total_ordering -class BeforeAny(object): +class BeforeAny: def __gt__(self, other): return False @@ -501,7 +501,7 @@ def __eq__(self, other): @functools.total_ordering -class AfterAny(object): +class AfterAny: def __lt__(self, other): return False @@ -509,7 +509,7 @@ def __eq__(self, other): return isinstance(other, AfterAny) -class Key(object): +class Key: """Marker to indicate that argument in signature is a key""" UNSPECIFIED = object() @@ -518,7 +518,7 @@ def __init__(self, type_=None, missing_return=UNSPECIFIED): self.missing_return = missing_return -class Signature(object): +class Signature: def __init__(self, name, fixed, repeat=(), flags=""): self.name = name self.fixed = fixed @@ -594,7 +594,7 @@ def decorator(func): return decorator -class FakeServer(object): +class FakeServer: def __init__(self): self.lock = threading.Lock() self.dbs = defaultdict(lambda: Database(self.lock)) @@ -607,7 +607,7 @@ def __init__(self): self.connected = True -class FakeSocket(object): +class FakeSocket: def __init__(self, server): self._server = server self._db = server.dbs[0] @@ -2425,7 +2425,7 @@ def publish(self, channel, message): delattr(FakeSocket, 'exec_') -class _DummyParser(object): +class _DummyParser: def __init__(self, socket_read_size): self.socket_read_size = socket_read_size @@ -2440,7 +2440,7 @@ def on_connect(self, connection): try: from redis.selector import BaseSelector except ImportError: - class BaseSelector(object): + class BaseSelector: def __init__(self, sock): self.sock = sock @@ -2505,7 +2505,7 @@ def __init__(self, server, db=0, username=None, password=None, self.next_health_check = 0 def connect(self): - super(FakeConnection, self).connect() + super().connect() # The selector is set in redis.Connection.connect() after _connect() is called self._selector = FakeSelector(self._sock) @@ -2547,7 +2547,7 @@ def read_response(self): return self._decode(response) -class FakeRedisMixin(object): +class FakeRedisMixin: def __init__(self, host='localhost', port=6379, db=0, password=None, socket_timeout=None, socket_connect_timeout=None, @@ -2587,7 +2587,7 @@ def __init__(self, host='localhost', port=6379, connection_pool = redis.connection.ConnectionPool(**kwargs) # These need to be passed by name due to # https://github.com/andymccurdy/redis-py/issues/1276 - super(FakeRedisMixin, self).__init__( + super().__init__( host=host, port=port, db=db, password=password, socket_timeout=socket_timeout, socket_connect_timeout=socket_connect_timeout, socket_keepalive=socket_keepalive, @@ -2606,7 +2606,7 @@ def from_url(cls, url, db=None, **kwargs): server = kwargs.pop('server', None) if server is None: server = FakeServer() - self = super(FakeRedisMixin, cls).from_url(url, db, **kwargs) + self = super().from_url(url, db, **kwargs) # Now override how it creates connections pool = self.connection_pool pool.connection_class = FakeConnection diff --git a/fakeredis/_zset.py b/fakeredis/_zset.py index 95097b4..47d1169 100644 --- a/fakeredis/_zset.py +++ b/fakeredis/_zset.py @@ -1,7 +1,7 @@ import sortedcontainers -class ZSet(object): +class ZSet: def __init__(self): self._bylex = {} # Maps value to score self._byscore = sortedcontainers.SortedList() diff --git a/test_fakeredis.py b/test_fakeredis.py index f703309..6ab7d38 100644 --- a/test_fakeredis.py +++ b/test_fakeredis.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- from collections import namedtuple from time import sleep, time from redis.exceptions import ResponseError @@ -137,30 +136,30 @@ def test_set_float_value(self): def test_saving_non_ascii_chars_as_value(self): self.assertEqual(self.redis.set('foo', 'Ñandu'), True) self.assertEqual(self.redis.get('foo'), - u'Ñandu'.encode('utf-8')) + 'Ñandu'.encode()) def test_saving_unicode_type_as_value(self): - self.assertEqual(self.redis.set('foo', u'Ñandu'), True) + self.assertEqual(self.redis.set('foo', 'Ñandu'), True) self.assertEqual(self.redis.get('foo'), - u'Ñandu'.encode('utf-8')) + 'Ñandu'.encode()) def test_saving_non_ascii_chars_as_key(self): self.assertEqual(self.redis.set('Ñandu', 'foo'), True) self.assertEqual(self.redis.get('Ñandu'), b'foo') def test_saving_unicode_type_as_key(self): - self.assertEqual(self.redis.set(u'Ñandu', 'foo'), True) - self.assertEqual(self.redis.get(u'Ñandu'), b'foo') + self.assertEqual(self.redis.set('Ñandu', 'foo'), True) + self.assertEqual(self.redis.get('Ñandu'), b'foo') def test_future_newbytes(self): bytes = pytest.importorskip('builtins', reason='future.types not available').bytes self.redis.set(bytes(b'\xc3\x91andu'), 'foo') - self.assertEqual(self.redis.get(u'Ñandu'), b'foo') + self.assertEqual(self.redis.get('Ñandu'), b'foo') def test_future_newstr(self): str = pytest.importorskip('builtins', reason='future.types not available').str - self.redis.set(str(u'Ñandu'), 'foo') - self.assertEqual(self.redis.get(u'Ñandu'), b'foo') + self.redis.set('Ñandu', 'foo') + self.assertEqual(self.redis.get('Ñandu'), b'foo') def test_get_does_not_exist(self): self.assertEqual(self.redis.get('foo'), None) @@ -4376,7 +4375,7 @@ def test_lock_extend_expired(self): lock.extend(3) -class DecodeMixin(object): +class DecodeMixin: decode_responses = True def _round_str(self, x): @@ -4399,13 +4398,13 @@ def _decode(cls, value): return value def assertEqual(self, a, b, msg=None): - super(DecodeMixin, self).assertEqual(a, self._decode(b), msg) + super().assertEqual(a, self._decode(b), msg) def assertIn(self, member, container, msg=None): - super(DecodeMixin, self).assertIn(self._decode(member), container) + super().assertIn(self._decode(member), container) def assertItemsEqual(self, a, b): - super(DecodeMixin, self).assertItemsEqual(a, self._decode(b)) + super().assertItemsEqual(a, self._decode(b)) class TestFakeStrictRedisDecodeResponses(DecodeMixin, TestFakeStrictRedis): diff --git a/test_fakeredis_hypothesis.py b/test_fakeredis_hypothesis.py index 1292f54..ef67df3 100644 --- a/test_fakeredis_hypothesis.py +++ b/test_fakeredis_hypothesis.py @@ -1,4 +1,3 @@ -from __future__ import print_function, division, absolute_import import operator import functools @@ -47,7 +46,7 @@ def sample_attr(draw, name): expires_ms = st.integers(min_value=100000000, max_value=10000000000000) -class WrappedException(object): +class WrappedException: """Wraps an exception for the purposes of comparison.""" def __init__(self, exc): self.wrapped = exc @@ -56,7 +55,7 @@ def __str__(self): return str(self.wrapped) def __repr__(self): - return 'WrappedException({0!r})'.format(self.wrapped) + return 'WrappedException({!r})'.format(self.wrapped) def __eq__(self, other): if not isinstance(other, WrappedException): @@ -92,8 +91,7 @@ def sort_list(lst): def flatten(args): if isinstance(args, (list, tuple)): for arg in args: - for item in flatten(arg): - yield item + yield from flatten(arg) elif args is not None: yield args @@ -102,7 +100,7 @@ def default_normalize(x): return x -class Command(object): +class Command: def __init__(self, *args): self.args = tuple(flatten(args)) @@ -352,7 +350,7 @@ class CommonMachine(hypothesis.stateful.GenericStateMachine): STATE_RUNNING = 2 def __init__(self): - super(CommonMachine, self).__init__() + super().__init__() self.fake = fakeredis.FakeStrictRedis() try: self.real = redis.StrictRedis('localhost', port=6379) @@ -377,7 +375,7 @@ def __init__(self): def teardown(self): self.real.connection_pool.disconnect() self.fake.connection_pool.disconnect() - super(CommonMachine, self).teardown() + super().teardown() def _evaluate(self, client, command): try: @@ -396,7 +394,7 @@ def _compare(self, command): if fake_exc is not None and real_exc is None: raise fake_exc elif real_exc is not None and fake_exc is None: - assert real_exc == fake_exc, "Expected exception {0} not raised".format(real_exc) + assert real_exc == fake_exc, "Expected exception {} not raised".format(real_exc) elif (real_exc is None and isinstance(real_result, list) and command.args and command.args[0].lower() == 'exec'): assert fake_result is not None @@ -452,7 +450,7 @@ def execute_step(self, step): self._compare(step) -class BaseTest(object): +class BaseTest: create_command_strategy = None """Base class for test classes."""