Remove unused imports, variables, and fix minor CodeQL findings

- Remove 28 unused imports across 22 test files
- Prefix 4 unused local variables with _ in app code
  (archives, bambu_mqtt, main) and remove 1 dead store
- Consolidate import/import-from in test_plate_detection.py
- Fix unreachable statement in test_archive_service.py
- Simplify redundant comparison in timelapse_processor.py

Resolves ~50 CodeQL py/unused-import, py/unused-local-variable,
py/import-and-import-from, py/unreachable-statement, and
py/redundant-comparison findings.
This commit is contained in:
maziggy 2026-02-06 12:19:17 +01:00
parent 5dcabbdda8
commit b99536cc33
26 changed files with 39 additions and 71 deletions

View file

@ -1270,7 +1270,7 @@ async def scan_timelapse(
if not matching_file and (archive.started_at or archive.completed_at or archive.created_at):
from datetime import datetime, timedelta
archive_start = archive.started_at
_archive_start = archive.started_at
archive_end = archive.completed_at or archive.created_at
best_match = None
best_diff = timedelta(hours=24)

View file

@ -963,7 +963,7 @@ async def on_print_start(printer_id: int, data: dict):
existing_archive.failure_reason = "Stale - print likely cancelled or failed without status update"
await db.commit()
# Fall through to create new archive (don't return)
existing_archive = None # Clear so we don't use stale archive
_existing_archive = None # Clear so we don't use stale archive
else:
logger.info(
f"Skipping duplicate - already have printing archive {existing_archive.id} for {check_name}"
@ -1325,7 +1325,6 @@ async def on_print_start(printer_id: int, data: dict):
if not notification_sent:
archive_data = {"print_time_seconds": archive.print_time_seconds}
await _send_print_start_notification(printer_id, data, archive_data, logger)
notification_sent = True
# Extract printable objects for skip object functionality
try:

View file

@ -1064,7 +1064,7 @@ class BambuMQTTClient:
def _update_state(self, data: dict):
"""Update printer state from message data."""
previous_state = self.state.state
_previous_state = self.state.state
# Update state fields
if "gcode_state" in data:
@ -2370,7 +2370,7 @@ class BambuMQTTClient:
def _handle_kprofile_response(self, data: dict):
"""Handle K-profile response from printer."""
response_nozzle = data.get("nozzle_diameter")
response_seq_id = data.get("sequence_id", "?")
_response_seq_id = data.get("sequence_id", "?")
filaments = data.get("filaments", [])
expected_nozzle = getattr(self, "_expected_kprofile_nozzle", None)
has_pending_request = self._pending_kprofile_response is not None

View file

@ -258,7 +258,8 @@ class TimelapseProcessor:
remaining_speed *= 2.0
# Add final atempo for remaining adjustment
if 0.5 <= remaining_speed <= 2.0 and remaining_speed != 1.0:
# After the while loops above, remaining_speed is guaranteed to be in [0.5, 2.0]
if remaining_speed != 1.0:
filters.append(f"atempo={remaining_speed:.4f}")
return ",".join(filters)

View file

@ -6,10 +6,8 @@ import json
import logging
import os
import shutil
import sys
import tempfile
from collections.abc import AsyncGenerator
from datetime import datetime
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch

View file

@ -3,7 +3,6 @@
Tests the full request/response cycle for /api/v1/printers/{id}/camera/ endpoints.
"""
import asyncio
from unittest.mock import AsyncMock, MagicMock, patch
import pytest

View file

@ -3,8 +3,6 @@
Tests the full request/response cycle for /api/v1/discovery/ endpoints.
"""
from unittest.mock import AsyncMock, patch
import pytest
from httpx import AsyncClient

View file

@ -4,7 +4,7 @@ Tests that verify endpoints properly enforce authentication when auth is enabled
and allow access when auth is disabled (CVE-2026-25505 fix verification).
"""
from unittest.mock import AsyncMock, patch
from unittest.mock import patch
import pytest
from httpx import AsyncClient

View file

@ -13,11 +13,9 @@ Full end-to-end tests require the actual database setup.
"""
import asyncio
from datetime import datetime
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from sqlalchemy import select
class TestPrintStartLogic:

View file

@ -3,7 +3,7 @@
Tests the full request/response cycle for /api/v1/printers/ endpoints.
"""
from unittest.mock import AsyncMock, MagicMock, patch
from unittest.mock import MagicMock, patch
import pytest
from httpx import AsyncClient

View file

@ -1,6 +1,6 @@
"""Integration tests for Updates API endpoints."""
from unittest.mock import AsyncMock, MagicMock, patch
from unittest.mock import AsyncMock, patch
import pytest
from httpx import AsyncClient

View file

@ -1,9 +1,6 @@
"""Unit tests for the archive service."""
from datetime import datetime
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
class TestArchiveServiceHelpers:
@ -364,41 +361,25 @@ class TestThreeMFPlateIndexExtraction:
# First priority should be plate_5.png
assert thumbnail_paths[0] == "Metadata/plate_5.png"
def test_print_name_enhanced_for_plate_greater_than_1(self):
"""Test that print_name is enhanced with plate info for plate > 1."""
plate_index = 5
print_name = "Benchy"
# Logic from archive.py
@staticmethod
def _enhance_print_name(print_name: str, plate_index: int) -> str:
"""Apply plate name enhancement logic from archive.py."""
if plate_index and plate_index > 1:
if print_name and f"Plate {plate_index}" not in print_name:
print_name = f"{print_name} - Plate {plate_index}"
return print_name
assert print_name == "Benchy - Plate 5"
def test_print_name_enhanced_for_plate_greater_than_1(self):
"""Test that print_name is enhanced with plate info for plate > 1."""
assert self._enhance_print_name("Benchy", 5) == "Benchy - Plate 5"
def test_print_name_not_enhanced_for_plate_1(self):
"""Test that print_name is NOT enhanced for plate 1."""
plate_index = 1
print_name = "Benchy"
# Logic from archive.py
if plate_index and plate_index > 1:
if print_name and f"Plate {plate_index}" not in print_name:
print_name = f"{print_name} - Plate {plate_index}"
assert print_name == "Benchy" # Unchanged for plate 1
assert self._enhance_print_name("Benchy", 1) == "Benchy"
def test_print_name_not_duplicated(self):
"""Test that plate info is not added if already present in print_name."""
plate_index = 5
print_name = "Benchy - Plate 5"
# Logic from archive.py
if plate_index and plate_index > 1:
if print_name and f"Plate {plate_index}" not in print_name:
print_name = f"{print_name} - Plate {plate_index}"
assert print_name == "Benchy - Plate 5" # Not duplicated
assert self._enhance_print_name("Benchy - Plate 5", 5) == "Benchy - Plate 5"
def test_high_plate_number_extraction(self):
"""Test extracting high plate numbers (e.g., plate 28)."""

View file

@ -4,8 +4,6 @@ Tests for the BambuMQTTClient service.
These tests focus on timelapse tracking during prints.
"""
from unittest.mock import MagicMock, patch
import pytest

View file

@ -4,7 +4,7 @@ Tests for the external camera service.
These tests cover pure functions and frame parsing logic.
"""
from unittest.mock import MagicMock, patch
from unittest.mock import patch
import pytest

View file

@ -1,7 +1,5 @@
"""Tests for HMS error code translations."""
import pytest
from backend.app.services.hms_errors import HMS_ERROR_DESCRIPTIONS, get_error_description

View file

@ -4,7 +4,6 @@ Tests event-based notifications and toggle behavior.
"""
import json
from datetime import datetime
from unittest.mock import AsyncMock, MagicMock, patch
import pytest

View file

@ -1,7 +1,5 @@
"""Unit tests for plate detection service."""
import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
@ -17,7 +15,12 @@ class TestPlateDetectionResult:
def test_result_to_dict(self):
"""Verify PlateDetectionResult.to_dict() returns correct structure."""
with patch.dict("sys.modules", {"cv2": cv2_mock, "numpy": np_mock}):
from backend.app.services.plate_detection import PlateDetectionResult
import importlib
import backend.app.services.plate_detection as pd_module
importlib.reload(pd_module)
PlateDetectionResult = pd_module.PlateDetectionResult
result = PlateDetectionResult(
is_empty=True,
@ -40,7 +43,12 @@ class TestPlateDetectionResult:
def test_result_with_debug_image(self):
"""Verify has_debug_image is True when debug_image is provided."""
with patch.dict("sys.modules", {"cv2": cv2_mock, "numpy": np_mock}):
from backend.app.services.plate_detection import PlateDetectionResult
import importlib
import backend.app.services.plate_detection as pd_module
importlib.reload(pd_module)
PlateDetectionResult = pd_module.PlateDetectionResult
result = PlateDetectionResult(
is_empty=False,
@ -57,7 +65,12 @@ class TestPlateDetectionResult:
def test_result_needs_calibration(self):
"""Verify needs_calibration flag is preserved."""
with patch.dict("sys.modules", {"cv2": cv2_mock, "numpy": np_mock}):
from backend.app.services.plate_detection import PlateDetectionResult
import importlib
import backend.app.services.plate_detection as pd_module
importlib.reload(pd_module)
PlateDetectionResult = pd_module.PlateDetectionResult
result = PlateDetectionResult(
is_empty=True,

View file

@ -3,9 +3,7 @@
Tests printer connection management, status tracking, and print control.
"""
import asyncio
from datetime import datetime
from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
import pytest

View file

@ -4,7 +4,6 @@ These tests specifically target the auto-off behavior and toggle functionality
that were identified as common regression points.
"""
import asyncio
from datetime import datetime
from unittest.mock import AsyncMock, MagicMock, patch

View file

@ -1,7 +1,5 @@
"""Unit tests for Spoolman tracking service helpers."""
import pytest
from backend.app.services.spoolman_tracking import (
_resolve_global_tray_id,
_resolve_spool_tag,

View file

@ -2,7 +2,6 @@
import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest

View file

@ -6,7 +6,6 @@ that could cause runtime errors but aren't caught by normal tests.
"""
import ast
import os
from pathlib import Path
import pytest

View file

@ -5,10 +5,6 @@ These tests use the capture_logs fixture to detect runtime errors
that might not cause test failures but indicate problems.
"""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
class TestMQTTMessageProcessingNoErrors:
"""Verify MQTT message processing doesn't log errors."""

View file

@ -1,6 +1,5 @@
"""Unit tests for plate object extraction from 3MF model_settings.config."""
import pytest
from defusedxml import ElementTree as ET

View file

@ -6,8 +6,6 @@ and cumulative layer usage lookup.
import math
import pytest
from backend.app.utils.threemf_tools import (
get_cumulative_usage_at_layer,
mm_to_grams,

View file

@ -8,7 +8,7 @@ are properly persisted to the database and survive page reloads.
import os
import time
from playwright.sync_api import expect, sync_playwright
from playwright.sync_api import sync_playwright
BASE_URL = os.environ.get("BAMBUDDY_URL", "http://localhost:8000")