1919from __future__ import annotations
2020
2121from enum import IntEnum
22- from typing import TYPE_CHECKING , Any , NamedTuple , TypeVar
22+ from typing import TYPE_CHECKING , NamedTuple , TypeVar
2323
2424import lap
2525import numpy as np
3535
3636 from beets .library import Item
3737
38+ from .hooks import Info
39+
40+ AnyMatch = TypeVar ("AnyMatch" , TrackMatch , AlbumMatch )
41+ Candidates = dict [Info .Identifier , AnyMatch ]
42+
3843# Global logger.
3944log = logging .getLogger ("beets" )
4045
@@ -179,33 +184,33 @@ def _recommendation(
179184 return rec
180185
181186
182- AnyMatch = TypeVar ("AnyMatch" , TrackMatch , AlbumMatch )
183-
184-
185187def _sort_candidates (candidates : Iterable [AnyMatch ]) -> Sequence [AnyMatch ]:
186188 """Sort candidates by distance."""
187189 return sorted (candidates , key = lambda match : match .distance )
188190
189191
190192def _add_candidate (
191193 items : Sequence [Item ],
192- results : dict [ Any , AlbumMatch ],
194+ results : Candidates [ AlbumMatch ],
193195 info : AlbumInfo ,
194196):
195197 """Given a candidate AlbumInfo object, attempt to add the candidate
196198 to the output dictionary of AlbumMatch objects. This involves
197199 checking the track count, ordering the items, checking for
198200 duplicates, and calculating the distance.
199201 """
200- log .debug ("Candidate: {0.artist} - {0.album} ({0.album_id})" , info )
202+ log .debug (
203+ "Candidate: {0.artist} - {0.album} ({0.album_id}) from {0.data_source}" ,
204+ info ,
205+ )
201206
202207 # Discard albums with zero tracks.
203208 if not info .tracks :
204209 log .debug ("No tracks." )
205210 return
206211
207212 # Prevent duplicates.
208- if info .album_id and info .album_id in results :
213+ if info .album_id and info .identifier in results :
209214 log .debug ("Duplicate." )
210215 return
211216
@@ -233,7 +238,7 @@ def _add_candidate(
233238 return
234239
235240 log .debug ("Success. Distance: {}" , dist )
236- results [info .album_id ] = hooks .AlbumMatch (
241+ results [info .identifier ] = hooks .AlbumMatch (
237242 dist , info , dict (item_info_pairs ), extra_items , extra_tracks
238243 )
239244
@@ -268,15 +273,15 @@ def tag_album(
268273 log .debug ("Tagging {} - {}" , cur_artist , cur_album )
269274
270275 # The output result, keys are the MB album ID.
271- candidates : dict [ Any , AlbumMatch ] = {}
276+ candidates : Candidates [ AlbumMatch ] = {}
272277
273278 # Search by explicit ID.
274279 if search_ids :
275280 for search_id in search_ids :
276281 log .debug ("Searching for album ID: {}" , search_id )
277282 for _info in metadata_plugins .albums_for_ids (search_id ):
278283 _add_candidate (items , candidates , _info )
279- if opt_candidate := candidates .get (_info .album_id ):
284+ if opt_candidate := candidates .get (_info .identifier ):
280285 plugins .send ("album_matched" , match = opt_candidate )
281286
282287 # Use existing metadata or text search.
@@ -320,7 +325,7 @@ def tag_album(
320325 items , search_artist , search_name , va_likely
321326 ):
322327 _add_candidate (items , candidates , matched_candidate )
323- if opt_candidate := candidates .get (matched_candidate .album_id ):
328+ if opt_candidate := candidates .get (matched_candidate .identifier ):
324329 plugins .send ("album_matched" , match = opt_candidate )
325330
326331 log .debug ("Evaluating {} candidates." , len (candidates ))
@@ -345,7 +350,7 @@ def tag_item(
345350 """
346351 # Holds candidates found so far: keys are MBIDs; values are
347352 # (distance, TrackInfo) pairs.
348- candidates = {}
353+ candidates : Candidates [ TrackMatch ] = {}
349354 rec : Recommendation | None = None
350355
351356 # First, try matching by the external source ID.
@@ -355,7 +360,7 @@ def tag_item(
355360 log .debug ("Searching for track ID: {}" , trackid )
356361 for info in metadata_plugins .tracks_for_ids (trackid ):
357362 dist = track_distance (item , info , incl_artist = True )
358- candidates [info .track_id ] = hooks .TrackMatch (dist , info )
363+ candidates [info .identifier ] = hooks .TrackMatch (dist , info )
359364
360365 # If this is a good match, then don't keep searching.
361366 rec = _recommendation (_sort_candidates (candidates .values ()))
@@ -381,7 +386,7 @@ def tag_item(
381386 item , search_artist , search_name
382387 ):
383388 dist = track_distance (item , track_info , incl_artist = True )
384- candidates [track_info .track_id ] = hooks .TrackMatch (dist , track_info )
389+ candidates [track_info .identifier ] = hooks .TrackMatch (dist , track_info )
385390
386391 # Sort by distance and return with recommendation.
387392 log .debug ("Found {} candidates." , len (candidates ))
0 commit comments