Skip to content

Cleaup python code/formatting #472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions django_prometheus/cache/backends/memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 4 additions & 6 deletions django_prometheus/cache/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
2 changes: 0 additions & 2 deletions django_prometheus/db/backends/mysql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions django_prometheus/db/backends/spatialite/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions django_prometheus/db/backends/sqlite3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion django_prometheus/db/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion django_prometheus/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions django_prometheus/tests/end2end/testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions django_prometheus/tests/end2end/testapp/test_caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion django_prometheus/tests/end2end/testapp/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion django_prometheus/tests/end2end/testapp/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down
2 changes: 1 addition & 1 deletion django_prometheus/tests/end2end/testapp/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
2 changes: 1 addition & 1 deletion django_prometheus/tests/end2end/testapp/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
3 changes: 1 addition & 2 deletions django_prometheus/tests/end2end/testapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions django_prometheus/tests/end2end/testapp/wsgi.py
Original file line number Diff line number Diff line change
@@ -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``.

Expand Down
8 changes: 4 additions & 4 deletions django_prometheus/tests/test_django_prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
45 changes: 30 additions & 15 deletions django_prometheus/tests/test_testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


class TestPrometheusTestCaseMixin:
@pytest.fixture()
@pytest.fixture
def registry(self):
return prometheus_client.CollectorRegistry()

Expand All @@ -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(
[
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions django_prometheus/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def TimeSince(t):

Returns:
the time since t, in fractional seconds.

"""
return default_timer() - t

Expand All @@ -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)]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_version():
packages=find_packages(
exclude=[
"tests",
]
],
),
test_suite="django_prometheus.tests",
long_description=LONG_DESCRIPTION,
Expand Down
10 changes: 4 additions & 6 deletions update_version_from_git.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
"""
Expand All @@ -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"
Expand Down Expand Up @@ -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)",
)