Improved NamedTuple declaration (#5393)

Utilize a [new way of declaring
NamedTuples](https://docs.python.org/3/library/typing.html#typing.NamedTuple),
which allows for typechecking as well. Maybe the latter is now redundant
in other places, but I'm not that familiar with the codebase yet, so I
just changed the declarations (and hopefully used the correct types).
While I was at it, I also ran `poetry update`, but I'll revert
poetry.lock in case I wasn't supposed to do that. This is my first
commit here, so I hope I didn't do anything wrong...
This commit is contained in:
Šarūnas Nejus 2024-08-27 13:30:13 +01:00 committed by GitHub
commit cd9347686a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 43 additions and 17 deletions

3
.gitignore vendored
View file

@ -91,3 +91,6 @@ ENV/
/.pydevproject
/.settings
.vscode
# pyright
pyrightconfig.json

View file

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

View file

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

View file

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

View file

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

View file

@ -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):