Skip to content

lastgenre: Improve original fallback (better use of aliases and count) - #6890

Open
JOJ0 wants to merge 3 commits into
masterfrom
lastgenre_improve_original_fallback
Open

lastgenre: Improve original fallback (better use of aliases and count)#6890
JOJ0 wants to merge 3 commits into
masterfrom
lastgenre_improve_original_fallback

Conversation

@JOJ0

@JOJ0 JOJ0 commented Jul 30, 2026

Copy link
Copy Markdown
Member

Description

When _get_genre runs with force, whitelist and canonicalization enabled, as a last resort it tries to fall back to the original genres. Of course this stage normalizes, canonicalizes, does all the stuff it's supposed to when resolving the stage. That also includes reducing genres to the configured count. Now in that case that could reduce the list of genres and is not exactly what a fall back to what we had before should look like.

This PR changes the stage as follows:

We don't run through _try_resolve_stage instantly because we want to make sure the count setting doesn't kick out anything prematurely! We then apply whitelist checks and return if something valid is found.

The very last resort action is kept as we had it already: Use keep_genres (the lowercased originals) to canonicalize and hope that now we find a whitelisted parent genre.

Note: Requires #6474

To Do

  • Documentation. (Not required IMO)
  • Changelog.
  • Tests.

@github-actions github-actions Bot added the lastgenre lastgenre plugin label Jul 30, 2026
@github-actions

Copy link
Copy Markdown

Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry.

@JOJ0
JOJ0 force-pushed the lastgenre_improve_original_fallback branch from dfef785 to a5f7d91 Compare July 30, 2026 05:53
@JOJ0
JOJ0 changed the base branch from master to lastgenre_refactor_get_genre July 30, 2026 05:54
@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.12%. Comparing base (e1ffd17) to head (a248e17).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6890      +/-   ##
==========================================
+ Coverage   76.11%   76.12%   +0.01%     
==========================================
  Files         163      163              
  Lines       21151    21151              
  Branches     3334     3333       -1     
==========================================
+ Hits        16099    16102       +3     
+ Misses       4264     4262       -2     
+ Partials      788      787       -1     
Files with missing lines Coverage Δ
beetsplug/lastgenre/__init__.py 86.41% <100.00%> (+0.34%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JOJ0
JOJ0 force-pushed the lastgenre_improve_original_fallback branch 2 times, most recently from c53cebb to 1906d93 Compare July 30, 2026 06:05
@JOJ0 JOJ0 mentioned this pull request Jul 30, 2026
3 tasks
@JOJ0
JOJ0 force-pushed the lastgenre_improve_original_fallback branch 2 times, most recently from b9cfd57 to 39927e7 Compare August 5, 2026 06:18
@JOJ0 JOJ0 changed the title lastgenre: improve original fallback lastgenre: Improve original fallback (better use of aliases) Aug 5, 2026
@JOJ0 JOJ0 changed the title lastgenre: Improve original fallback (better use of aliases) lastgenre: Improve original fallback (better use of aliases and count) Aug 5, 2026
@JOJ0
JOJ0 marked this pull request as ready for review August 5, 2026 06:41
Copilot AI lite review requested due to automatic review settings August 5, 2026 06:41
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

PR make lastgenre original-genre fallback better when force + keep_existing + whitelist (+ canonical) is on. Goal is: do alias normalize + whitelist check without _resolve_genres path that can apply count too early, then only do canonical-parent fallback as last resort.

Changes:

  • Change “original fallback” stage to alias-normalize originals, then run whitelist/ignore filtering directly (no _try_resolve_stage yet).
  • Keep existing canonicalized-parent fallback via _try_resolve_stage(...) when nothing survives whitelist after aliasing.
  • Update docstring to describe new fallback order.

Comment thread beetsplug/lastgenre/__init__.py
Comment thread beetsplug/lastgenre/__init__.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 1 out of 1 changed files in this pull request and generated no new comments.

Suppressed comments (1)

beetsplug/lastgenre/init.py:517

  • grug see new logic to avoid count dropping good original genres before whitelist. grug want regression test so this not break later. add pytest case where force+keep_existing+whitelist+canonical and count=1, existing genres has non-whitelisted first then whitelisted later, and expect "original fallback" returns whitelisted one (not lost to early count).
        # We do not run through try_resolve_stage yet because count could drop
        # existing genres, but we still apply aliases before whitelist
        # filtering.
        normalized = [
            norm if norm != g.lower() else g
            for g in genres
            if (norm := normalize_genre(self._log, self.alias_patterns, g))
        ]

@JOJ0
JOJ0 force-pushed the lastgenre_improve_original_fallback branch from 389148e to b4df07a Compare August 12, 2026 04:18
@JOJ0
JOJ0 requested a review from a team as a code owner August 12, 2026 05:02
@JOJ0
JOJ0 requested a lite review from Copilot August 12, 2026 05:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

beetsplug/lastgenre/init.py:517

  • grug see alias normalize can make two old genres become same new genre. then code return duplicates (like 'hip-hop' + 'hiphop' -> 'hip hop' twice). grug think should dedup here, but still not apply count.
        normalized = [
            norm if norm != g.lower() else g
            for g in genres
            if (norm := normalize_genre(self._log, self.alias_patterns, g))
        ]

@JOJ0
JOJ0 force-pushed the lastgenre_improve_original_fallback branch from 1a75921 to c1e8a5e Compare August 17, 2026 06:22
@JOJ0
JOJ0 marked this pull request as draft August 17, 2026 06:52
Base automatically changed from lastgenre_refactor_get_genre to master August 18, 2026 05:15
JOJ0 added a commit that referenced this pull request Aug 18, 2026
## 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:

- #6890
- #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)
JOJ0 added 2 commits August 18, 2026 07:29
The discrepancy:

- Normalization might help to keep more whitelisted genres!
- But running through try_resolve_stage would be less code
- The difference being that the latter reduces existing genres to the
  configured count which is not really fitting with a "last resort
  fallback to original genres"

The here suggested solution:

- We don't run through try_resolve_stage instantly because we want to
  make sure the "count" setting doesnt kick out anything but we apply
  aliases before whitelist check.
@JOJ0
JOJ0 force-pushed the lastgenre_improve_original_fallback branch from c1e8a5e to f32df2c Compare August 18, 2026 05:29
@JOJ0
JOJ0 marked this pull request as ready for review August 18, 2026 05:32
@github-actions

Copy link
Copy Markdown

Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry.

@JOJ0
JOJ0 requested review from henry-oberholtzer and snejus and a lite review from Copilot August 18, 2026 05:41
@JOJ0

JOJ0 commented Aug 18, 2026

Copy link
Copy Markdown
Member Author

Finalized this one, added a changelog, ready to review now.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 3 out of 3 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lastgenre lastgenre plugin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants