fixed + improved typing in some places

This commit is contained in:
Konstantin 2024-08-27 00:16:30 +02:00
parent e04f3e5597
commit 647d6ab29a
4 changed files with 16 additions and 11 deletions

View file

@ -78,7 +78,7 @@ class Recommendation(OrderedEnum):
class Proposal(NamedTuple):
candidates: Sequence[AlbumMatch | TrackMatch]
candidates: Sequence[Union[AlbumMatch, TrackMatch]]
recommendation: Recommendation

View file

@ -322,7 +322,7 @@ class Model(ABC):
"""
@cached_classproperty
def _relation(cls) -> type[Model]:
def _relation(cls) -> Model:
"""The model that this model is closely related to."""
return cls

View file

@ -21,6 +21,7 @@ import re
import traceback
from collections import defaultdict
from functools import wraps
from typing import Any, Dict
import mediafile
@ -291,7 +292,7 @@ def load_plugins(names=()):
)
_instances = {}
_instances: Dict[Any, Any] = {}
def find_plugins():

View file

@ -298,7 +298,7 @@ def fnmatch_all(names: Sequence[bytes], patterns: Sequence[bytes]) -> bool:
def prune_dirs(
path: str,
root: Optional[Bytes_or_String] = None,
clutter: Sequence[str] = (".DS_Store", "Thumbs.db"),
clutter: Sequence[Bytes_or_String] = [".DS_Store", "Thumbs.db"],
):
"""If path is an empty directory, then remove it. Recursively remove
path's ancestry up to root (which is never removed) where there are
@ -330,7 +330,7 @@ def prune_dirs(
if not os.path.exists(directory):
# Directory gone already.
continue
clutter: List[bytes] = [bytestring_path(c) for c in clutter]
clutter = [bytestring_path(c) for c in clutter]
match_paths = [bytestring_path(d) for d in os.listdir(directory)]
try:
if fnmatch_all(match_paths, clutter):
@ -439,14 +439,18 @@ def displayable_path(
return path.decode("utf-8", "ignore")
def syspath(path: PathLike, prefix: bool = True) -> str:
def syspath(path: Optional[PathLike], prefix: bool = True) -> str:
"""Convert a path for use by the operating system. In particular,
paths on Windows must receive a magic prefix and must be converted
to Unicode before they are sent to the OS. To disable the magic
prefix on Windows, set `prefix` to False---but only do this if you
*really* know what you're doing.
"""
if path is None:
raise ValueError("The path cannot be None")
str_path = os.fsdecode(path)
# Don't do anything if we're not on windows
if os.path.__name__ != "ntpath":
return str_path
@ -756,7 +760,7 @@ def legalize_path(
length: int,
extension: bytes,
fragment: bool,
) -> Tuple[Union[Bytes_or_String, bool]]:
) -> Tuple[Bytes_or_String, bool]:
"""Given a path-like Unicode string, produce a legal path. Return
the path and a flag indicating whether some replacements had to be
ignored (see below).
@ -823,7 +827,7 @@ def as_string(value: Any) -> str:
return str(value)
def plurality(objs: Sequence[T]) -> T:
def plurality(objs: Sequence[T]) -> Tuple[T, int]:
"""Given a sequence of hashble objects, returns the object that
is most common in the set and the its number of appearance. The
sequence must contain at least one object.
@ -834,7 +838,7 @@ def plurality(objs: Sequence[T]) -> T:
return c.most_common(1)[0]
def convert_command_args(args: List[bytes]) -> List[str]:
def convert_command_args(args: Sequence[Bytes_or_String]) -> List[str]:
"""Convert command arguments, which may either be `bytes` or `str`
objects, to uniformly surrogate-escaped strings."""
assert isinstance(args, list)
@ -854,7 +858,7 @@ class CommandOutput(NamedTuple):
def command_output(
cmd: List[Bytes_or_String],
cmd: Sequence[Bytes_or_String],
shell: bool = False,
) -> CommandOutput:
"""Runs the command and returns its output after it has exited.
@ -1036,7 +1040,7 @@ def asciify_path(path: str, sep_replace: str) -> str:
# if this platform has an os.altsep, change it to os.sep.
if os.altsep:
path = path.replace(os.altsep, os.sep)
path_components: List[Bytes_or_String] = path.split(os.sep)
path_components: Sequence[Bytes_or_String] = path.split(os.sep)
for index, item in enumerate(path_components):
path_components[index] = unidecode(item).replace(os.sep, sep_replace)
if os.altsep: