Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing #5396

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ ENV/
/.settings
.vscode

# pyright
pyrightconfig.json
# Pyright
pyrightconfig.json
3 changes: 2 additions & 1 deletion beets/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import traceback
from collections import defaultdict
from functools import wraps
from typing import Any, Dict

import mediafile

Expand Down Expand Up @@ -291,7 +292,7 @@ def load_plugins(names=()):
)


_instances = {}
_instances: Dict[Any, Any] = {}


def find_plugins():
Expand Down
20 changes: 12 additions & 8 deletions beets/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
"""Provide the canonical form of the path suitable for storing in
the database.
"""
path = syspath(path, prefix=False)

Check failure on line 171 in beets/util/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Incompatible types in assignment (expression has type "str", variable has type "bytes")
path = os.path.normpath(os.path.abspath(os.path.expanduser(path)))
return bytestring_path(path)

Expand All @@ -182,7 +182,7 @@

The argument should *not* be the result of a call to `syspath`.
"""
out = []

Check failure on line 185 in beets/util/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Need type annotation for "out" (hint: "out: List[<type>] = ...")
last_path = None
while path:
path = os.path.dirname(path)
Expand All @@ -199,17 +199,17 @@

def sorted_walk(
path: AnyStr,
ignore: Sequence = (),

Check failure on line 202 in beets/util/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Missing type parameters for generic type "Sequence"
ignore_hidden: bool = False,
logger: Optional[Logger] = None,
) -> Generator[Tuple, None, None]:

Check failure on line 205 in beets/util/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Missing type parameters for generic type "Tuple"
"""Like `os.walk`, but yields things in case-insensitive sorted,
breadth-first order. Directory and file names matching any glob
pattern in `ignore` are skipped. If `logger` is provided, then
warning messages are logged there when a directory cannot be listed.
"""
# Make sure the paths aren't Unicode strings.
path = bytestring_path(path)

Check failure on line 212 in beets/util/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Incompatible types in assignment (expression has type "bytes", variable has type "str")
ignore = [bytestring_path(i) for i in ignore]

# Get all the directories and files at this level.
Expand All @@ -226,7 +226,7 @@
dirs = []
files = []
for base in contents:
base = bytestring_path(base)

Check failure on line 229 in beets/util/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Incompatible types in assignment (expression has type "bytes", variable has type "str")

# Skip ignored filenames.
skip = False
Expand All @@ -242,16 +242,16 @@
continue

# Add to output as either a file or a directory.
cur = os.path.join(path, base)

Check failure on line 245 in beets/util/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

No overload variant of "join" matches argument types "bytes", "str"
if (ignore_hidden and not hidden.is_hidden(cur)) or not ignore_hidden:

Check failure on line 246 in beets/util/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument 1 to "is_hidden" has incompatible type "str"; expected "Union[bytes, Path]"
if os.path.isdir(syspath(cur)):
dirs.append(base)
else:
files.append(base)

# Sort lists (case-insensitive) and yield the current level.
dirs.sort(key=bytes.lower)

Check failure on line 253 in beets/util/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument "key" to "sort" of "list" has incompatible type "Callable[[bytes], bytes]"; expected "Callable[[str], Union[SupportsDunderLT[Any], SupportsDunderGT[Any]]]"
files.sort(key=bytes.lower)

Check failure on line 254 in beets/util/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument "key" to "sort" of "list" has incompatible type "Callable[[bytes], bytes]"; expected "Callable[[str], Union[SupportsDunderLT[Any], SupportsDunderGT[Any]]]"
yield (path, dirs, files)

# Recurse into directories.
Expand Down Expand Up @@ -300,7 +300,7 @@
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
Expand Down Expand Up @@ -332,7 +332,7 @@
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):
Expand Down Expand Up @@ -441,14 +441,18 @@
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
Expand Down Expand Up @@ -760,7 +764,7 @@
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).
Expand Down Expand Up @@ -827,7 +831,7 @@
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.
Expand All @@ -838,7 +842,7 @@
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)
Expand All @@ -858,7 +862,7 @@


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.
Expand Down Expand Up @@ -1040,7 +1044,7 @@
# 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:
Expand Down