Skip to content

Conversation

snejus
Copy link
Member

@snejus snejus commented Aug 26, 2025

Context

See #5916 where we've come across a need to define common logic between TrackInfo and AlbumInfo.

Changes

  • Introduce generic Info base (extends AttrDict) used by AlbumInfo / TrackInfo to centralize shared attributes and initialisation logic.
  • Sort keyword parameters in each constructor alphabetically and make them explicit.
  • Deduplicate and simplify shared copy() method using copy.deepcopy
  • Improve type hints and documentation.
  • Drop unused logging artifacts.

Summary by Sourcery

Refactor metadata-handling classes by extracting common functionality into a new Info base, updating AlbumInfo and TrackInfo to extend it with explicit sorted parameters, unify their copy logic, improve type annotations and docs, and drop obsolete logging code

New Features:

  • Introduce a generic Info base class to centralize shared logic for AlbumInfo and TrackInfo

Enhancements:

  • Alphabetically sort and explicitly declare constructor keyword parameters for consistency
  • Unify and simplify the copy() implementation in AttrDict using deepcopy
  • Enhance type hints and documentation for metadata classes

Chores:

  • Remove unused logging imports and artifacts

Copy link

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.

Copy link
Contributor

@Copilot 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

This PR introduces a generic Info base class that centralizes shared attributes and initialization logic between AlbumInfo and TrackInfo. The refactoring aims to reduce code duplication and improve maintainability.

  • Extracts common functionality into a new Info base class extending AttrDict
  • Consolidates shared attributes and implements a unified copy() method using copy.deepcopy
  • Alphabetizes constructor parameters and makes them explicit with keyword-only arguments

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link

codecov bot commented Aug 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.47%. Comparing base (7340f15) to head (19c43c9).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5963      +/-   ##
==========================================
- Coverage   66.49%   66.47%   -0.03%     
==========================================
  Files         117      117              
  Lines       18122    18105      -17     
  Branches     3071     3071              
==========================================
- Hits        12051    12036      -15     
+ Misses       5415     5414       -1     
+ Partials      656      655       -1     
Files with missing lines Coverage Δ
beets/autotag/hooks.py 100.00% <100.00%> (+1.80%) ⬆️
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@semohr
Copy link
Contributor

semohr commented Aug 26, 2025

As a short note, we already do this in beets-flask ;) Might be helpful here 🤷 (see MusicInfo, ItemInfo and AlbumInfo)

@snejus
Copy link
Member Author

snejus commented Sep 6, 2025

As a short note, we already do this in beets-flask ;) Might be helpful here 🤷 (see MusicInfo, ItemInfo and AlbumInfo)

I see. I looked into making them dataclasses, however we're limited due to backwards compatibility and need to keep dict-like item access in place :/

@snejus
Copy link
Member Author

snejus commented Sep 6, 2025

@sourcery-ai review

Copy link
Contributor

sourcery-ai bot commented Sep 6, 2025

Reviewer's Guide

This PR introduces a new Info base class in place of duplicated initialisation logic in AlbumInfo and TrackInfo, reorganising constructors, centralising copy functionality using deepcopy, cleaning up logging, and enhancing type annotations and documentation.

Class diagram for new Info base class and its usage in AlbumInfo and TrackInfo

