From 44a19a720789953a4ef4a2a3521f9de9f3f6a454 Mon Sep 17 00:00:00 2001 From: Aleksandr Shtaub <91491081+aleksandr-shtaub@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:22:34 +0200 Subject: [PATCH 1/5] Upgrade with pyupgrade --- docs/conf.py | 1 - redis_cache/backends/base.py | 2 +- redis_cache/backends/multiple.py | 2 +- redis_cache/backends/single.py | 2 +- redis_cache/compressors.py | 8 ++++---- redis_cache/connection.py | 2 +- redis_cache/serializers.py | 10 +++++----- redis_cache/sharder.py | 4 ++-- redis_cache/utils.py | 2 +- tests/testapp/tests/base_tests.py | 9 +++------ tests/testapp/tests/compressor_tests.py | 3 +-- tests/testapp/tests/master_slave_tests.py | 2 +- tests/testapp/tests/multi_server_tests.py | 4 ++-- tests/testapp/tests/serializers_tests.py | 3 --- tests/testapp/tests/socket_tests.py | 1 - tests/testapp/tests/socket_timeout_tests.py | 1 - tests/testapp/tests/tcp_tests.py | 1 - 17 files changed, 23 insertions(+), 34 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index da4b568b..c89bd461 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -# -*- coding: utf-8 -*- # # django-redis-cache documentation build configuration file, created by # sphinx-quickstart on Mon Jul 20 10:08:24 2015. diff --git a/redis_cache/backends/base.py b/redis_cache/backends/base.py index 3420055a..34dac31a 100644 --- a/redis_cache/backends/base.py +++ b/redis_cache/backends/base.py @@ -40,7 +40,7 @@ def __init__(self, server, params): """ Connect to Redis, and set up cache backend. """ - super(BaseRedisCache, self).__init__(params) + super().__init__(params) self.server = server self.servers = get_servers(server) self.params = params or {} diff --git a/redis_cache/backends/multiple.py b/redis_cache/backends/multiple.py index d3d3f6c2..d036d7a7 100644 --- a/redis_cache/backends/multiple.py +++ b/redis_cache/backends/multiple.py @@ -9,7 +9,7 @@ class ShardedRedisCache(BaseRedisCache): def __init__(self, server, params): - super(ShardedRedisCache, self).__init__(server, params) + super().__init__(server, params) self.sharder = HashRing() for server in self.servers: diff --git a/redis_cache/backends/single.py b/redis_cache/backends/single.py index 4a77ecf6..209a0c0c 100644 --- a/redis_cache/backends/single.py +++ b/redis_cache/backends/single.py @@ -15,7 +15,7 @@ def __init__(self, server, params): """ Connect to Redis, and set up cache backend. """ - super(RedisCache, self).__init__(server, params) + super().__init__(server, params) for server in self.servers: client = self.create_client(server) diff --git a/redis_cache/compressors.py b/redis_cache/compressors.py index c246d24d..3fa66361 100644 --- a/redis_cache/compressors.py +++ b/redis_cache/compressors.py @@ -6,10 +6,10 @@ pass -class BaseCompressor(object): +class BaseCompressor: def __init__(self, **kwargs): - super(BaseCompressor, self).__init__() + super().__init__() def compress(self, value): raise NotImplementedError @@ -31,7 +31,7 @@ class ZLibCompressor(BaseCompressor): def __init__(self, level=6): self.level = level - super(ZLibCompressor, self).__init__() + super().__init__() def compress(self, value): return zlib.compress(value, self.level) @@ -44,7 +44,7 @@ class BZip2Compressor(BaseCompressor): def __init__(self, compresslevel=9): self.compresslevel = compresslevel - super(BZip2Compressor, self).__init__() + super().__init__() def compress(self, value): return bz2.compress(value, compresslevel=self.compresslevel) diff --git a/redis_cache/connection.py b/redis_cache/connection.py index 20742eb4..283519d4 100644 --- a/redis_cache/connection.py +++ b/redis_cache/connection.py @@ -1,7 +1,7 @@ from redis.connection import UnixDomainSocketConnection, Connection, SSLConnection -class CacheConnectionPool(object): +class CacheConnectionPool: def __init__(self): self._clients = {} diff --git a/redis_cache/serializers.py b/redis_cache/serializers.py index dcddebcc..a7e7cc6f 100644 --- a/redis_cache/serializers.py +++ b/redis_cache/serializers.py @@ -18,10 +18,10 @@ from django.utils.encoding import force_bytes, force_str -class BaseSerializer(object): +class BaseSerializer: def __init__(self, **kwargs): - super(BaseSerializer, self).__init__(**kwargs) + super().__init__(**kwargs) def serialize(self, value): raise NotImplementedError @@ -30,7 +30,7 @@ def deserialize(self, value): raise NotImplementedError -class PickleSerializer(object): +class PickleSerializer: def __init__(self, pickle_version=-1): self.pickle_version = pickle_version @@ -45,7 +45,7 @@ def deserialize(self, value): class JSONSerializer(BaseSerializer): def __init__(self, **kwargs): - super(JSONSerializer, self).__init__(**kwargs) + super().__init__(**kwargs) def serialize(self, value): return force_bytes(json.dumps(value)) @@ -75,7 +75,7 @@ def deserialize(self, value): class DummySerializer(BaseSerializer): def __init__(self, **kwargs): - super(DummySerializer, self).__init__(**kwargs) + super().__init__(**kwargs) def serialize(self, value): return value diff --git a/redis_cache/sharder.py b/redis_cache/sharder.py index 9a5bd834..da23a84e 100644 --- a/redis_cache/sharder.py +++ b/redis_cache/sharder.py @@ -10,7 +10,7 @@ def get_slot(key): return int(digest[-DIGITS:], 16) -class Node(object): +class Node: def __init__(self, node, i): self._node = node @@ -28,7 +28,7 @@ def __gt__(self, other): ) -class HashRing(object): +class HashRing: def __init__(self, replicas=16): self.replicas = replicas diff --git a/redis_cache/utils.py b/redis_cache/utils.py index 1e639714..fea2b890 100644 --- a/redis_cache/utils.py +++ b/redis_cache/utils.py @@ -135,7 +135,7 @@ def parse_connection_kwargs(server, db=None, **kwargs): port = int(port) except (ValueError, TypeError): raise ImproperlyConfigured( - "{0} from {1} must be an integer".format( + "{} from {} must be an integer".format( repr(port), server ) diff --git a/tests/testapp/tests/base_tests.py b/tests/testapp/tests/base_tests.py index c90566b3..8a3c2842 100644 --- a/tests/testapp/tests/base_tests.py +++ b/tests/testapp/tests/base_tests.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from hashlib import sha1 import os import subprocess @@ -69,7 +66,7 @@ def start_redis_servers(servers, db=None, master=None): if is_socket: parameters.update( port=0, - unixsocket='/tmp/redis{0}.sock'.format(i), + unixsocket=f'/tmp/redis{i}.sock', unixsocketperm=755, ) if master and not connection_kwargs == master_connection_kwargs: @@ -82,7 +79,7 @@ def start_redis_servers(servers, db=None, master=None): ) args = ['./redis/src/redis-server'] + [ - "--{parameter} {value}".format(parameter=parameter, value=value) + f"--{parameter} {value}" for parameter, value in parameters.items() ] p = subprocess.Popen(args, stdout=devnull) @@ -91,7 +88,7 @@ def start_redis_servers(servers, db=None, master=None): return processes -class SetupMixin(object): +class SetupMixin: processes = None @classmethod diff --git a/tests/testapp/tests/compressor_tests.py b/tests/testapp/tests/compressor_tests.py index 392e0c92..0d8cff71 100644 --- a/tests/testapp/tests/compressor_tests.py +++ b/tests/testapp/tests/compressor_tests.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from django.test import TestCase, override_settings from tests.testapp.tests.base_tests import BaseRedisTestCase @@ -6,7 +5,7 @@ LOCATION = "127.0.0.1:6381" -class CompressionTestCase(object): +class CompressionTestCase: def test_compression(self): key = 'a' diff --git a/tests/testapp/tests/master_slave_tests.py b/tests/testapp/tests/master_slave_tests.py index 6b0ca33a..97998d14 100644 --- a/tests/testapp/tests/master_slave_tests.py +++ b/tests/testapp/tests/master_slave_tests.py @@ -33,7 +33,7 @@ class MasterSlaveTestCase(SetupMixin, TestCase): def setUp(self): - super(MasterSlaveTestCase, self).setUp() + super().setUp() pool.reset() def test_master_client(self): diff --git a/tests/testapp/tests/multi_server_tests.py b/tests/testapp/tests/multi_server_tests.py index ee720b53..e4e8adce 100644 --- a/tests/testapp/tests/multi_server_tests.py +++ b/tests/testapp/tests/multi_server_tests.py @@ -14,7 +14,7 @@ def stddev(lst): return sqrt(variance) -class MultiServerTests(object): +class MultiServerTests: def test_distribution(self): nodes = [node._position for node in self.cache.sharder._nodes] @@ -32,7 +32,7 @@ def test_distribution(self): def test_make_key_distribution(self): ring = HashRing() - nodes = set([str(node._node) for node in self.cache.sharder._nodes]) + nodes = {str(node._node) for node in self.cache.sharder._nodes} nodes = [ ('127.0.0.1', 6379, 15, '/tmp/redis0.sock'), ('127.0.0.1', 6379, 15, '/tmp/redis1.sock'), diff --git a/tests/testapp/tests/serializers_tests.py b/tests/testapp/tests/serializers_tests.py index 338ffe85..2a530ae0 100644 --- a/tests/testapp/tests/serializers_tests.py +++ b/tests/testapp/tests/serializers_tests.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.test import TestCase, override_settings from tests.testapp.tests.base_tests import SetupMixin diff --git a/tests/testapp/tests/socket_tests.py b/tests/testapp/tests/socket_tests.py index 4efbf1a1..53223c05 100644 --- a/tests/testapp/tests/socket_tests.py +++ b/tests/testapp/tests/socket_tests.py @@ -1,4 +1,3 @@ -# # -*- coding: utf-8 -*- from collections import Counter from tests.testapp.tests.base_tests import BaseRedisTestCase from tests.testapp.tests.multi_server_tests import MultiServerTests diff --git a/tests/testapp/tests/socket_timeout_tests.py b/tests/testapp/tests/socket_timeout_tests.py index 90e7d52a..5d21df7c 100644 --- a/tests/testapp/tests/socket_timeout_tests.py +++ b/tests/testapp/tests/socket_timeout_tests.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from django.test import TestCase, override_settings from redis.exceptions import ConnectionError diff --git a/tests/testapp/tests/tcp_tests.py b/tests/testapp/tests/tcp_tests.py index 1ebd6e1d..ef7348a0 100644 --- a/tests/testapp/tests/tcp_tests.py +++ b/tests/testapp/tests/tcp_tests.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from tests.testapp.tests.base_tests import BaseRedisTestCase from tests.testapp.tests.multi_server_tests import MultiServerTests from django.test import TestCase, override_settings From c96d7196797d6671b5397c84e41b85dab6b3de06 Mon Sep 17 00:00:00 2001 From: Aleksandr Shtaub <91491081+aleksandr-shtaub@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:23:29 +0200 Subject: [PATCH 2/5] Remove compat with Django <3.2 --- tests/testapp/tests/base_tests.py | 9 +++------ tests/testapp/tests/master_slave_tests.py | 5 +---- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/testapp/tests/base_tests.py b/tests/testapp/tests/base_tests.py index 8a3c2842..8ed93f2c 100644 --- a/tests/testapp/tests/base_tests.py +++ b/tests/testapp/tests/base_tests.py @@ -123,12 +123,9 @@ def setUp(self): def tearDown(self): # clear caches to allow @override_settings(CACHES=...) to work. - if django.VERSION < (3, 2): - caches._caches.caches = {} - else: - for alias in caches: - if hasattr(caches._connections, alias): - del caches[alias] + for alias in caches: + if hasattr(caches._connections, alias): + del caches[alias] # Sometimes it will be necessary to skip this method because we need to # test default initialization and that may be using a different port # than the test redis server. diff --git a/tests/testapp/tests/master_slave_tests.py b/tests/testapp/tests/master_slave_tests.py index 97998d14..908dc52a 100644 --- a/tests/testapp/tests/master_slave_tests.py +++ b/tests/testapp/tests/master_slave_tests.py @@ -38,10 +38,7 @@ def setUp(self): def test_master_client(self): # Reset the cache at the beginning of the test. - if django.VERSION < (3, 2): - del caches._caches.caches['default'] - else: - del caches['default'] + del caches['default'] cache = self.get_cache() client = cache.master_client self.assertEqual( From 55d1b9c074adb3104413045b1ae197c0219e3a27 Mon Sep 17 00:00:00 2001 From: Aleksandr Shtaub <91491081+aleksandr-shtaub@users.noreply.github.com> Date: Mon, 29 Jan 2024 15:31:26 +0200 Subject: [PATCH 3/5] Migrate to GHA --- .github/workflows/test.yml | 36 ++++++++++++++++++++++++++++++++++++ .travis.yml | 17 ----------------- setup.py | 6 ++---- 3 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 .github/workflows/test.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..6009ee9c --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,36 @@ +name: Test + +on: + - push + +jobs: + build: + name: Python ${{ matrix.python-version }} / ${{ matrix.django-version }} + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + python-version: + - '3.8' + - '3.9' + django-version: + - '>=3.2,<4.0' + - '>=4.2,<5.0' + + env: + DJANGO_VERSION: ${{ matrix.django-version }} + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install redis + run: ./install_redis.sh + + - name: Run tests + run: make test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 330f6a63..00000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -dist: xenial -language: python -python: - - 3.6 - - 3.7 - - 3.8 -env: - - DJANGO_VERSION='>=3.0,<3.1' - - DJANGO_VERSION='>=3.1,<3.2' - - DJANGO_VERSION='>=3.2,<4.0' -# command to run tests -install: ./install_redis.sh -script: make test DJANGO_VERSION=$DJANGO_VERSION -branches: - only: - - unstable - - master diff --git a/setup.py b/setup.py index f1f9c70a..2d384660 100644 --- a/setup.py +++ b/setup.py @@ -12,17 +12,15 @@ install_requires=['redis<4.0'], classifiers=[ "Programming Language :: Python", - "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Topic :: Software Development :: Libraries", "Topic :: Utilities", "Environment :: Web Environment", "Framework :: Django", - "Framework :: Django :: 3.0", - "Framework :: Django :: 3.1", "Framework :: Django :: 3.2", + "Framework :: Django :: 4.2", ], ) From 240bbc1f6de555e44854728ce2f25376a40d3cdd Mon Sep 17 00:00:00 2001 From: Aleksandr Shtaub <91491081+aleksandr-shtaub@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:39:51 +0200 Subject: [PATCH 4/5] Update redis repo link --- install_redis.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_redis.sh b/install_redis.sh index a949ea2c..68871833 100755 --- a/install_redis.sh +++ b/install_redis.sh @@ -2,6 +2,6 @@ : ${REDIS_VERSION:="4.0.11"} -test -d redis || git clone https://github.com/antirez/redis +test -d redis || git clone https://github.com/redis/redis git -C redis checkout $REDIS_VERSION make -C redis From 10cff0e67ec383de1cec3c535ef9316f6398f666 Mon Sep 17 00:00:00 2001 From: Aleksandr Shtaub <91491081+aleksandr-shtaub@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:40:21 +0200 Subject: [PATCH 5/5] Bump default DJANGO_VERSION --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2e41ff65..90c0eb6b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SHELL := /bin/bash PACKAGE_NAME=redis_cache -DJANGO_VERSION?=>=1.11,<4.0 +DJANGO_VERSION?=>=4.2,<5.0 .PHONY: install_requirements install_requirements: requirements*.txt