Skip to content

Commit

Permalink
Tighten lint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jul 24, 2024
1 parent 101cdc5 commit 8c709a8
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 18 deletions.
3 changes: 2 additions & 1 deletion cavalry/db_django2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion cavalry/db_django3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 1 addition & 4 deletions cavalry/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion cavalry/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions cavalry/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions cavalry/timing.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions cavalry_tests/tests/test_cavalry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down

0 comments on commit 8c709a8

Please sign in to comment.