Skip to content

typing: add types to beets.util and beets.test.helper#6805

Open
snejus wants to merge 8 commits into
masterfrom
type-beets-util-test-helper
Open

typing: add types to beets.util and beets.test.helper#6805
snejus wants to merge 8 commits into
masterfrom
type-beets-util-test-helper

Conversation

@snejus

@snejus snejus commented Jun 30, 2026

Copy link
Copy Markdown
Member

I've started working on migrating the codebase to pathlib.Path and this is the very first step: fully type beets.util and beets.test.helper. Next, I will me migrating tests to use pathlib.Path.

  • Extracts shared path/temp-directory behavior into PathsMixin, and splits import-specific test setup into ImporterMixin. This makes beets.test.helper more modular and gives test helpers a clearer inheritance structure.

  • Updates TestHelper and import helpers to use those smaller mixins, while keeping existing test behavior intact. The main architectural effect is better separation between core test setup, filesystem helpers, and importer-specific utilities.

  • Tightens typing across beets.test.helper and beets.util, especially around path handling. beets.util now accepts a broader set of path types through PathLike/AnyPath, which reduces friction between bytes, str, and Path usage.

  • Aligns helper internals with the typed interfaces by normalizing path handling in methods like touch, using typed template(...) entries for importer path formats, and fixing a few return-type/inheritance edge cases.

  • High-level impact: this is mainly a typing and test-infrastructure cleanup. It improves maintainability and type-checker accuracy without changing production architecture or intended runtime behavior.

@snejus snejus requested a review from a team as a code owner June 30, 2026 10:56
Copilot AI review requested due to automatic review settings June 30, 2026 10:56
@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.

@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.80%. Comparing base (76113c1) to head (9232a66).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
beets/util/__init__.py 85.71% 3 Missing ⚠️
Additional details and impacted files
@@                Coverage Diff                 @@
##           speed-up-tests    #6805      +/-   ##
==================================================
- Coverage           74.98%   74.80%   -0.19%     
==================================================
  Files                 163      163              
  Lines               20966    20967       +1     
  Branches             3302     3302              
==================================================
- Hits                15721    15684      -37     
- Misses               4486     4527      +41     
+ Partials              759      756       -3     
Files with missing lines Coverage Δ
beets/util/__init__.py 79.27% <85.71%> (+0.04%) ⬆️

... and 2 files 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.

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 try make first step toward pathlib.Path migration by adding typing to beets.util and beets.test.helper, plus split test helper behavior into smaller mixins (PathsMixin, ImporterMixin).

Changes:

  • Add tighter type hints in beets/util/__init__.py and widen some path-accepting APIs to PathLike.
  • Refactor beets/test/helper.py into smaller mixins and update helper methods to normalize path handling.
  • Adjust importer test helper setup to use typed template(...) for lib.path_formats.

Reviewed changes

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

File Description
beets/util/init.py Type annotations on filesystem helpers; small behavior-neutral cleanup in move() typing comments.
beets/test/helper.py New mixins + typing; path normalization changes in helpers; importer helper setup updated.

Comment thread beets/test/helper.py
Comment thread beets/test/helper.py
Comment thread beets/test/helper.py Outdated
Comment thread beets/util/__init__.py Outdated
Comment thread beets/test/helper.py Outdated

@semohr semohr 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.

Looks good from my perspective.

Consider adding the files to the optin stricter mypy config.

@snejus snejus force-pushed the type-beets-util-test-helper branch from 74a0002 to 0375672 Compare June 30, 2026 11:14
@snejus snejus force-pushed the type-beets-util-test-helper branch 2 times, most recently from a835cc4 to e26ee18 Compare June 30, 2026 12:10
@snejus snejus requested a review from Copilot June 30, 2026 12:10

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 4 comments.

Comment thread beets/test/helper.py Outdated
Comment thread beets/test/helper.py
Comment thread beets/util/__init__.py Outdated
Comment thread beets/util/__init__.py Outdated
@snejus

snejus commented Jun 30, 2026

Copy link
Copy Markdown
Member Author

Consider adding the files to the optin stricter mypy config.

I tried adding them:

diff --git a/setup.cfg b/setup.cfg
index 01971c01d..259e8a5c0 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -55,6 +55,12 @@ check_untyped_defs = true
 disallow_untyped_decorators = true
 check_untyped_defs = true
 
+[[mypy-beets.utils]]
+strict = true
+
+[[mypy-beets.test.helper]]
+strict = true
+
 [[mypy-beetsplug.musicbrainz]]
 strict = true

And ran mypy on master but it didn't complain about any issues :/

@snejus snejus force-pushed the type-beets-util-test-helper branch from e26ee18 to f2f5997 Compare June 30, 2026 12:35
@snejus snejus requested a review from Copilot June 30, 2026 12:35

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 2 comments.

Comment thread beets/test/helper.py
Comment on lines +722 to 729
if task.candidates:
if choice == importer.Action.APPLY:
return task.candidates[0] # type: ignore[return-value]
if isinstance(choice, int):
return task.candidates[choice - 1] # type: ignore[return-value]

assert not isinstance(choice, int), f"Invalid choice: {choice}"
return choice
Comment thread beets/test/helper.py
Comment on lines +868 to +875
def _make_album_match(
self,
artist: str,
album: str,
tracks: int,
distance: int = 0,
missing: int = 0,
) -> AlbumInfo:
@snejus snejus force-pushed the type-beets-util-test-helper branch from f2f5997 to 7716532 Compare June 30, 2026 20:59
snejus added 6 commits July 1, 2026 00:02
- Removing the coverage omit configuration exposed unused branches, helpers, and
  fixtures across the test suite.
- Delete that redundant logic and simplify affected tests while preserving their
  behavior.
@snejus snejus force-pushed the type-beets-util-test-helper branch from 7716532 to 9232a66 Compare July 1, 2026 07:10
@semohr

semohr commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Consider adding the files to the optin stricter mypy config.

Maybe using disallow_untyped_defs would work? Am also not sure the config is picked up correctly 🤔

My idea here was just to have future changes require stricter rules for these files as they are typed properly right now.

Base automatically changed from speed-up-tests to master July 3, 2026 21:07
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.

3 participants