diff --git a/.gitignore b/.gitignore index 15f11a433..90ef7387d 100644 --- a/.gitignore +++ b/.gitignore @@ -91,3 +91,6 @@ ENV/ /.pydevproject /.settings .vscode + +# pyright +pyrightconfig.json diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index 0a9b7daf4..363bcaab4 100644 --- a/beets/autotag/hooks.py +++ b/beets/autotag/hooks.py @@ -17,7 +17,6 @@ from __future__ import annotations import re -from collections import namedtuple from functools import total_ordering from typing import ( Any, @@ -26,6 +25,7 @@ from typing import ( Iterable, Iterator, List, + NamedTuple, Optional, Tuple, TypeVar, @@ -589,11 +589,18 @@ class Distance: # Structures that compose all the information for a candidate match. -AlbumMatch = namedtuple( - "AlbumMatch", ["distance", "info", "mapping", "extra_items", "extra_tracks"] -) -TrackMatch = namedtuple("TrackMatch", ["distance", "info"]) +class AlbumMatch(NamedTuple): + distance: Distance + info: AlbumInfo + mapping: Dict[Item, TrackInfo] + extra_items: List[Item] + extra_tracks: List[TrackInfo] + + +class TrackMatch(NamedTuple): + distance: Distance + info: TrackInfo # Aggregation of sources. diff --git a/beets/autotag/match.py b/beets/autotag/match.py index 63db9e33c..23f81ce92 100644 --- a/beets/autotag/match.py +++ b/beets/autotag/match.py @@ -17,14 +17,16 @@ releases and tracks. """ +from __future__ import annotations + import datetime import re -from collections import namedtuple from typing import ( Any, Dict, Iterable, List, + NamedTuple, Optional, Sequence, Tuple, @@ -76,7 +78,10 @@ class Recommendation(OrderedEnum): # consists of a list of possible candidates (i.e., AlbumInfo or TrackInfo # objects) and a recommendation value. -Proposal = namedtuple("Proposal", ("candidates", "recommendation")) + +class Proposal(NamedTuple): + candidates: Sequence[AlbumMatch | TrackMatch] + recommendation: Recommendation # Primary matching functionality. @@ -351,7 +356,7 @@ def match_by_id(items: Iterable[Item]): def _recommendation( - results: Sequence[Union[AlbumMatch, TrackMatch]], + results: Sequence[AlbumMatch | TrackMatch], ) -> Recommendation: """Given a sorted list of AlbumMatch or TrackMatch objects, return a recommendation based on the results' distances. diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 826dc07a3..24cae1dd1 100755 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -19,10 +19,10 @@ interface. import os import re -from collections import Counter, namedtuple +from collections import Counter from itertools import chain from platform import python_version -from typing import Sequence +from typing import Any, NamedTuple, Sequence import beets from beets import autotag, config, importer, library, logging, plugins, ui, util @@ -47,7 +47,6 @@ from beets.util import ( from . import _store_dict VARIOUS_ARTISTS = "Various Artists" -PromptChoice = namedtuple("PromptChoice", ["short", "long", "callback"]) # Global logger. log = logging.getLogger("beets") @@ -664,8 +663,8 @@ class AlbumChange(ChangeRepresentation): suggests for them. """ # Tracks. - # match is an AlbumMatch named tuple, mapping is a dict - # Sort the pairs by the track_info index (at index 1 of the namedtuple) + # match is an AlbumMatch NamedTuple, mapping is a dict + # Sort the pairs by the track_info index (at index 1 of the NamedTuple) pairs = list(self.match.mapping.items()) pairs.sort(key=lambda item_and_track_info: item_and_track_info[1].index) # Build up LHS and RHS for track difference display. The `lines` list @@ -840,6 +839,12 @@ def _summary_judgment(rec): return action +class PromptChoice(NamedTuple): + short: str + long: str + callback: Any + + def choose_candidate( candidates, singleton, diff --git a/beets/util/__init__.py b/beets/util/__init__.py index bfb23c053..4f0aa283c 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -26,7 +26,7 @@ import subprocess import sys import tempfile import traceback -from collections import Counter, namedtuple +from collections import Counter from contextlib import suppress from enum import Enum from logging import Logger @@ -40,6 +40,7 @@ from typing import ( Iterable, List, MutableSequence, + NamedTuple, Optional, Pattern, Sequence, @@ -847,7 +848,9 @@ def convert_command_args(args: List[bytes]) -> List[str]: # stdout and stderr as bytes -CommandOutput = namedtuple("CommandOutput", ("stdout", "stderr")) +class CommandOutput(NamedTuple): + stdout: bytes + stderr: bytes def command_output( diff --git a/beets/vfs.py b/beets/vfs.py index 4a9681a92..1e217a43b 100644 --- a/beets/vfs.py +++ b/beets/vfs.py @@ -16,11 +16,14 @@ libraries. """ -from collections import namedtuple +from typing import Any, Dict, NamedTuple from beets import util -Node = namedtuple("Node", ["files", "dirs"]) + +class Node(NamedTuple): + files: Dict[str, Any] + dirs: Dict[str, Any] def _insert(node, path, itemid):