Now we are able to type Results simply as a Sequence. This way, we can
also now use a more generic representation `Sequence[LibModel]` to refer
to *any* of the two models.
Fixes: https://github.com/beetbox/beets/issues/6921
- Adds explicit typing to the plugin event system in `beets.plugins`,
including per-event argument shapes and return types for
`register_listener()` and `send()`. This makes the listener API much
clearer and turns implicit plugin contracts into checked interfaces.
- Updates all plugins to match those typed event signatures, by adding
concrete parameter/return types and aligning handlers with the events
they subscribe to. The architectural effect is better consistency across
plugin boundaries, especially around importer hooks and metadata
callbacks.
- Includes a small set of follow-up fixes uncovered by the typing work
in places like `advancedrewrite`, `badfiles`, `playlist`, `permissions`,
`importsource`, and query/path handling. These are mostly correctness
and config/path-type cleanups rather than new features.
- High-level impact: this change improves maintainability and static
analysis across the plugin layer, reduces ambiguity in hook behavior,
and makes future plugin changes safer without changing the overall
architecture or user-facing workflows in a major way.
## Description
The monolithic `_get_genre` method was broken down into several private
instance methods and refactored for readability. The contract is kept
and is already well tested (`test_get_genre`)
- **Core Helpers** - were moved from within `_get_genre` to a reusable
instance method and a `cached_property`:
- `_try_resolve_stage`: Handles the canonicalization and logging of
genres for a specific stage.
- `fallback`: Provides the configured fallback genre. Is used as a last
resort in `_try_resolve_existing_genres` and when `_get_genre` couldn't
find any genre in any stage at all.
- **Lookup Stages** - some were complex enough to deserve their own
instance method for readability, some stay inline in `_get_genre`:
- `_try_resolve_existing_genres`: Manages the initial check for
pre-existing genres and the `cleanup_existing` logic when `force` is
disabled.
- track stage: stays inline
- album stage: indentical to track stage, but not worth moving /
deduplication doesn't buy much (see subsequent PR though)
- `_fetch_artist_stage`: Fetches and resolves artist-level genres,
including multi-valued album artists and "Various Artists" logic.
- `_fetch_va_genres`: specifically handles the plurality logic for
"Various Artists" albums.
- **Fallbacks**:
- `_try_resolve_original_fallback`: Handles the "keep_existing" logic
that attempts to use/canonicalize originally present genres if no new
ones are found.
Make sure to also look at subsequent PR's:
- https://github.com/beetbox/beets/pull/6890
- https://github.com/beetbox/beets/pull/6893
## To Do
- [x] ~Documentation~
- [x] Changelog. (Not required, refactor only)
- [x] ~Tests~ (_get_genre was already well covered and the signature of
the method was kept)
## Description
Fixes#6862.
Items with no artist/title tags produce a search with an empty query and
no filters. The request was still sent to the metadata source API, and
MusicBrainz answers it with `400 Bad Request`, logging a traceback once
per affected file. `_search_api` now returns no candidates instead of
issuing a request that cannot match anything.
## To Do
- [x] ~Documentation~ (bugfix, no user-facing option changed)
- [x] Changelog.
- [x] Tests. (`test_search_api_skips_request_without_query_and_filters`
fails without the fix, passes with it.)
So I was looking to add types to `beets.util.bluelet` and realised that
it is only used by `BPD` plugin. Instead of investing any time into it,
I scrapped it and used `asyncio` directly in `BPD`. I used GitHub search
to check that it is not used outside of our codebase.
---
- Replaces the custom coroutine scheduler in `beets.util.bluelet` with
Python's built-in `asyncio` for the `bpd` plugin.
- In `beetsplug/bpd/__init__.py`, the server architecture shifts from
Bluelet generators and event objects to native async I/O:
- connection handling now uses `asyncio.start_server`
- connection flows are rewritten as `async def` methods with `await`
- notification delivery is handled with background `asyncio` tasks
- socket lifecycle and disconnect handling move to `asyncio` stream
readers/writers
- This removes an internal async framework from the codebase,
consolidates `bpd` on a standard runtime model, and makes the networking
layer simpler to reason about and maintain.
- Tests in `test/plugins/test_bpd.py` are updated to mock
`asyncio.start_server` instead of Bluelet internals, matching the new
server entrypoint and preserving coverage around dynamic port
assignment.
- Track active notification tasks by connection so repeated dispatches do not
send duplicate idle responses while a previous send is still draining.
- Add regression coverage for serialized notification delivery and idle command
disconnect handling.
Fixes#4339.
The album half of this issue is already fixed, `album_for_id` guards the
`contributors` key. This covers the remaining call site, `_get_track`,
which still assumes the key exists:
```python
artist, artist_id = self.get_artist(
track_data.get("contributors", [track_data["artist"]])
)
```
Two problems. The default argument to `.get` is evaluated eagerly, so
`track_data["artist"]` runs on every call, even when `contributors` is
present. A track payload carrying `contributors` but no `artist` raises
KeyError, despite the code reading as though the fallback only applies
when `contributors` is missing. And the fallback itself depends on
`artist` being there, which is the same assumption sampsyo asked the
plugin to stop making.
I mirrored the shape already merged for the album path: use
`contributors` when present, fall back to `artist` when that's the one
we have, and leave the artist fields unset when neither key exists.
`str(artist_id)` now also only runs when there's an id, so a track with
no artist info gets `None` instead of the string "None". The album path
still calls `str(artist_id)` unconditionally and can store "None" the
same way, but I left it alone to keep this to the one call site. Happy
to follow up on it.
`_get_track` had no test coverage, so I added three tests: contributors
without artist, artist without contributors (the old fallback still
behaves the same), and neither key. The first and third fail on master
with KeyError at the `.get` line, and all three pass with the change.
ruff check and format are clean on both files.
_get_track resolved the artist with track_data.get('contributors', [track_data['artist']]),
whose default is evaluated eagerly, so track_data['artist'] raises KeyError whenever the
artist key is absent, even when contributors is present. Guard the fallback the same way
album_for_id already does. Fixes#4339.
Follow-up to #6842, based on @Serene-Arc follow-up
[comment](https://github.com/beetbox/beets/pull/6842#issuecomment-5187227636)
.
This makes the upgrade available when `duplicate_action: ask`, allowing
users to explicitly select it in the interactive duplicate prompt rather
than relying solely on configuration.
- [x] Documentation. (If you've added a new command-line flag, for
example, find the appropriate page under `docs/` to describe it.)
- [x] Changelog. (Add an entry to `docs/changelog.rst` to the bottom of
one of the lists near the top of the document.)
- [x] Tests. (Very much encouraged but not strictly required.)
Return _normalize_label to regex-based logic, per review.
Uses the apply_transforms method that was previously in lyrics.py, now
moved to new module beetsplug/_utis/func.py.
Replace the regex-based function with string methods, per review
feedback.
Also fixes a potential bug with the order of the license-to and the
corporate-relationship clause.
Verified against the same copyright samples as the previous
implementation, output is identical.
_parse_label() used to store Tidal's raw copyright info as the label
name. Add _normalize_label(), a helper that strips leading markers,
years, legal entities, and boilerplate clauses.
Fixes#6796
## `beets.autotag` Source-Centric Refactor
Fixes: #6684
### Core change
This PR restructures autotagging around a new
`beets.autotag.source.Source` object.
Before this change, album and singleton matching pulled "current
metadata" from several places:
- importer tasks
- matching code
- distance calculation
- UI display code
After this change, those flows all receive the same input shape: a
single `Source` value that carries:
- the original `items`
- the current display/search fields (`artist`, `name`)
- extracted metadata in `data`
- current external ID information (`id`, `id_consensus`)
That makes `Source` the new "current thing being matched" abstraction
for both albums and tracks.
---
## Architecture
### 1. New single input object: `Source`
`Source` centralizes metadata extraction that used to be scattered
across the autotag pipeline.
```text
Before
items
├─ importer computes current artist/album
├─ match code recomputes common tags
├─ distance recomputes common tags
└─ UI receives separate current values
After
Source
├─ source.artist
├─ source.name
├─ source.data
├─ source.items
├─ source.id / source.id_consensus
└─ source.va_likely
```
This is the biggest architectural shift in the PR.
### 2. `tag_album()` and `tag_item()` now operate on `Source`
The matching entry points now take a `Source` instead of a loose mix of:
- raw `items`
- `item`
- current artist/title values
- repeated ID lookups
That gives album and track matching the same calling convention and
reduces special-case plumbing.
```python
# Before
tag_album(items, ...)
tag_item(item, ...)
# After
tag_album(source, ...)
tag_item(source, ...)
```
### 3. `distance()` is now narrower and more explicit
`distance()` no longer derives "current metadata" internally from a list
of items.
Instead, it receives:
- `original`: the already-extracted metadata container
- `unmatched_count`: the number of unmatched local tracks
```text
Before
distance(items, album_info, item_info_pairs)
-> recompute common tags from items
-> infer unmatched track count
After
distance(source.data, album_info, item_info_pairs, unmatched_count)
-> compare against already-prepared metadata
```
This is a good separation of responsibilities:
- `Source` prepares current metadata
- `distance()` only compares metadata
### 4. Importer tasks now cache the current source
`ImportTask` and `SingletonImportTask` now expose a cached `source`
property:
- album tasks use `Source.from_items(...)`
- singleton tasks use `Source.from_item(...)`
That means importer state no longer needs separate `cur_artist` and
`cur_album` fields, and downstream code can read from `task.source`
instead.
### 5. UI/display code now depends on the same source model
Import session and display functions now use `task.source.artist` and
`task.source.name` when showing candidate changes or prompting for
manual search/ID entry.
This removes another copy of "current metadata" logic from the UI layer.
### 6. `AttrDict` moves to `beets.util`
`AttrDict` was previously defined inside `beets.autotag.hooks`. It now
lives in `beets.util`.
High-level effect:
- metadata-container behavior is treated as shared infrastructure
- autotag-specific modules depend on a common utility instead of owning
the abstraction
### 7. `beets.autotag` public API is flatter
Related cleanup continues by exporting more autotag types directly from
`beets.autotag`, including `Source`.
This reduces import-path churn for callers and makes the package
boundary easier to understand.
---
## High-level impact
### What this improves
#### 1. One place defines "the current metadata we are matching from"
The main benefit is conceptual clarity.
Reviewers can now think about autotagging as:
```text
local files -> Source -> candidate lookup -> distance -> proposal/UI
```
instead of following several separate paths that each rediscover the
same metadata.
#### 2. Album and singleton flows now look alike
Albums and single tracks now use the same structure and similar call
patterns.
That should make future autotag changes easier because new behavior can
usually be added once at the `Source` boundary rather than twice in
parallel code paths.
#### 3. Less coupling between importer, matcher, distance logic, and UI
Previously, each layer needed to know how to reconstruct "current
artist/album/common tags".
Now:
- importer builds `Source`
- matching uses `Source`
- UI displays from `Source`
- `distance()` receives prepared metadata
Each layer does less guessing about the others.
#### 4. Simpler data flow for debugging and future refactors
When a match looks wrong, there is now a clearer place to inspect the
original input: `Source`.
That should help with debugging, logging, and future changes to metadata
extraction rules.
---
## Data flow at a glance
```text
ImportTask / SingletonImportTask
↓
`task.source`
↓
`tag_album(source)` / `tag_item(source)`
↓
candidate search + item/track assignment
↓
`distance(source.data, info, pairs, unmatched_count)`
↓
`Proposal`
↓
UI display and user choice
```
---
## Notable follow-on cleanup included here
- `get_most_common_tags()` now returns only the extracted metadata
dictionary, not a `(likelies, consensus)` pair
- call sites that depended on consensus now compute only the specific
signals they need, such as `id_consensus` or `va_likely`
- tests were updated to reflect the new `Source`-based flow
- `.git-blame-ignore-revs` was updated for mechanical refactor commits
---
## Reviewer guide
If reviewing this PR, the easiest way to read it is:
1. Start with `beets/autotag/source.py`
2. Then look at `tag_album()` and `tag_item()` in
`beets/autotag/match.py`
3. Then review the `distance()` signature change
4. Finally scan importer/UI call sites to see how they now pass
`task.source`
That path shows the architectural intent without getting lost in the
mechanical updates.