diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 9ddadf6903..408ddb5baa 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -15,12 +15,12 @@ from collections import defaultdict from functools import cached_property, singledispatchmethod from pathlib import Path -from typing import TYPE_CHECKING, Any, Protocol +from typing import TYPE_CHECKING, Any, Protocol, Unpack import confuse import yaml -from beets import config, library, plugins, ui +from beets import config, events, library, plugins, ui from beets.library import Album, Item from beets.util import plurality, unique_list from beetsplug.lastgenre.utils import is_ignored, normalize_genre @@ -138,6 +138,7 @@ def setup(self) -> None: """Setup plugin from config options""" if self.config["auto"]: self.import_stages = [self.imported] + self.register_listener("item_imported", self.item_imported) self.whitelist: Whitelist = self._load_whitelist() self.c14n_branches: CanonTree @@ -826,4 +827,10 @@ def lastgenre_func( return [lastgenre_cmd] def imported(self, _: ImportSession, task: ImportTask) -> None: - self._process(task.album if task.is_album else task.item, write=False) # type: ignore[attr-defined] + if task.is_album: + self._process(task.album, write=False) + + def item_imported( + self, **kwargs: Unpack[events.ItemImportedEventArgs] + ) -> None: + self._process(kwargs["item"], write=False) diff --git a/docs/changelog.rst b/docs/changelog.rst index 60080c7a8c..4db2b225ac 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -167,6 +167,8 @@ Bug fixes instead of silently falling back to plain lyrics. :bug:`7070` - :doc:`plugins/info`: Fix a :class:`TypeError` when printing list-valued tag fields with non-string elements, such as ``synced_lyrics``. :bug:`7183` +- :doc:`plugins/lastgenre`: Fix lastgenre not triggering on singleton imports in + auto mode. :bug:`7190` For plugin developers ~~~~~~~~~~~~~~~~~~~~~ diff --git a/test/plugins/test_lastgenre.py b/test/plugins/test_lastgenre.py index e27bade2b0..1e510cf6de 100644 --- a/test/plugins/test_lastgenre.py +++ b/test/plugins/test_lastgenre.py @@ -160,6 +160,19 @@ def unexpected_store(*_, **__): assert album.genres == ["Original Genre"] assert album.items()[0].genres == ["Original Genre"] + def test_item_imported_processes_singleton(self): + item = self.create_item( + album="Singleton Album", + albumartist="Singleton Artist", + artist="Singleton Artist", + title="Singleton Track", + ) + + with patch.object(self.plugin, "_process") as process: + self.plugin.item_imported(lib=self.lib, item=item) + + process.assert_called_once_with(item, write=False) + def _add_album_for_import(self) -> Album: item = self.create_item( album="Import Album",