diff --git a/django_prometheus/cache/backends/memcached.py b/django_prometheus/cache/backends/memcached.py index 7150abd5..78a7cb17 100644 --- a/django_prometheus/cache/backends/memcached.py +++ b/django_prometheus/cache/backends/memcached.py @@ -22,10 +22,6 @@ def get(self, key, default=None, version=None): class PyLibMCCache(MemcachedPrometheusCacheMixin, memcached.PyLibMCCache): """Inherit memcached to add metrics about hit/miss ratio""" - pass - class PyMemcacheCache(MemcachedPrometheusCacheMixin, memcached.PyMemcacheCache): """Inherit memcached to add metrics about hit/miss ratio""" - - pass diff --git a/django_prometheus/cache/backends/redis.py b/django_prometheus/cache/backends/redis.py index a8ce7206..b777ed76 100644 --- a/django_prometheus/cache/backends/redis.py +++ b/django_prometheus/cache/backends/redis.py @@ -28,9 +28,8 @@ def get(self, key, default=None, version=None, client=None): if cached is not None: django_cache_hits_total.labels(backend="redis").inc() return cached - else: - django_cache_misses_total.labels(backend="redis").inc() - return default + django_cache_misses_total.labels(backend="redis").inc() + return default class NativeRedisCache(DjangoRedisCache): @@ -44,6 +43,5 @@ def get(self, key, default=None, version=None): if result is not None: django_cache_hits_total.labels(backend="native_redis").inc() return result - else: - django_cache_misses_total.labels(backend="native_redis").inc() - return default + django_cache_misses_total.labels(backend="native_redis").inc() + return default diff --git a/django_prometheus/db/backends/mysql/base.py b/django_prometheus/db/backends/mysql/base.py index a6d0d5c1..39df0de3 100644 --- a/django_prometheus/db/backends/mysql/base.py +++ b/django_prometheus/db/backends/mysql/base.py @@ -6,8 +6,6 @@ class DatabaseFeatures(base.DatabaseFeatures): """Our database has the exact same features as the base one.""" - pass - class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper): CURSOR_CLASS = base.CursorWrapper diff --git a/django_prometheus/db/backends/spatialite/base.py b/django_prometheus/db/backends/spatialite/base.py index c995781f..2aa4eacd 100644 --- a/django_prometheus/db/backends/spatialite/base.py +++ b/django_prometheus/db/backends/spatialite/base.py @@ -7,8 +7,6 @@ class DatabaseFeatures(features.DatabaseFeatures): """Our database has the exact same features as the base one.""" - pass - class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper): CURSOR_CLASS = sqlite_base.SQLiteCursorWrapper diff --git a/django_prometheus/db/backends/sqlite3/base.py b/django_prometheus/db/backends/sqlite3/base.py index 089a3d4e..ed8e8e9f 100644 --- a/django_prometheus/db/backends/sqlite3/base.py +++ b/django_prometheus/db/backends/sqlite3/base.py @@ -6,8 +6,6 @@ class DatabaseFeatures(base.DatabaseFeatures): """Our database has the exact same features as the base one.""" - pass - class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper): CURSOR_CLASS = base.SQLiteCursorWrapper diff --git a/django_prometheus/db/common.py b/django_prometheus/db/common.py index 7f87dfca..8292413a 100644 --- a/django_prometheus/db/common.py +++ b/django_prometheus/db/common.py @@ -55,7 +55,6 @@ def ExportingCursorWrapper(cursor_class, alias, vendor): """Returns a CursorWrapper class that knows its database's alias and vendor name. """ - labels = {"alias": alias, "vendor": vendor} class CursorWrapper(cursor_class): diff --git a/django_prometheus/migrations.py b/django_prometheus/migrations.py index 97908751..c5ca6188 100644 --- a/django_prometheus/migrations.py +++ b/django_prometheus/migrations.py @@ -31,7 +31,6 @@ def ExportMigrations(): This is meant to be called during app startup, ideally by django_prometheus.apps.AppConfig. """ - # Import MigrationExecutor lazily. MigrationExecutor checks at # import time that the apps are ready, and they are not when # django_prometheus is imported. ExportMigrations() should be diff --git a/django_prometheus/tests/end2end/testapp/settings.py b/django_prometheus/tests/end2end/testapp/settings.py index 31b09f1b..faf00661 100644 --- a/django_prometheus/tests/end2end/testapp/settings.py +++ b/django_prometheus/tests/end2end/testapp/settings.py @@ -40,9 +40,9 @@ "django.template.context_processors.request", "django.contrib.auth.context_processors.auth", "django.contrib.messages.context_processors.messages", - ] + ], }, - } + }, ] WSGI_APPLICATION = "testapp.wsgi.application" diff --git a/django_prometheus/tests/end2end/testapp/test_caches.py b/django_prometheus/tests/end2end/testapp/test_caches.py index ed1ffcd6..a2152e0b 100644 --- a/django_prometheus/tests/end2end/testapp/test_caches.py +++ b/django_prometheus/tests/end2end/testapp/test_caches.py @@ -70,5 +70,5 @@ def test_cache_version_support(self, supported_cache): tested_cache = caches[supported_cache] tested_cache.set("foo1", "bar v.1", version=1) tested_cache.set("foo1", "bar v.2", version=2) - assert "bar v.1" == tested_cache.get("foo1", version=1) - assert "bar v.2" == tested_cache.get("foo1", version=2) + assert tested_cache.get("foo1", version=1) == "bar v.1" + assert tested_cache.get("foo1", version=2) == "bar v.2" diff --git a/django_prometheus/tests/end2end/testapp/test_db.py b/django_prometheus/tests/end2end/testapp/test_db.py index 4faa708d..f23a7d2f 100644 --- a/django_prometheus/tests/end2end/testapp/test_db.py +++ b/django_prometheus/tests/end2end/testapp/test_db.py @@ -33,7 +33,8 @@ class TestDbMetrics(BaseDBTest): def test_config_has_expected_databases(self): """Not a real unit test: ensures that testapp.settings contains the - databases this test expects.""" + databases this test expects. + """ assert "default" in connections.databases.keys() assert "test_db_1" in connections.databases.keys() assert "test_db_2" in connections.databases.keys() diff --git a/django_prometheus/tests/end2end/testapp/test_middleware.py b/django_prometheus/tests/end2end/testapp/test_middleware.py index 58c250c5..e83eab4a 100644 --- a/django_prometheus/tests/end2end/testapp/test_middleware.py +++ b/django_prometheus/tests/end2end/testapp/test_middleware.py @@ -20,7 +20,8 @@ def M(metric_name): def T(metric_name): """Makes a full metric name from a short metric name like M(metric_name) - This method adds a '_total' postfix for metrics.""" + This method adds a '_total' postfix for metrics. + """ return f"{M(metric_name)}_total" diff --git a/django_prometheus/tests/end2end/testapp/test_migrations.py b/django_prometheus/tests/end2end/testapp/test_migrations.py index a15795eb..26e46561 100644 --- a/django_prometheus/tests/end2end/testapp/test_migrations.py +++ b/django_prometheus/tests/end2end/testapp/test_migrations.py @@ -15,7 +15,7 @@ def M(metric_name): return f"django_migrations_{metric_name}" -@pytest.mark.django_db() +@pytest.mark.django_db class TestMigrations: """Test migration counters.""" diff --git a/django_prometheus/tests/end2end/testapp/test_models.py b/django_prometheus/tests/end2end/testapp/test_models.py index c15cff27..0f898663 100644 --- a/django_prometheus/tests/end2end/testapp/test_models.py +++ b/django_prometheus/tests/end2end/testapp/test_models.py @@ -13,7 +13,7 @@ def M(metric_name): return f"django_model_{metric_name}" -@pytest.mark.django_db() +@pytest.mark.django_db class TestModelMetrics: """Test django_prometheus.models.""" diff --git a/django_prometheus/tests/end2end/testapp/views.py b/django_prometheus/tests/end2end/testapp/views.py index d93edb0e..54d6e04d 100644 --- a/django_prometheus/tests/end2end/testapp/views.py +++ b/django_prometheus/tests/end2end/testapp/views.py @@ -54,8 +54,7 @@ def sql(request): "sql.html", {"query": query, "rows": results, "databases": databases}, ) - else: - return TemplateResponse(request, "sql.html", {"query": None, "rows": None, "databases": databases}) + return TemplateResponse(request, "sql.html", {"query": None, "rows": None, "databases": databases}) def file(request): diff --git a/django_prometheus/tests/end2end/testapp/wsgi.py b/django_prometheus/tests/end2end/testapp/wsgi.py index b5f70a97..23e8ae0d 100644 --- a/django_prometheus/tests/end2end/testapp/wsgi.py +++ b/django_prometheus/tests/end2end/testapp/wsgi.py @@ -1,5 +1,4 @@ -""" -WSGI config for testapp project. +"""WSGI config for testapp project. It exposes the WSGI callable as a module-level variable named ``application``. diff --git a/django_prometheus/tests/test_django_prometheus.py b/django_prometheus/tests/test_django_prometheus.py index 8e2f509c..3664d028 100644 --- a/django_prometheus/tests/test_django_prometheus.py +++ b/django_prometheus/tests/test_django_prometheus.py @@ -5,7 +5,7 @@ class TestDjangoPrometheus: def testPowersOf(self): """Tests utils.PowersOf.""" - assert [0, 1, 2, 4, 8] == PowersOf(2, 4) - assert [0, 3, 9, 27, 81, 243] == PowersOf(3, 5, lower=1) - assert [1, 2, 4, 8] == PowersOf(2, 4, include_zero=False) - assert [4, 8, 16, 32, 64, 128] == PowersOf(2, 6, lower=2, include_zero=False) + assert PowersOf(2, 4) == [0, 1, 2, 4, 8] + assert PowersOf(3, 5, lower=1) == [0, 3, 9, 27, 81, 243] + assert PowersOf(2, 4, include_zero=False) == [1, 2, 4, 8] + assert PowersOf(2, 6, lower=2, include_zero=False) == [4, 8, 16, 32, 64, 128] diff --git a/django_prometheus/tests/test_testutils.py b/django_prometheus/tests/test_testutils.py index 7f8034de..3d3793f4 100644 --- a/django_prometheus/tests/test_testutils.py +++ b/django_prometheus/tests/test_testutils.py @@ -17,7 +17,7 @@ class TestPrometheusTestCaseMixin: - @pytest.fixture() + @pytest.fixture def registry(self): return prometheus_client.CollectorRegistry() @@ -43,20 +43,23 @@ def some_labelled_gauge(self, registry): def test_get_metric(self, registry): """Tests get_metric.""" - assert 42 == get_metric("some_gauge", registry=registry) - assert 1 == get_metric( - "some_labelled_gauge", - registry=registry, - labelred="pink", - labelblue="indigo", + assert get_metric("some_gauge", registry=registry) == 42 + assert ( + get_metric( + "some_labelled_gauge", + registry=registry, + labelred="pink", + labelblue="indigo", + ) + == 1 ) def test_get_metrics_vector(self, registry): """Tests get_metrics_vector.""" vector = get_metrics_vector("some_nonexistent_gauge", registry=registry) - assert [] == vector + assert vector == [] vector = get_metrics_vector("some_gauge", registry=registry) - assert [({}, 42)] == vector + assert vector == [({}, 42)] vector = get_metrics_vector("some_labelled_gauge", registry=registry) assert sorted( [ @@ -99,18 +102,30 @@ def test_registry_saving(self, registry, some_gauge, some_labelled_gauge): """Tests save_registry and frozen registries operations.""" frozen_registry = save_registry(registry=registry) # Test that we can manipulate a frozen scalar metric. - assert 42 == get_metric_from_frozen_registry("some_gauge", frozen_registry) + assert get_metric_from_frozen_registry("some_gauge", frozen_registry) == 42 some_gauge.set(99) - assert 42 == get_metric_from_frozen_registry("some_gauge", frozen_registry) + assert get_metric_from_frozen_registry("some_gauge", frozen_registry) == 42 assert_metric_diff(frozen_registry, 99 - 42, "some_gauge", registry=registry) assert_metric_no_diff(frozen_registry, 1, "some_gauge", registry=registry) # Now test the same thing with a labelled metric. - assert 1 == get_metric_from_frozen_registry( - "some_labelled_gauge", frozen_registry, labelred="pink", labelblue="indigo" + assert ( + get_metric_from_frozen_registry( + "some_labelled_gauge", + frozen_registry, + labelred="pink", + labelblue="indigo", + ) + == 1 ) some_labelled_gauge.labels("pink", "indigo").set(5) - assert 1 == get_metric_from_frozen_registry( - "some_labelled_gauge", frozen_registry, labelred="pink", labelblue="indigo" + assert ( + get_metric_from_frozen_registry( + "some_labelled_gauge", + frozen_registry, + labelred="pink", + labelblue="indigo", + ) + == 1 ) assert_metric_diff( frozen_registry, diff --git a/django_prometheus/utils.py b/django_prometheus/utils.py index a45e0182..06ddef56 100644 --- a/django_prometheus/utils.py +++ b/django_prometheus/utils.py @@ -17,6 +17,7 @@ def TimeSince(t): Returns: the time since t, in fractional seconds. + """ return default_timer() - t @@ -25,5 +26,4 @@ def PowersOf(logbase, count, lower=0, include_zero=True): """Returns a list of count powers of logbase (from logbase**lower).""" if not include_zero: return [logbase**i for i in range(lower, count + lower)] - else: - return [0] + [logbase**i for i in range(lower, count + lower)] + return [0] + [logbase**i for i in range(lower, count + lower)] diff --git a/setup.py b/setup.py index 9b7a1259..e93e415c 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ def get_version(): packages=find_packages( exclude=[ "tests", - ] + ], ), test_suite="django_prometheus.tests", long_description=LONG_DESCRIPTION, diff --git a/update_version_from_git.py b/update_version_from_git.py index 69becb93..6b3e7d9b 100644 --- a/update_version_from_git.py +++ b/update_version_from_git.py @@ -1,5 +1,4 @@ -""" -Adapted from https://github.com/pygame/pygameweb/blob/master/pygameweb/builds/update_version_from_git.py +"""Adapted from https://github.com/pygame/pygameweb/blob/master/pygameweb/builds/update_version_from_git.py For updating the version from git. __init__.py contains a __version__ field. @@ -67,7 +66,7 @@ def get_git_version_info(): def prerelease_version(): - """return what the prerelease version should be. + """Return what the prerelease version should be. https://packaging.python.org/tutorials/distributing-packages/#pre-release-versioning 0.0.2.dev22 """ @@ -90,8 +89,7 @@ def get_version(): def increase_patch_version(old_version): - """ - :param old_version: 2.0.1 + """:param old_version: 2.0.1 :return: 2.0.2.dev """ return f"{old_version.major}.{old_version.minor}.{old_version.micro + 1}.dev" @@ -125,5 +123,5 @@ def release_version_correct(): print( "Invalid usage. Supply 0 or 1 arguments. " "Argument can be either a version '1.2.3' or 'patch' " - "if you want to increase the patch-version (1.2.3 -> 1.2.4.dev)" + "if you want to increase the patch-version (1.2.3 -> 1.2.4.dev)", )