mirror of
https://github.com/ankitects/anki.git
synced 2026-09-10 10:59:28 -04:00
## Linked issue Fixes #5355 ## Summary / motivation Two locations used `assert` statements inside broad `except` blocks, flagged by SonarCloud rule [python:S5779](https://sonarcloud.io/project/issues?rules=python%3AS5779&issueStatuses=OPEN%2CCONFIRMED&id=ankitects_anki). This is problematic because: - `AssertionError` is caught by the surrounding `except`, so the assertion is silently swallowed instead of surfacing a meaningful error. - Under `python -O`, `assert` statements are stripped entirely, so the check disappears in optimized builds and the failure resurfaces later as an opaque `AttributeError`. Changes: - **`qt/aqt/addons.py`** (`download_addon`): replace `assert match is not None` with `if match is None: raise ValueError(...)` naming the unexpected `content-disposition` header. The raise is still caught by the existing handler and returned as a `DownloadError`, now with a descriptive message. - **`qt/aqt/editor_legacy.py`** (`setup_mask_editor`): replace `assert self.note is not None` with a guard that warns the user (`tr.browsing_no_selection()`) and returns early. ## How to test (required) ### Details - `just lint` ✅ - `just test-py` ✅ — includes a new regression test, `test_download_addon_rejects_bad_content_disposition`, covering the malformed `content-disposition` path in `download_addon`. - No test added for the `editor_legacy.py` guard: it defends an effectively unreachable state (a missing note while editing an existing note), and that Qt-side method isn't reachable from the web e2e harness. ## Before / after behavior - **addons.py** — Before: malformed header → empty/opaque `DownloadError` (or `AttributeError` under `-O`). After: `DownloadError` carrying a `ValueError` that names the offending header. - **editor_legacy.py** — Before: missing note → empty warning dialog (blank `AssertionError` message). After: a clear warning, and early return. ## Scope - [x] This PR is focused on one change (no unrelated edits).