From 15fe7705363645ede903ec744bcdf7f6fef8e2f3 Mon Sep 17 00:00:00 2001 From: Ziad Arafat Date: Wed, 25 Nov 2026 17:00:02 -0700 Subject: [PATCH] Keep the MusicBrainz tests typing clean and formatted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The release-group branch landed with test-only type errors and unformatted modules, so mise run check failed before the cog could ship. Generated with Codebuff 🤖 Co-Authored-By: Codebuff --- tests/test_music_mb_resolve.py | 10 ++++++---- tests/test_music_release_preference.py | 20 +++++++++++++++----- tests/test_musicbrainz.py | 10 ++++++---- watch/music.py | 4 +--- watch/musicbrainz.py | 4 +--- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/tests/test_music_mb_resolve.py b/tests/test_music_mb_resolve.py index 880b77b..94a271c 100644 --- a/tests/test_music_mb_resolve.py +++ b/tests/test_music_mb_resolve.py @@ -1,6 +1,6 @@ """MusicBrainz containment and resolution inside the album importer.""" -from typing import Any +from typing import Any, cast def album_row( @@ -80,7 +80,7 @@ class FakeArr: async def __call__(self, path: str, **kwargs: Any) -> Any: # noqa: ANN401 """Serve the handful of routes the importer uses.""" params = kwargs.get("params") or {} - body = kwargs.get("body") + body = cast("dict[str, Any] | None", kwargs.get("body")) method = kwargs.get("method") or ("POST" if body else "GET") if path == "album/lookup": return self.lookup @@ -89,6 +89,7 @@ class FakeArr: row = self.catalog.get(mbid) if mbid else None return [row] if row else [] if path == "album" and method == "POST": + assert body is not None self.added.append(body) row = dict(body) row["id"] = row.get("id") or (900 + len(self.added)) @@ -115,6 +116,7 @@ class FakeArr: } ] if path == "command": + assert body is not None self.searches.append(body["albumIds"]) return {"id": 1} return {} @@ -132,8 +134,8 @@ async def engine_with(arr: FakeArr, brainz: Any | None) -> Any: # noqa: ANN401 return MusicImporter( arr, - None, - None, + cast("Any", None), + cast("Any", None), save, authorize, musicbrainz=brainz, diff --git a/tests/test_music_release_preference.py b/tests/test_music_release_preference.py index ee6e941..77e450a 100644 --- a/tests/test_music_release_preference.py +++ b/tests/test_music_release_preference.py @@ -47,10 +47,14 @@ def test_an_ep_wins_over_a_single_but_not_over_an_album() -> None: album(1, "Levels", "Single"), album(2, "Levels", "EP"), ] - assert choose("Levels", candidates)["id"] == 2 + selected = choose("Levels", candidates) + assert selected is not None + assert selected["id"] == 2 candidates.append(album(3, "Levels", "Album")) - assert choose("Levels", candidates)["id"] == 3 + selected = choose("Levels", candidates) + assert selected is not None + assert selected["id"] == 3 def test_a_plain_album_wins_over_a_compilation_or_live_edition() -> None: @@ -60,7 +64,9 @@ def test_a_plain_album_wins_over_a_compilation_or_live_edition() -> None: album(2, "True", "Album", ["Live"]), album(3, "True", "Album"), ] - assert choose("True", candidates)["id"] == 3 + selected = choose("True", candidates) + assert selected is not None + assert selected["id"] == 3 def test_a_single_wins_over_a_remix_edition() -> None: @@ -69,7 +75,9 @@ def test_a_single_wins_over_a_remix_edition() -> None: album(1, "Wake Me Up", "Single", ["Remix"]), album(2, "Wake Me Up", "Single"), ] - assert choose("Wake Me Up", candidates)["id"] == 2 + selected = choose("Wake Me Up", candidates) + assert selected is not None + assert selected["id"] == 2 def test_equally_ranked_albums_stay_ambiguous() -> None: @@ -110,4 +118,6 @@ def test_release_preference_never_overrides_the_artist_check() -> None: "artist": {"artistName": "A Tribute Band"}, }, ] - assert choose("True", candidates)["id"] == 1 + selected = choose("True", candidates) + assert selected is not None + assert selected["id"] == 1 diff --git a/tests/test_musicbrainz.py b/tests/test_musicbrainz.py index 3900b0f..34f529f 100644 --- a/tests/test_musicbrainz.py +++ b/tests/test_musicbrainz.py @@ -1,6 +1,6 @@ """MusicBrainz resolves a track to the release groups that contain it.""" -from typing import Any +from typing import Any, cast GROUP_SINGLE = { "id": "rg-single", @@ -159,7 +159,7 @@ async def test_rate_limit_is_retried_then_succeeds() -> None: async def sleep(seconds: float) -> None: delays.append(seconds) - mb = MusicBrainz(client, sleep=sleep, clock=lambda: 0.0) + mb = MusicBrainz(cast("Any", client), sleep=sleep, clock=lambda: 0.0) groups = await mb.release_groups("Avicii", "Wake Me Up") assert [g["foreignAlbumId"] for g in groups] == ["rg-single", "rg-comp"] assert len(client.calls) == 2 @@ -175,7 +175,7 @@ async def test_repeated_failure_degrades_to_no_groups() -> None: async def sleep(_seconds: float) -> None: return None - mb = MusicBrainz(client, sleep=sleep, clock=lambda: 0.0) + mb = MusicBrainz(cast("Any", client), sleep=sleep, clock=lambda: 0.0) assert await mb.release_groups("Avicii", "Wake Me Up") == [] @@ -191,7 +191,9 @@ async def test_calls_are_spaced_by_one_second() -> None: delays.append(seconds) now["t"] += seconds - mb = MusicBrainz(client, sleep=sleep, clock=lambda: now["t"]) + mb = MusicBrainz( + cast("Any", client), sleep=sleep, clock=lambda: now["t"] + ) await mb.release_groups("Avicii", "Wake Me Up") await mb.release_groups("Avicii", "Hey Brother") assert delays == [1.0] diff --git a/watch/music.py b/watch/music.py index 89712c2..48555d3 100644 --- a/watch/music.py +++ b/watch/music.py @@ -128,9 +128,7 @@ def choose_album( if holding: # A release group MusicBrainz says holds the track beats # one that only outranks it on type. - held = [ - a for a in matches if a["foreignAlbumId"] in holding - ] + held = [a for a in matches if a["foreignAlbumId"] in holding] if held: matches = held unique = {a["foreignAlbumId"]: a for a in matches} diff --git a/watch/musicbrainz.py b/watch/musicbrainz.py index 1a221ed..e2e43d2 100644 --- a/watch/musicbrainz.py +++ b/watch/musicbrainz.py @@ -59,9 +59,7 @@ def parse_recordings(payload: dict[str, Any]) -> list[dict[str, Any]]: if credited and isinstance(credited[0], dict): name = credited[0].get("name") or "" primary = meta.get("primary-type") or "" - secondary = list( - meta.get("secondary-types") or [] - ) + secondary = list(meta.get("secondary-types") or []) row = groups.setdefault( group_id, {