Skip to content

Commit

Permalink
Run pyupgrade --py3-only
Browse files Browse the repository at this point in the history
Now that Python 2 support is dropped.
  • Loading branch information
bmerry committed Feb 3, 2020
1 parent d8eee78 commit 78e3ae2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
42 changes: 21 additions & 21 deletions fakeredis/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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']
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -492,7 +492,7 @@ def decode(cls, value):


@functools.total_ordering
class BeforeAny(object):
class BeforeAny:
def __gt__(self, other):
return False

Expand All @@ -501,15 +501,15 @@ def __eq__(self, other):


@functools.total_ordering
class AfterAny(object):
class AfterAny:
def __lt__(self, other):
return False

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()

Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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]
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion fakeredis/_zset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sortedcontainers


class ZSet(object):
class ZSet:
def __init__(self):
self._bylex = {} # Maps value to score
self._byscore = sortedcontainers.SortedList()
Expand Down
25 changes: 12 additions & 13 deletions test_fakeredis.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
18 changes: 8 additions & 10 deletions test_fakeredis_hypothesis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function, division, absolute_import
import operator
import functools

Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -102,7 +100,7 @@ def default_normalize(x):
return x


class Command(object):
class Command:
def __init__(self, *args):
self.args = tuple(flatten(args))

Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 78e3ae2

Please sign in to comment.