Skip to content

Commit cab0246

Browse files
committed
Fix types in all edited files
1 parent 53717ee commit cab0246

File tree

6 files changed

+53
-50
lines changed

6 files changed

+53
-50
lines changed

beets/autotag/hooks.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __hash__(self):
5555
return id(self)
5656

5757

58-
class AlbumInfo(AttrDict):
58+
class AlbumInfo(AttrDict[Any]):
5959
"""Describes a canonical release that may be used to match a release
6060
in the library. Consists of these data members:
6161
@@ -165,7 +165,7 @@ def copy(self) -> AlbumInfo:
165165
return dupe
166166

167167

168-
class TrackInfo(AttrDict):
168+
class TrackInfo(AttrDict[Any]):
169169
"""Describes a canonical track present on a release. Appears as part
170170
of an AlbumInfo's ``tracks`` list. Consists of these data members:
171171
@@ -356,8 +356,8 @@ class Distance:
356356
for each individual penalty.
357357
"""
358358

359-
def __init__(self):
360-
self._penalties = {}
359+
def __init__(self) -> None:
360+
self._penalties: dict[str, list[float]] = {}
361361
self.tracks: dict[TrackInfo, Distance] = {}
362362

363363
@cached_classproperty

beets/autotag/match.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,8 @@ def tag_item(
604604
return Proposal([], Recommendation.none)
605605

606606
# Search terms.
607-
if not (search_artist and search_title):
608-
search_artist, search_title = item.artist, item.title
607+
search_artist = search_artist or item.artist
608+
search_title = search_title or item.title
609609
log.debug("Item search terms: {0} - {1}", search_artist, search_title)
610610

611611
# Get and evaluate candidate metadata.

beets/plugins.py

+8-15
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@
5151

5252
from beets.autotag import AlbumInfo, Distance, TrackInfo
5353
from beets.dbcore import Query
54-
from beets.dbcore.db import FieldQueryType, SQLiteType
54+
from beets.dbcore.db import FieldQueryType
55+
from beets.dbcore.types import Type
5556
from beets.importer import ImportSession, ImportTask
5657
from beets.library import Album, Item, Library
5758
from beets.ui import Subcommand
59+
from beets.util.id_extractors import RegexDict
5860

5961
# TYPE_CHECKING guard is needed for any derived type
6062
# which uses an import from `beets.library` and `beets.imported`
@@ -225,7 +227,7 @@ def track_distance(
225227

226228
def album_distance(
227229
self,
228-
items: list[Item],
230+
items: Sequence[Item],
229231
album_info: AlbumInfo,
230232
mapping: dict[Item, TrackInfo],
231233
) -> Distance:
@@ -430,10 +432,10 @@ def queries() -> dict[str, type[Query]]:
430432
return out
431433

432434

433-
def types(model_cls: type[AnyModel]) -> dict[str, type[SQLiteType]]:
435+
def types(model_cls: type[AnyModel]) -> dict[str, Type]:
434436
# Gives us `item_types` and `album_types`
435437
attr_name = f"{model_cls.__name__.lower()}_types"
436-
types: dict[str, type[SQLiteType]] = {}
438+
types: dict[str, Type] = {}
437439
for plugin in find_plugins():
438440
plugin_types = getattr(plugin, attr_name, {})
439441
for field in plugin_types:
@@ -470,7 +472,7 @@ def track_distance(item: Item, info: TrackInfo) -> Distance:
470472

471473

472474
def album_distance(
473-
items: list[Item],
475+
items: Sequence[Item],
474476
album_info: AlbumInfo,
475477
mapping: dict[Item, TrackInfo],
476478
) -> Distance:
@@ -768,15 +770,6 @@ class Response(TypedDict):
768770
id: str
769771

770772

771-
class RegexDict(TypedDict):
772-
"""A dictionary containing a regex pattern and the number of the
773-
match group.
774-
"""
775-
776-
pattern: str
777-
match_group: int
778-
779-
780773
R = TypeVar("R", bound=Response)
781774

782775

@@ -924,7 +917,7 @@ def item_candidates(
924917

925918
def album_distance(
926919
self,
927-
items: list[Item],
920+
items: Sequence[Item],
928921
album_info: AlbumInfo,
929922
mapping: dict[Item, TrackInfo],
930923
) -> Distance:

beets/test/helper.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,11 @@ class PluginMixin(ConfigMixin):
448448
plugin: ClassVar[str]
449449
preload_plugin: ClassVar[bool] = True
450450

451+
original_item_types = dict(Item._types)
452+
original_album_types = dict(Album._types)
453+
original_item_queries = dict(Item._queries)
454+
original_album_queries = dict(Album._queries)
455+
451456
def setup_beets(self):
452457
super().setup_beets()
453458
if self.preload_plugin:
@@ -471,13 +476,8 @@ def load_plugins(self, *plugins: str) -> None:
471476

472477
# Take a backup of the original _types and _queries to restore
473478
# when unloading.
474-
Item._original_types = dict(Item._types)
475-
Album._original_types = dict(Album._types)
476479
Item._types.update(beets.plugins.types(Item))
477480
Album._types.update(beets.plugins.types(Album))
478-
479-
Item._original_queries = dict(Item._queries)
480-
Album._original_queries = dict(Album._queries)
481481
Item._queries.update(beets.plugins.named_queries(Item))
482482
Album._queries.update(beets.plugins.named_queries(Album))
483483

@@ -489,10 +489,10 @@ def unload_plugins(self) -> None:
489489
self.config["plugins"] = []
490490
beets.plugins._classes = set()
491491
beets.plugins._instances = {}
492-
Item._types = getattr(Item, "_original_types", {})
493-
Album._types = getattr(Album, "_original_types", {})
494-
Item._queries = getattr(Item, "_original_queries", {})
495-
Album._queries = getattr(Album, "_original_queries", {})
492+
Item._types = self.original_item_types
493+
Album._types = self.original_album_types
494+
Item._queries = self.original_item_queries
495+
Album._queries = self.original_album_queries
496496

497497
@contextmanager
498498
def configure_plugin(self, config: Any):

beets/util/id_extractors.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,31 @@
1515
"""Helpers around the extraction of album/track ID's from metadata sources."""
1616

