Update Ruff and fix its errors

This commit is contained in:
Joakim Hellsén 2026-07-21 04:12:13 +02:00
commit 1424978854
Signed by: Joakim Hellsén
SSH key fingerprint: SHA256:/9h/CsExpFp+PRhsfA0xznFx2CGfTT5R/kpuFfUgEQk
39 changed files with 183 additions and 175 deletions

View file

@ -14,7 +14,7 @@ class Command(BaseCommand):
help = "Backfill image dimensions for existing cached images"
def handle(self, *args, **options) -> None: # noqa: ARG002, PLR0915
def handle(self, *args, **options) -> None: # ruff:ignore[unused-method-argument, too-many-statements]
"""Execute the command."""
total_updated = 0

View file

@ -2,7 +2,7 @@ import io
import json
import os
import shutil
import subprocess # noqa: S404
import subprocess # ruff:ignore[suspicious-subprocess-import]
from compression import zstd
from datetime import datetime
from pathlib import Path
@ -191,11 +191,11 @@ def _write_table_rows(
connection: SQLite connection.
table: Table name.
"""
cursor = connection.execute(f'SELECT * FROM "{table}"') # noqa: S608
cursor = connection.execute(f'SELECT * FROM "{table}"') # ruff:ignore[hardcoded-sql-expression]
columns = [description[0] for description in cursor.description]
for row in cursor.fetchall():
values = ", ".join(_sql_literal(row[idx]) for idx in range(len(columns)))
handle.write(f'INSERT INTO "{table}" VALUES ({values});\n') # noqa: S608
handle.write(f'INSERT INTO "{table}" VALUES ({values});\n') # ruff:ignore[hardcoded-sql-expression]
def _write_indexes(
@ -268,7 +268,7 @@ def _write_postgres_dump(output_path: Path, tables: list[str]) -> None:
for table in tables:
cmd.extend(["-t", f"public.{table}"])
process = subprocess.Popen( # noqa: S603
process = subprocess.Popen( # ruff:ignore[subprocess-without-shell-equals-true]
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
@ -339,7 +339,7 @@ def _write_json_dump(output_path: Path, tables: list[str]) -> None:
data: dict[str, list[dict]] = {}
with django_connection.cursor() as cursor:
for table in tables:
cursor.execute(f'SELECT * FROM "{table}"') # noqa: S608
cursor.execute(f'SELECT * FROM "{table}"') # ruff:ignore[hardcoded-sql-expression]
columns: list[str] = [col[0] for col in cursor.description]
rows = cursor.fetchall()
data[table] = [dict(zip(columns, row, strict=False)) for row in rows]

View file

@ -403,7 +403,7 @@ def repair_partially_broken_json(raw_text: str) -> str:
wrapped: str = f"[{raw_text}]"
wrapped_data = json.loads(wrapped)
# Validate that all items look like GraphQL responses
if isinstance(wrapped_data, list) and wrapped_data: # noqa: SIM102
if isinstance(wrapped_data, list) and wrapped_data: # ruff:ignore[collapsible-if]
# Check if all items have "data" or "extensions" (GraphQL response structure)
if all(
isinstance(item, dict) and ("data" in item or "extensions" in item)
@ -452,7 +452,7 @@ def repair_partially_broken_json(raw_text: str) -> str:
valid_lines: list[dict[str, Any]] = []
for line in lines:
line: str = line.strip() # noqa: PLW2901
line: str = line.strip() # ruff:ignore[redefined-loop-name]
if line and line.startswith("{"):
try:
fixed_line: str = json_repair.repair_json(line, logging=False)
@ -749,7 +749,7 @@ class Command(BaseCommand):
return channel_obj
def process_responses( # noqa: PLR0915
def process_responses( # ruff:ignore[too-many-statements]
self,
responses: list[dict[str, Any]],
file_path: Path,
@ -1180,7 +1180,7 @@ class Command(BaseCommand):
msg: str = f"Path does not exist: {input_path}"
raise CommandError(msg)
def handle(self, *args, **options) -> None: # noqa: ARG002
def handle(self, *args, **options) -> None: # ruff:ignore[unused-method-argument]
"""Main entry point for the command."""
colorama_init(autoreset=True)

View file

@ -49,7 +49,7 @@ class Command(BaseCommand):
),
)
def handle(self, *args: Any, **options: Any) -> None: # noqa: ANN401, ARG002
def handle(self, *args: Any, **options: Any) -> None: # ruff:ignore[any-type, unused-method-argument]
"""Execute the command to detach the organization and optionally re-import data.
Args:

View file

@ -39,7 +39,7 @@ class Command(BaseCommand):
help="Re-download even if a local box art file already exists.",
)
def handle( # noqa: PLR0914, PLR0915
def handle( # ruff:ignore[too-many-locals, too-many-statements]
self,
*_args: str,
**options: str | bool | int | None,

View file

@ -50,7 +50,7 @@ class Command(BaseCommand):
help="Twitch Access Token (optional - will be obtained automatically if not provided)",
)
def handle(self, *args, **options) -> None: # noqa: ARG002
def handle(self, *args, **options) -> None: # ruff:ignore[unused-method-argument]
"""Main entry point for the command.
Raises:

View file

@ -27,7 +27,7 @@ import hashlib
import json
import os
import pathlib
import subprocess # noqa: S404
import subprocess # ruff:ignore[suspicious-subprocess-import]
import tempfile
from typing import TYPE_CHECKING
from typing import Any
@ -194,7 +194,7 @@ class Command(BaseCommand):
),
)
def handle(self, *args: Any, **options: Any) -> None: # noqa: ANN401, ARG002, PLR0914, PLR0915
def handle(self, *args: Any, **options: Any) -> None: # ruff:ignore[any-type, unused-method-argument, too-many-locals, too-many-statements]
"""Main entry point for the command.
Raises:
@ -301,7 +301,7 @@ class Command(BaseCommand):
crash_on_error=crash_on_error,
skip_existing=skip_existing,
)
except Exception as exc: # noqa: BLE001
except Exception as exc: # ruff:ignore[blind-except]
msg = f"Failed to import drops.json: {exc}"
self.stderr.write(self.style.ERROR(msg))
errors.append(msg)
@ -315,7 +315,7 @@ class Command(BaseCommand):
crash_on_error=crash_on_error,
skip_existing=skip_existing,
)
except Exception as exc: # noqa: BLE001
except Exception as exc: # ruff:ignore[blind-except]
msg = f"Failed to import rewards.json: {exc}"
self.stderr.write(self.style.ERROR(msg))
errors.append(msg)
@ -353,7 +353,7 @@ class Command(BaseCommand):
# Historical import via git clone
# ------------------------------------------------------------------
def _process_historical( # noqa: PLR0913
def _process_historical( # ruff:ignore[too-many-arguments]
self,
source: str,
*,
@ -423,7 +423,7 @@ class Command(BaseCommand):
max_commits=max_commits,
skip_existing=skip_existing,
)
except Exception as exc: # noqa: BLE001
except Exception as exc: # ruff:ignore[blind-except]
errors.append(str(exc))
finally:
if own_clone:
@ -431,7 +431,7 @@ class Command(BaseCommand):
return drops_count, rewards_count, errors
def _import_historical_file( # noqa: PLR0913
def _import_historical_file( # ruff:ignore[too-many-arguments]
self,
git_dir: str,
file_path: str,
@ -581,7 +581,7 @@ class Command(BaseCommand):
Stdout of the git command.
"""
cmd = ["git", "--git-dir", git_dir, *args]
result = subprocess.run(cmd, capture_output=True, text=True, check=check) # noqa: S603
result = subprocess.run(cmd, capture_output=True, text=True, check=check) # ruff:ignore[subprocess-without-shell-equals-true]
return result.stdout
def _rmtree(self, path: str) -> None:
@ -659,7 +659,7 @@ class Command(BaseCommand):
)
@staticmethod
def _strip_typename(data: Any) -> Any: # noqa: ANN401
def _strip_typename(data: Any) -> Any: # ruff:ignore[any-type]
"""Recursively remove ``__typename`` keys from parsed JSON data.
Old commits in the repo included GraphQL ``__typename`` metadata which
@ -683,7 +683,7 @@ class Command(BaseCommand):
return [Command._strip_typename(item) for item in data]
return data
def _generate_report( # noqa: PLR0914, PLR0915
def _generate_report( # ruff:ignore[too-many-locals, too-many-statements]
self,
drops_url: str,
rewards_url: str,
@ -707,7 +707,7 @@ class Command(BaseCommand):
Raises:
ValidationError: If any item fails schema validation and crash_on_error is True.
"""
from collections import Counter # noqa: PLC0415
from collections import Counter # ruff:ignore[import-outside-top-level]
self.stdout.write("=" * 60)
self.stdout.write(self.style.SUCCESS("COMPARISON REPORT"))
@ -719,14 +719,14 @@ class Command(BaseCommand):
# Track differences for field-level comparison
sample_limit = 5
for label, raw_url, model_cls, id_field in [ # noqa: PLR1702
for label, raw_url, model_cls, id_field in [ # ruff:ignore[too-many-nested-blocks]
("drops", drops_url, DropCampaign, "twitch_id"),
("rewards", rewards_url, RewardCampaign, "twitch_id"),
]:
self.stdout.write(f"\n--- {label.upper()} ---")
try:
raw_data = self._fetch_json(raw_url, f"{label}.json")
except Exception as exc: # noqa: BLE001
except Exception as exc: # ruff:ignore[blind-except]
self.stderr.write(
self.style.ERROR(f" Failed to fetch {label}.json: {exc}"),
)
@ -894,7 +894,7 @@ class Command(BaseCommand):
self.stdout.write("\n" + "=" * 60)
def _parse_and_import_drops( # noqa: PLR0913
def _parse_and_import_drops( # ruff:ignore[too-many-arguments]
self,
raw_data: list[dict[str, Any]],
source: str,
@ -976,7 +976,7 @@ class Command(BaseCommand):
return total_campaigns
def _process_sunkwibot_reward( # noqa: PLR0915
def _process_sunkwibot_reward( # ruff:ignore[too-many-statements]
self,
reward: SunkwiBotRewardSchema,
game_obj: Game,
@ -1133,7 +1133,7 @@ class Command(BaseCommand):
self,
tbd_schema: SunkwiBotTimeBasedDropSchema,
campaign_obj: DropCampaign,
source: str, # noqa: ARG002
source: str, # ruff:ignore[unused-method-argument]
*,
verbose: bool,
) -> None:
@ -1335,7 +1335,7 @@ class Command(BaseCommand):
skip_existing=skip_existing,
)
def _parse_and_import_rewards( # noqa: PLR0913
def _parse_and_import_rewards( # ruff:ignore[too-many-arguments]
self,
raw_data: list[dict[str, Any]],
source: str,
@ -1563,12 +1563,12 @@ class Command(BaseCommand):
self._org_cache[twitch_id] = org_obj
return org_obj
def _get_or_create_game( # noqa: PLR0913
def _get_or_create_game( # ruff:ignore[too-many-arguments]
self,
twitch_id: str,
display_name: str,
box_art_url: str,
source: str, # noqa: ARG002
source: str, # ruff:ignore[unused-method-argument]
*,
verbose: bool,
org_obj: Organization | None = None,

View file

@ -37,7 +37,7 @@ class Command(BaseCommand):
help="Path to directory to watch for JSON files",
)
def handle(self, *args, **options) -> None: # noqa: ARG002
def handle(self, *args, **options) -> None: # ruff:ignore[unused-method-argument]
"""Main entry point for the watch command.
Args:
@ -87,7 +87,7 @@ class Command(BaseCommand):
importer_command: An instance of the BetterImportDropsCommand to handle the import logic.
watch_path: The directory path to watch for JSON files.
"""
# TODO(TheLovinator): Implement actual file watching using watchdog or similar library. # noqa: TD003
# TODO(TheLovinator): Implement actual file watching using watchdog or similar library. # ruff:ignore[missing-todo-link]
json_files: list[Path] = [
f for f in watch_path.iterdir() if f.suffix == ".json" and f.is_file()
]