Skip to content

Commit aa44ca2

Browse files
committed
Fix typing issues
1 parent 168c554 commit aa44ca2

4 files changed

Lines changed: 38 additions & 28 deletions

File tree

beetsplug/mbpseudo.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
from beets.autotag.distance import Distance
4242
from beets.autotag.hooks import AlbumMatch
4343
from beets.library import Item
44-
from beetsplug._typing import JSONDict
4544

4645
from ._utils.musicbrainz import (
4746
Release,
@@ -179,7 +178,9 @@ def _intercept_mb_release(self, data: Release) -> list[str]:
179178
is not None
180179
]
181180

182-
def _has_desired_script(self, release: Release) -> bool:
181+
def _has_desired_script(
182+
self, release: Release | ReleaseRelationRelease
183+
) -> bool:
183184
if len(self._scripts) == 0:
184185
return False
185186
elif script := release.get("text_representation", {}).get("script"):
@@ -212,9 +213,7 @@ def _wanted_pseudo_release_id(
212213
return None
213214

214215
def _replace_artist_with_alias(
215-
self,
216-
raw_pseudo_release: JSONDict,
217-
pseudo_release: AlbumInfo,
216+
self, raw_pseudo_release: Release, pseudo_release: AlbumInfo
218217
):
219218
"""Use the pseudo-release's language to search for artist
220219
alias if the user hasn't configured import languages."""

beetsplug/musicbrainz.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -379,17 +379,17 @@ def track_info(
379379
if r["type"] == "remixer"
380380
] or None
381381

382-
if recording.get("length"):
383-
info.length = int(recording["length"]) / 1000.0
382+
if length := recording.get("length"):
383+
info.length = length / 1000.0
384384

385385
info.trackdisambig = recording.get("disambiguation")
386386

387387
if recording.get("isrcs"):
388388
info.isrc = ";".join(recording["isrcs"])
389389

390-
lyricists = []
391-
composer = []
392-
composer_sort = []
390+
lyricists: list[str] = []
391+
composers: list[str] = []
392+
composer_sort: list[str] = []
393393
for work_relation in recording.get("work_relations", ()):
394394
if work_relation["type"] != "performance":
395395
continue
@@ -406,14 +406,14 @@ def track_info(
406406
if type == "lyricist":
407407
lyricists.append(artist_relation["artist"]["name"])
408408
elif type == "composer":
409-
composer.append(artist_relation["artist"]["name"])
409+
composers.append(artist_relation["artist"]["name"])
410410
composer_sort.append(
411411
artist_relation["artist"]["sort_name"]
412412
)
413413
if lyricists:
414414
info.lyricists = lyricists
415-
if composer:
416-
info.composers = composer
415+
if composers:
416+
info.composers = composers
417417
info.composer_sort = ", ".join(composer_sort)
418418

419419
arrangers = []
@@ -547,8 +547,8 @@ def album_info(self, release: Release) -> AlbumInfo:
547547

548548
ti.artists_ids = _artist_ids(track["artist_credit"])
549549
ti.artist_id = ti.artists_ids[0]
550-
if track.get("length"):
551-
ti.length = int(track["length"]) / (1000.0)
550+
if length := track.get("length"):
551+
ti.length = length / 1000.0
552552

553553
track_infos.append(ti)
554554

@@ -614,7 +614,7 @@ def album_info(self, release: Release) -> AlbumInfo:
614614

615615
# Release events.
616616
info.country, release_date = _preferred_release_event(release)
617-
release_group_date = release["release_group"].get("first_release_date")
617+
release_group_date = release["release_group"]["first_release_date"]
618618
if not release_date:
619619
# Fall back if release-specific date is not available.
620620
release_date = release_group_date
@@ -682,9 +682,11 @@ def album_info(self, release: Release) -> AlbumInfo:
682682
url_source.capitalize(),
683683
)
684684

685-
for source, url in urls.items():
685+
for url_source, url in urls.items():
686686
setattr(
687-
info, f"{source}_album_id", extract_release_id(source, url)
687+
info,
688+
f"{url_source}_album_id",
689+
extract_release_id(url_source, url),
688690
)
689691

690692
extra_albumdatas = plugins.send("mb_album_extract", data=release)

test/plugins/test_mbpseudo.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import pathlib
2121

2222
from beetsplug._typing import JSONDict
23+
from beetsplug._utils.musicbrainz import Release
2324

2425

2526
@pytest.fixture(scope="module")
@@ -28,13 +29,13 @@ def rsrc_dir(pytestconfig: pytest.Config):
2829

2930

3031
@pytest.fixture
31-
def official_release(rsrc_dir: pathlib.Path) -> JSONDict:
32+
def official_release(rsrc_dir: pathlib.Path) -> Release:
3233
info_json = (rsrc_dir / "official_release.json").read_text(encoding="utf-8")
3334
return json.loads(info_json)
3435

3536

3637
@pytest.fixture
37-
def pseudo_release(rsrc_dir: pathlib.Path) -> JSONDict:
38+
def pseudo_release(rsrc_dir: pathlib.Path) -> Release:
3839
info_json = (rsrc_dir / "pseudo_release.json").read_text(encoding="utf-8")
3940
return json.loads(info_json)
4041

@@ -141,7 +142,7 @@ def test_extract_id_uses_music_brainz_pattern(
141142
def test_album_info_for_pseudo_release(
142143
self,
143144
mbpseudo_plugin: MusicBrainzPseudoReleasePlugin,
144-
pseudo_release: JSONDict,
145+
pseudo_release: Release,
145146
):
146147
album_info = mbpseudo_plugin.album_info(pseudo_release)
147148
assert not isinstance(album_info, PseudoAlbumInfo)
@@ -159,10 +160,10 @@ def test_album_info_for_pseudo_release(
159160
def test_interception_skip_when_rel_values_dont_match(
160161
self,
161162
mbpseudo_plugin: MusicBrainzPseudoReleasePlugin,
162-
official_release: JSONDict,
163+
official_release: Release,
163164
json_key: str,
164165
):
165-
del official_release["release_relations"][0][json_key]
166+
del official_release["release_relations"][0][json_key] # type: ignore[misc]
166167

167168
album_info = mbpseudo_plugin.album_info(official_release)
168169
assert not isinstance(album_info, PseudoAlbumInfo)
@@ -171,7 +172,7 @@ def test_interception_skip_when_rel_values_dont_match(
171172
def test_interception_skip_when_script_doesnt_match(
172173
self,
173174
mbpseudo_plugin: MusicBrainzPseudoReleasePlugin,
174-
official_release: JSONDict,
175+
official_release: Release,
175176
):
176177
official_release["release_relations"][0]["release"][
177178
"text_representation"
@@ -184,7 +185,7 @@ def test_interception_skip_when_script_doesnt_match(
184185
def test_interception(
185186
self,
186187
mbpseudo_plugin: MusicBrainzPseudoReleasePlugin,
187-
official_release: JSONDict,
188+
official_release: Release,
188189
):
189190
album_info = mbpseudo_plugin.album_info(official_release)
190191
assert isinstance(album_info, PseudoAlbumInfo)
@@ -245,7 +246,7 @@ def test_custom_tags(
245246
self,
246247
config,
247248
mbpseudo_plugin: MusicBrainzPseudoReleasePlugin,
248-
official_release: JSONDict,
249+
official_release: Release,
249250
):
250251
config["import"]["languages"] = ["en", "jp"]
251252
album_info = mbpseudo_plugin.album_info(official_release)
@@ -260,7 +261,7 @@ def test_custom_tags_with_import_languages(
260261
self,
261262
config,
262263
mbpseudo_plugin: MusicBrainzPseudoReleasePlugin,
263-
official_release: JSONDict,
264+
official_release: Release,
264265
):
265266
config["import"]["languages"] = []
266267
album_info = mbpseudo_plugin.album_info(official_release)

test/plugins/test_musicbrainz.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ def test_follow_pseudo_releases(self):
955955
],
956956
"release_group": {
957957
"id": "another-id",
958+
"first_release_date": "2009",
958959
},
959960
"release_relations": [
960961
{
@@ -997,6 +998,7 @@ def test_follow_pseudo_releases(self):
997998
],
998999
"release_group": {
9991000
"id": "another-id",
1001+
"first_release_date": "2009",
10001002
},
10011003
"country": "COUNTRY",
10021004
},
@@ -1042,6 +1044,7 @@ def test_pseudo_releases_with_empty_links(self):
10421044
],
10431045
"release_group": {
10441046
"id": "another-id",
1047+
"first_release_date": "2009",
10451048
},
10461049
}
10471050
]
@@ -1086,6 +1089,7 @@ def test_pseudo_releases_without_links(self):
10861089
],
10871090
"release_group": {
10881091
"id": "another-id",
1092+
"first_release_date": "2009",
10891093
},
10901094
}
10911095
]
@@ -1130,6 +1134,7 @@ def test_pseudo_releases_with_unsupported_links(self):
11301134
],
11311135
"release_group": {
11321136
"id": "another-id",
1137+
"first_release_date": "2009",
11331138
},
11341139
"release_relations": [
11351140
{
@@ -1239,7 +1244,10 @@ def test_candidates(self, monkeypatch, mb):
12391244
"artist_credit": [
12401245
{"artist": {"name": "some-artist", "id": "some-id"}}
12411246
],
1242-
"release_group": {"id": "another-id"},
1247+
"release_group": {
1248+
"id": "another-id",
1249+
"first_release_date": "2009",
1250+
},
12431251
},
12441252
)
12451253
candidates = list(mb.candidates([], "hello", "there", False))

0 commit comments

Comments
 (0)