From 2e76a03987db6e26ae6987b2b23cc0c8e3c8df65 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 24 Jul 2024 13:50:57 +0300 Subject: [PATCH] Tighten lint rules --- cavalry/db_django2.py | 3 ++- cavalry/db_django3.py | 3 ++- cavalry/middleware.py | 5 +---- cavalry/policy.py | 2 +- cavalry/reporter.py | 6 +++--- cavalry/timing.py | 5 +++-- cavalry_tests/tests/test_cavalry.py | 8 ++++---- pyproject.toml | 17 +++++++++++++++-- 8 files changed, 31 insertions(+), 18 deletions(-) diff --git a/cavalry/db_django2.py b/cavalry/db_django2.py index d555ecc..be42245 100644 --- a/cavalry/db_django2.py +++ b/cavalry/db_django2.py @@ -8,7 +8,8 @@ from cavalry.db_common import record from cavalry.timing import get_time -assert django.VERSION[0] == 2, "This module won't work with Django 3" +if django.VERSION[0] != 2: + raise RuntimeError("This module only works with Django 2") DefaultCursorDebugWrapper = db_backend_utils.CursorDebugWrapper diff --git a/cavalry/db_django3.py b/cavalry/db_django3.py index 25d8be2..7dacf10 100644 --- a/cavalry/db_django3.py +++ b/cavalry/db_django3.py @@ -6,7 +6,8 @@ from cavalry.db_common import record from cavalry.timing import get_time -assert django.VERSION[0] != 2, "This module won't work with Django 2" +if django.VERSION[0] == 2: + raise RuntimeError("This module won't work with Django 2") def log_query(execute, sql, params, many, context): diff --git a/cavalry/middleware.py b/cavalry/middleware.py index 97d929f..5cc50f1 100644 --- a/cavalry/middleware.py +++ b/cavalry/middleware.py @@ -35,10 +35,7 @@ def _process(request, get_response): data["databases"] = {} for conn in connections.all(): queries = [q for q in conn.queries if "hrtime" in q] - if queries: - total_time = sum(q.get("hrtime", 0) * 1000 for q in queries) - else: - total_time = 0 + total_time = sum(q.get("hrtime", 0) * 1000 for q in queries) if queries else 0 data["databases"][conn.alias] = { "queries": queries, "n_queries": len(queries), diff --git a/cavalry/policy.py b/cavalry/policy.py index c7b3676..3a27ef1 100644 --- a/cavalry/policy.py +++ b/cavalry/policy.py @@ -13,7 +13,7 @@ def can_cavalrize(request: WSGIRequest) -> bool: if not getattr(settings, "CAVALRY_ENABLED", False): return False probability = getattr(settings, "CAVALRY_PROBABILITY", 1.0) - return probability >= 1 or (random() < probability) + return probability >= 1 or (random() < probability) # noqa: S311 def can_post_stats(request: WSGIRequest) -> bool: diff --git a/cavalry/reporter.py b/cavalry/reporter.py index 82282a2..2dc542e 100644 --- a/cavalry/reporter.py +++ b/cavalry/reporter.py @@ -49,9 +49,9 @@ def inject_html(request: WSGIRequest, response: HttpResponse, data: dict, summar def generate_console_script(data: dict, with_stacks: bool = False) -> List[str]: script = [] - for db, data in data.get("databases", {}).items(): - queries = data["queries"] - header = f'{db}: {data["n_queries"]} queries ({data["time"]:.2f} msec)' + for db, datum in data.get("databases", {}).items(): + queries = datum["queries"] + header = f'{db}: {datum["n_queries"]} queries ({datum["time"]:.2f} msec)' script.append(f"console.group({json.dumps(header)});") for query in queries: script.append(build_log_command(query)) diff --git a/cavalry/timing.py b/cavalry/timing.py index 4b68a81..c0edfe4 100644 --- a/cavalry/timing.py +++ b/cavalry/timing.py @@ -1,7 +1,8 @@ import time try: - assert time.perf_counter() + if not time.perf_counter(): + raise Exception() get_time = time.perf_counter -except: # noqa +except Exception: get_time = time.time diff --git a/cavalry_tests/tests/test_cavalry.py b/cavalry_tests/tests/test_cavalry.py index 5c91574..cabbfb6 100644 --- a/cavalry_tests/tests/test_cavalry.py +++ b/cavalry_tests/tests/test_cavalry.py @@ -5,10 +5,10 @@ from django.test import Client -@pytest.mark.django_db -@pytest.mark.parametrize("enable", (False, True)) -@pytest.mark.parametrize("as_admin", (False, True)) -@pytest.mark.parametrize("posting", (False, True)) +@pytest.mark.django_db() +@pytest.mark.parametrize("enable", [False, True]) +@pytest.mark.parametrize("as_admin", [False, True]) +@pytest.mark.parametrize("posting", [False, True]) def test_cavalry(settings, as_admin, enable, posting, admin_user): settings.CAVALRY_ENABLED = enable settings.CAVALRY_ELASTICSEARCH_URL_TEMPLATE = "http://localhost:59595/asdf/foo" if posting else None diff --git a/pyproject.toml b/pyproject.toml index eab0f67..62bae47 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,9 +48,22 @@ line-length = 119 [tool.ruff.lint] extend-select = [ + "B", # bugbear "C", - "COM", - "I", + "COM", # trailing commas + "E", # pycodestyle + "F", # pyflakes + "I", # isort + "PT", + "S", # security + "T", # debugger and print + "UP", # upgrade + "W", # pycodestyle +] + +[tool.ruff.lint.extend-per-file-ignores] +"cavalry_tests/**" = [ + "S101", # Use of assert detected ] [[tool.mypy.overrides]]