Skip to content

Commit 397d7df

Browse files
committed
Unified search string construction between albums and items.
1 parent c26c342 commit 397d7df

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

beets/autotag/match.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ def _add_candidate(
236236
)
237237

238238

239+
def _parse_search_terms(
240+
*pairs: tuple[str | None, str],
241+
) -> tuple[str, ...]:
242+
"""Determine the search terms to use, falling back to current values."""
243+
return tuple(term or current for term, current in pairs)
244+
245+
239246
def tag_album(
240247
items,
241248
search_artist: str | None = None,
@@ -295,22 +302,23 @@ def tag_album(
295302
)
296303

297304
# Search terms.
298-
if not (search_artist and search_album):
299-
# No explicit search terms -- use current metadata.
300-
search_artist, search_album = cur_artist, cur_album
301-
log.debug("Search terms: {} - {}", search_artist, search_album)
305+
_search_artist, _search_album = _parse_search_terms(
306+
(search_artist, cur_artist),
307+
(search_album, cur_album),
308+
)
309+
log.debug("Search terms: {} - {}", _search_artist, _search_album)
302310

303311
# Is this album likely to be a "various artist" release?
304312
va_likely = (
305313
(not consensus["artist"])
306-
or (search_artist.lower() in VA_ARTISTS)
314+
or (_search_artist.lower() in VA_ARTISTS)
307315
or any(item.comp for item in items)
308316
)
309317
log.debug("Album might be VA: {}", va_likely)
310318

311319
# Get the results from the data sources.
312320
for matched_candidate in metadata_plugins.candidates(
313-
items, search_artist, search_album, va_likely
321+
items, _search_artist, _search_album, va_likely
314322
):
315323
_add_candidate(items, candidates, matched_candidate)
316324

@@ -322,7 +330,7 @@ def tag_album(
322330

323331

324332
def tag_item(
325-
item,
333+
item: Item,
326334
search_artist: str | None = None,
327335
search_title: str | None = None,
328336
search_ids: list[str] | None = None,
@@ -365,13 +373,17 @@ def tag_item(
365373
return Proposal([], Recommendation.none)
366374

367375
# Search terms.
368-
search_artist = search_artist or item.artist
369-
search_title = search_title or item.title
370-
log.debug("Item search terms: {} - {}", search_artist, search_title)
376+
_search_artist, _search_title = _parse_search_terms(
377+
(search_artist, item.artist),
378+
(search_title, item.title),
379+
)
380+
log.debug("Item search terms: {} - {}", _search_artist, _search_title)
371381

372382
# Get and evaluate candidate metadata.
373383
for track_info in metadata_plugins.item_candidates(
374-
item, search_artist, search_title
384+
item,
385+
_search_artist,
386+
_search_title,
375387
):
376388
dist = track_distance(item, track_info, incl_artist=True)
377389
candidates[track_info.track_id] = hooks.TrackMatch(dist, track_info)

beets/metadata_plugins.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,35 @@ def find_metadata_source_plugins() -> list[MetadataSourcePlugin]:
3535

3636

3737
@notify_info_yielded("albuminfo_received")
38-
def candidates(*args, **kwargs) -> Iterable[AlbumInfo]:
38+
def candidates(
39+
items: Sequence[Item],
40+
artist: str,
41+
album: str,
42+
va_likely: bool,
43+
) -> Iterable[AlbumInfo]:
3944
"""Return matching album candidates from all metadata source plugins."""
4045
for plugin in find_metadata_source_plugins():
41-
yield from plugin.candidates(*args, **kwargs)
46+
yield from plugin.candidates(
47+
items=items,
48+
artist=artist,
49+
album=album,
50+
va_likely=va_likely,
51+
)
4252

4353

4454
@notify_info_yielded("trackinfo_received")
45-
def item_candidates(*args, **kwargs) -> Iterable[TrackInfo]:
46-
"""Return matching track candidates fromm all metadata source plugins."""
55+
def item_candidates(
56+
item: Item,
57+
artist: str,
58+
title: str,
59+
) -> Iterable[TrackInfo]:
60+
"""Return matching track candidates from all metadata source plugins."""
4761
for plugin in find_metadata_source_plugins():
48-
yield from plugin.item_candidates(*args, **kwargs)
62+
yield from plugin.item_candidates(
63+
item=item,
64+
artist=artist,
65+
title=title,
66+
)
4967

5068

5169
def album_for_id(_id: str) -> AlbumInfo | None:
@@ -157,15 +175,22 @@ def candidates(
157175

158176
@abc.abstractmethod
159177
def item_candidates(
160-
self, item: Item, artist: str, title: str
178+
self,
179+
item: Item,
180+
artist: str,
181+
title: str,
161182
) -> Iterable[TrackInfo]:
162183
"""Return :py:class:`TrackInfo` candidates that match the given track.
163184
164185
Used in the autotag functionality to search for tracks.
165186
166187
:param item: Track item
167-
:param artist: Track artist
168-
:param title: Track title
188+
:param artist: Track artist, either a search manually provided or
189+
preprocessed from the item. If no metadata is available an empty string
190+
is passed.
191+
:param title: Track title, either a search manually provided or
192+
preprocessed from the item. If no metadata is available an empty string
193+
is passed.
169194
"""
170195
raise NotImplementedError
171196

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ For packagers:
1919

2020
Other changes:
2121

22+
- Standardized ``search_*`` parameter handling in autotag matchers. Manual album and
23+
singleton searches now behave consistently: when the prompt does not specify a
24+
search query, the system defaults to using the corresponding metadata value.
25+
2226
2.5.1 (October 14, 2025)
2327
------------------------
2428

0 commit comments

Comments
 (0)