1717
import re
18+
from typing import TypedDict
19+
20+
21+
class RegexDict(TypedDict):
22+
"""A dictionary containing a regex pattern and the number of the
23+
match group.
24+
"""
25+
26+
pattern: str
27+
match_group: int
28+
1829

1930
# Spotify IDs consist of 22 alphanumeric characters
2031
# (zero-left-padded base62 representation of randomly generated UUID4)
21-
spotify_id_regex = {
32+
spotify_id_regex: RegexDict = {
2233
"pattern": r"(^|open\.spotify\.com/{}/)([0-9A-Za-z]{{22}})",
2334
"match_group": 2,
2435
}
2536

26-
deezer_id_regex = {
37+
deezer_id_regex: RegexDict = {
2738
"pattern": r"(^|deezer\.com/)([a-z]*/)?({}/)?(\d+)",
2839
"match_group": 4,
2940
}
3041

31-
beatport_id_regex = {
42+
beatport_id_regex: RegexDict = {
3243
"pattern": r"(^|beatport\.com/release/.+/)(\d+)$",
3344
"match_group": 2,
3445
}

beetsplug/musicbrainz.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
from collections import Counter
2222
from itertools import product
2323
from typing import TYPE_CHECKING, Any
24-
from collections.abc import Sequence
25-
from itertools import product
26-
from typing import TYPE_CHECKING, Any
2724
from urllib.parse import urljoin
2825

2926
import musicbrainzngs
@@ -44,6 +41,8 @@
4441

4542
from beets.library import Item
4643

44+
from ._typing import JSONDict
45+
4746
VARIOUS_ARTISTS_ID = "89ad4ac3-39f7-470e-963a-56509c546377"
4847

4948
BASE_URL = "https://musicbrainz.org/"
@@ -124,7 +123,7 @@ def get_message(self):
124123
BROWSE_MAXTRACKS = 500
125124

126125

127-
def _preferred_alias(aliases: list):
126+
def _preferred_alias(aliases: list[JSONDict]):
128127
"""Given an list of alias structures for an artist credit, select
129128
and return the user's preferred alias alias or None if no matching
130129
alias is found.
@@ -133,7 +132,7 @@ def _preferred_alias(aliases: list):
133132
return
134133

135134
# Only consider aliases that have locales set.
136-
aliases = [a for a in aliases if "locale" in a]
135+
valid_aliases = [a for a in aliases if "locale" in a]
137136

138137
# Get any ignored alias types and lower case them to prevent case issues
139138
ignored_alias_types = config["import"]["ignored_alias_types"].as_str_seq()
@@ -144,13 +143,13 @@ def _preferred_alias(aliases: list):
144143
# Find matching primary aliases for this locale that are not
145144
# being ignored
146145
matches = []
147-
for a in aliases:
146+
for alias in valid_aliases:
148147
if (
149-
a["locale"] == locale
150-
and "primary" in a
151-
and a.get("type", "").lower() not in ignored_alias_types
148+
alias["locale"] == locale
149+
and "primary" in alias
150+
and alias.get("type", "").lower() not in ignored_alias_types
152151
):
153-
matches.append(a)
152+
matches.append(alias)
154153

155154
# Skip to the next locale if we have no matches
156155
if not matches:
@@ -160,7 +159,7 @@ def _preferred_alias(aliases: list):
160159

161160

162161
def _multi_artist_credit(
163-
credit: list[dict], include_join_phrase: bool
162+
credit: list[JSONDict], include_join_phrase: bool
164163
) -> tuple[list[str], list[str], list[str]]:
165164
"""Given a list representing an ``artist-credit`` block, accumulate
166165
data into a triple of joined artist name lists: canonical, sort, and
@@ -212,7 +211,7 @@ def track_url(trackid: str) -> str:
212211
return urljoin(BASE_URL, "recording/" + trackid)
213212

214213

215-
def _flatten_artist_credit(credit: list[dict]) -> tuple[str, str, str]:
214+
def _flatten_artist_credit(credit: list[JSONDict]) -> tuple[str, str, str]:
216215
"""Given a list representing an ``artist-credit`` block, flatten the
217216
data into a triple of joined artist name strings: canonical, sort, and
218217
credit.
@@ -227,7 +226,7 @@ def _flatten_artist_credit(credit: list[dict]) -> tuple[str, str, str]:
227226
)
228227

229228

230-
def _artist_ids(credit: list[dict]) -> list[str]:
229+
def _artist_ids(credit: list[JSONDict]) -> list[str]:
231230
"""
232231
Given a list representing an ``artist-credit``,
233232
return a list of artist IDs
@@ -320,8 +319,8 @@ def _is_translation(r):
320319

321320

322321
def _find_actual_release_from_pseudo_release(
323-
pseudo_rel: dict,
324-
) -> dict | None:
322+
pseudo_rel: JSONDict,
323+
) -> JSONDict | None:
325324
try:
326325
relations = pseudo_rel["release"]["release-relation-list"]
327326
except KeyError:
@@ -417,7 +416,7 @@ def __init__(self):
417416

418417
def track_info(
419418
self,
420-
recording: dict,
419+
recording: JSONDict,
421420
index: int | None = None,
422421
medium: int | None = None,
423422
medium_index: int | None = None,
@@ -518,7 +517,7 @@ def track_info(
518517

519518
return info
520519

521-
def album_info(self, release: dict) -> beets.autotag.hooks.AlbumInfo:
520+
def album_info(self, release: JSONDict) -> beets.autotag.hooks.AlbumInfo:
522521
"""Takes a MusicBrainz release result dictionary and returns a beets
523522
AlbumInfo object containing the interesting data about that release.
524523
"""

0 commit comments

Comments
 (0)