Skip to content

Support multi-valued fields in rewrite and advancedrewrite plugins#6518

Merged
snejus merged 6 commits intomasterfrom
support-list-fields-rewrite
Apr 10, 2026
Merged

Support multi-valued fields in rewrite and advancedrewrite plugins#6518
snejus merged 6 commits intomasterfrom
support-list-fields-rewrite

Conversation

@snejus
Copy link
Copy Markdown
Member

@snejus snejus commented Apr 10, 2026

Fix rewriting of multi-valued fields (rewrite / advancedrewrite plugins)

Bug: Both rewrite and advancedrewrite plugins assumed all field values are scalars, so list-type fields (e.g. genres) were not rewritten correctly. Additionally, only the first matching rule was ever applied to a field.


What changed

Core logic (beetsplug/rewrite.py):

  • Introduced a rewrite_value singledispatch function to handle both str and list[str] values. For lists, each element is rewritten individually.
  • Extracted apply_rewrite_rules as a shared utility — now applies all matching rules in config order (previously stopped at the first match).

advancedrewrite plugin:

  • Replaced its own inline rule-matching loop with a call to the shared apply_rewrite_rules, fixing list field support there too.

Behaviour change — rule application order:

Previously, only the first matching rule was applied. Now, all rules run in config order, allowing chained rewrites. For example:

rewrite:
    artist .*hendrix.*: hendrix catalog
    artist .*catalog.*: Experience catalog

This now produces "Experience catalog" instead of "hendrix catalog".

Fixes: #6515

@snejus snejus requested a review from a team as a code owner April 10, 2026 09:39
Copilot AI review requested due to automatic review settings April 10, 2026 09:39
@github-actions github-actions Bot added the advancedrewrite advancedrewrite plugin label Apr 10, 2026
@snejus snejus added the rewrite rewrite plugin label Apr 10, 2026
@snejus snejus force-pushed the support-list-fields-rewrite branch from 6377a8d to 619f7e8 Compare April 10, 2026 09:41
@snejus snejus force-pushed the support-list-fields-rewrite branch from 619f7e8 to e21f56f Compare April 10, 2026 09:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds proper rewrite support for multi-valued (list) fields in the rewrite and advancedrewrite plugins so rules can affect fields like genres instead of being ignored.

Changes:

  • Introduce apply_rewrite_rules / rewrite_value in beetsplug/rewrite.py to handle str and list values.
  • Delegate advancedrewrite simple-rule rewriting to the shared helper.
  • Add tests + docs + changelog entry covering list-field rewriting behavior.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
beetsplug/rewrite.py Adds singledispatch-based rewrite helper for scalar + list values; used by template field rewriters.
beetsplug/advancedrewrite.py Switches simple-rule path to call shared helper before advanced-query rewrites.
test/plugins/test_rewrite.py New tests for scalar rewriting, rule order, and list-field (genres) rewriting.
test/plugins/test_advancedrewrite.py Adds coverage for simple-rule rewriting on list fields.
docs/plugins/rewrite.rst Documents that rewrite rules apply to matching list entries for multi-valued fields.
docs/plugins/advancedrewrite.rst Notes parity with rewrite for list-field simple rules.
docs/changelog.rst Adds bugfix note for multi-valued rewrite behavior.

Comment thread beetsplug/rewrite.py Outdated
Comment thread beetsplug/advancedrewrite.py
@github-actions github-actions Bot removed the rewrite rewrite plugin label Apr 10, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 10, 2026

Codecov Report

❌ Patch coverage is 95.45455% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 70.55%. Comparing base (fd586ef) to head (2ff7887).
⚠️ Report is 7 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
beetsplug/rewrite.py 94.73% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6518      +/-   ##
==========================================
+ Coverage   70.40%   70.55%   +0.15%     
==========================================
  Files         148      148              
  Lines       18806    18820      +14     
  Branches     3067     3066       -1     
==========================================
+ Hits        13240    13279      +39     
+ Misses       4916     4894      -22     
+ Partials      650      647       -3     
Files with missing lines Coverage Δ
beetsplug/advancedrewrite.py 82.43% <100.00%> (+1.35%) ⬆️
beetsplug/rewrite.py 97.95% <94.73%> (+69.38%) ⬆️
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Comment thread test/plugins/test_rewrite.py
Comment thread test/plugins/test_advancedrewrite.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Comment thread beetsplug/rewrite.py Outdated
Comment thread test/plugins/test_rewrite.py Outdated
@snejus snejus force-pushed the support-list-fields-rewrite branch from 930714a to 2ff7887 Compare April 10, 2026 13:55
@snejus snejus requested a review from Copilot April 10, 2026 14:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comment thread beetsplug/rewrite.py
@snejus snejus merged commit 72b1118 into master Apr 10, 2026
25 checks passed
@snejus snejus deleted the support-list-fields-rewrite branch April 10, 2026 17:39
@snejus snejus added the rewrite rewrite plugin label Apr 10, 2026
@github-actions github-actions Bot removed the rewrite rewrite plugin label Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

advancedrewrite advancedrewrite plugin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rewrite plugin crashes with AttributeError on list-type fields (e.g. genres) in beets 2.8.0

2 participants