Introduce Candidates.from_source method

This commit is contained in:
Šarūnas Nejus
2026-05-30 17:22:52 +01:00
parent 84809bd905
commit d104644fee
3 changed files with 12 additions and 12 deletions

View File

@@ -36,7 +36,7 @@ class Recommendation(IntEnum):
@dataclass
class Candidates(Generic[InfoT, MatchT], Sequence[MatchT]):
class Candidates(Sequence[MatchT], Generic[InfoT, MatchT]):
MATCH_CLASS: ClassVar[type[MatchT]]
source: Source
@@ -57,6 +57,11 @@ class Candidates(Generic[InfoT, MatchT], Sequence[MatchT]):
def __getitem__(self, i: int | slice) -> MatchT | Sequence[MatchT]:
return self.matches[i]
@classmethod
def from_source(cls, source: Source) -> Candidates[Any, Any]:
_class = AlbumCandidates if source.type == "album" else TrackCandidates
return _class(source)
@property
def matches(self) -> list[MatchT]:
return sorted(self.candidate_by_id.values(), key=lambda m: m.distance)

View File

@@ -14,7 +14,7 @@ from typing import TYPE_CHECKING, Any
import mediafile
from beets import config, library, plugins, util
from beets.autotag import AlbumCandidates, AlbumMatch, Source, TrackCandidates
from beets.autotag import AlbumMatch, Candidates, Source
from beets.dbcore.query import PathQuery
from beets.util import extension
from beets.util.extension import remux_mpeglayer3_wav
@@ -25,7 +25,7 @@ from .state import ImportState
if TYPE_CHECKING:
from collections.abc import Iterable, Mapping
from beets.autotag import Candidates, TrackMatch
from beets.autotag import TrackMatch
from .session import ImportSession
@@ -112,8 +112,7 @@ class ImportTask(BaseImportTask):
The import session and stages call the following methods in the
given order.
* `lookup_candidates()` Sets the `common_artist`, `common_album`,
`candidates`, and `rec` attributes. `candidates` is a list of
* `lookup_candidates()` Resolves candidates. `candidates` is a list of
`AlbumMatch` objects.
* `choose_match()` Uses the session to set the `match` attribute
@@ -147,7 +146,7 @@ class ImportTask(BaseImportTask):
@cached_property
def candidates(self) -> Candidates[Any, Any]:
return AlbumCandidates(self.source)
return Candidates.from_source(self.source)
def __init__(
self,
@@ -665,10 +664,6 @@ class SingletonImportTask(ImportTask):
def source(self) -> Source:
return Source.from_item(self.item)
@cached_property
def candidates(self) -> TrackCandidates:
return TrackCandidates(self.source)
def __init__(
self, toppath: util.PathBytes | None, item: library.Item
) -> None:

View File

@@ -4,7 +4,7 @@ import cProfile
import timeit
from beets import importer, plugins, ui
from beets.autotag import AlbumCandidates, Source
from beets.autotag import Candidates, Source
from beets.plugins import BeetsPlugin
from beets.util.functemplate import Template
from beets.util.pathformats import PF_KEY_DEFAULT
@@ -68,7 +68,7 @@ def match_benchmark(lib, prof, query=None, album_id=None):
# Run the match.
def _run_match():
source = Source.from_items(items)
AlbumCandidates(source).resolve([album_id])
Candidates.from_source(source).resolve([album_id])
if prof:
cProfile.runctx(