Skip to content

Commit 66fd8f5

Browse files
committed
Fix types in all edited files
1 parent a4ce506 commit 66fd8f5

File tree

6 files changed

+53
-50
lines changed

6 files changed

+53
-50
lines changed

beets/autotag/hooks.py

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 8 additions & 15 deletions
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

Lines changed: 9 additions & 9 deletions
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

Lines changed: 14 additions & 3 deletions
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

Lines changed: 16 additions & 17 deletions
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
@@ -45,6 +42,8 @@
4542

4643
from beets.library import Item
4744

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

5049
BASE_URL = "https://musicbrainz.org/"
@@ -125,7 +124,7 @@ def get_message(self):
125124
BROWSE_MAXTRACKS = 500
126125

127126

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

136135
# Only consider aliases that have locales set.
137-
aliases = [a for a in aliases if "locale" in a]
136+
valid_aliases = [a for a in aliases if "locale" in a]
138137

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

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

162161

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

215214

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

230229

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

322321

323322
def _find_actual_release_from_pseudo_release(
324-
pseudo_rel: dict,
325-
) -> dict | None:
323+
pseudo_rel: JSONDict,
324+
) -> JSONDict | None:
326325
try:
327326
relations = pseudo_rel["release"]["release-relation-list"]
328327
except KeyError:
@@ -418,7 +417,7 @@ def __init__(self):
418417

419418
def track_info(
420419
self,
421-
recording: dict,
420+
recording: JSONDict,
422421
index: int | None = None,
423422
medium: int | None = None,
424423
medium_index: int | None = None,
@@ -519,7 +518,7 @@ def track_info(
519518

520519
return info
521520

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

0 commit comments

Comments
 (0)