Skip to content

Commit b11f52c

Browse files
committed
lastgenre: Dedup fallback handling in _get_genre
and fix fallback types
1 parent dde83f6 commit b11f52c

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

beetsplug/lastgenre/__init__.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,12 @@ def _get_genre(self, obj: LibModel) -> tuple[list[str], str]:
417417
and the whitelist feature was disabled.
418418
"""
419419

420+
def _fallback_stage() -> tuple[list[str], str]:
421+
"""Return the fallback genre and label."""
422+
if fallback := self.config["fallback"].get():
423+
return [fallback], "fallback"
424+
return [], "fallback unconfigured"
425+
420426
def _try_resolve_stage(
421427
stage_label: str,
422428
keep_genres: list[str],
@@ -453,8 +459,7 @@ def _try_resolve_stage(
453459
):
454460
return result
455461

456-
# Return fallback string (None if not set).
457-
return self.config["fallback"].get(), "fallback"
462+
return _fallback_stage()
458463

459464
# If cleanup_existing is not set, the pre-populated tags are
460465
# returned as-is.
@@ -548,7 +553,14 @@ def _try_resolve_stage(
548553

549554
# Nothing found, leave original if configured and valid.
550555
if genres and self.config["keep_existing"].get():
551-
artist = self._artist_for_filter(obj)
556+
if isinstance(obj, library.Item):
557+
# For track items, use track artist (important for compilations).
558+
artist = getattr(obj, "artist", None)
559+
else:
560+
# For albums, prefer albumartist, fall back to artist.
561+
artist = getattr(obj, "albumartist", None) or getattr(
562+
obj, "artist", None
563+
)
552564
if valid_genres := self._filter_valid(genres, artist=artist):
553565
return valid_genres, "original fallback"
554566
# If the original genre doesn't match a whitelisted genre, check
@@ -558,12 +570,7 @@ def _try_resolve_stage(
558570
):
559571
return result
560572

561-
# Return fallback as a list.
562-
if fallback := self.config["fallback"].get():
563-
return [fallback], "fallback"
564-
565-
# No fallback configured.
566-
return [], "fallback unconfigured"
573+
return _fallback_stage()
567574

568575
# Beets plugin hooks and CLI.
569576

0 commit comments

Comments
 (0)