classDiagram
    class AttrDict {
        +copy() Self
        +__getattr__(attr: str) V
        +__setattr__(key: str, value: V)
        +__hash__() int
    }
    class Info {
        +album: str | None
        +artist_credit: str | None
        +artist_id: str | None
        +artist: str | None
        +artists_credit: list[str] | None
        +artists_ids: list[str] | None
        +artists: list[str] | None
        +artist_sort: str | None
        +artists_sort: list[str] | None
        +data_source: str | None
        +data_url: str | None
        +genre: str | None
        +media: str | None
        +__init__(...)
    }
    class AlbumInfo {
        +tracks: list[TrackInfo]
        +album_id: str | None
        +albumdisambig: str | None
        +albumstatus: str | None
        +albumtype: str | None
        +albumtypes: list[str] | None
        +asin: str | None
        +barcode: str | None
        +catalognum: str | None
        +country: str | None
        +day: int | None
        +discogs_albumid: str | None
        +discogs_artistid: str | None
        +discogs_labelid: str | None
        +label: str | None
        +language: str | None
        +mediums: int | None
        +month: int | None
        +original_day: int | None
        +original_month: int | None
        +original_year: int | None
        +release_group_title: str | None
        +releasegroup_id: str | None
        +releasegroupdisambig: str | None
        +script: str | None
        +style: str | None
        +va: bool
        +year: int | None
        +__init__(...)
    }
    class TrackInfo {
        +arranger: str | None
        +bpm: str | None
        +composer: str | None
        +composer_sort: str | None
        +disctitle: str | None
        +index: int | None
        +initial_key: str | None
        +length: float | None
        +lyricist: str | None
        +mb_workid: str | None
        +medium: int | None
        +medium_index: int | None
        +medium_total: int | None
        +release_track_id: str | None
        +title: str | None
        +track_alt: str | None
        +track_id: str | None
        +work: str | None
        +work_disambig: str | None
        +__init__(...)
    }
    AttrDict <|-- Info
    Info <|-- AlbumInfo
    Info <|-- TrackInfo
    AlbumInfo "1" o-- "*" TrackInfo
Loading

Class diagram for updated copy() method centralisation

classDiagram
    class AttrDict {
        +copy() Self
    }
    class Info {
        +copy() Self
    }
    class AlbumInfo {
        +copy() Self
    }
    class TrackInfo {
        +copy() Self
    }
    AttrDict <|-- Info
    Info <|-- AlbumInfo
    Info <|-- TrackInfo
Loading

File-Level Changes

Change Details Files
Extracted generic Info base class for shared metadata logic
  • Introduced Info subclass of AttrDict with common init fields
  • Migrated shared attribute initialisation from AlbumInfo and TrackInfo into Info
  • Updated AlbumInfo and TrackInfo to inherit from Info and removed duplicate setup
beets/autotag/hooks.py
Reordered and made constructor parameters explicit
  • Sorted keyword-only parameters alphabetically in Info, AlbumInfo, and TrackInfo
  • Added '*' to enforce keyword-only signature
  • Removed redundant **kwargs entries after explicit declarations
beets/autotag/hooks.py
Simplified copy methods using deepcopy
  • Added copy() implementation in AttrDict using copy.deepcopy
  • Removed custom copy() overrides in AlbumInfo and TrackInfo
beets/autotag/hooks.py
Removed unused logging artifacts
  • Deleted logging import and log variable
  • Eliminated stale logging.getLogger usage
beets/autotag/hooks.py
Improved type hints and documentation
  • Annotated copy() return type with Self and added precise type hints
  • Enhanced class and method docstrings to reflect updated behavior and parameters
beets/autotag/hooks.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `beets/autotag/hooks.py:54` </location>
<code_context>
-class AlbumInfo(AttrDict[Any]):
-    """Describes a canonical release that may be used to match a release
-    in the library. Consists of these data members:
+class Info(AttrDict[Any]):
+    """Container for metadata about a musical entity."""
+
+    def __init__(
+        self,
+        album: str | None = None,
</code_context>

<issue_to_address>
Info class constructor sets many attributes directly before calling update.

If kwargs contain keys matching the directly set attributes, their values may be overwritten. Please confirm this behavior is intended.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@snejus snejus force-pushed the create-common-info-class branch from 4e442c9 to 19c43c9 Compare September 7, 2025 20:08
@snejus snejus requested a review from a team as a code owner September 7, 2025 20:08
@snejus snejus merged commit f24beca into master Sep 8, 2025
20 checks passed
@snejus snejus deleted the create-common-info-class branch September 8, 2025 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants