diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index 780bbcc3a..3fa80c6f3 100644 --- a/beets/autotag/hooks.py +++ b/beets/autotag/hooks.py @@ -17,18 +17,21 @@ from __future__ import annotations import re -from collections.abc import Iterable, Iterator from functools import total_ordering -from typing import Any, Callable, NamedTuple, TypeVar, cast +from typing import TYPE_CHECKING, Any, Callable, NamedTuple, TypeVar, cast from jellyfish import levenshtein_distance from unidecode import unidecode from beets import config, logging, plugins from beets.autotag import mb -from beets.library import Item from beets.util import as_string, cached_classproperty +if TYPE_CHECKING: + from collections.abc import Iterable, Iterator + + from beets.library import Item + log = logging.getLogger("beets") V = TypeVar("V") diff --git a/beets/autotag/match.py b/beets/autotag/match.py index a9df116e5..c1642b05a 100644 --- a/beets/autotag/match.py +++ b/beets/autotag/match.py @@ -22,7 +22,7 @@ import datetime import re from collections.abc import Iterable, Sequence from enum import IntEnum -from typing import Any, NamedTuple, TypeVar, Union, cast +from typing import TYPE_CHECKING, Any, NamedTuple, TypeVar, Union, cast from munkres import Munkres @@ -35,9 +35,11 @@ from beets.autotag import ( TrackMatch, hooks, ) -from beets.library import Item from beets.util import plurality +if TYPE_CHECKING: + from beets.library import Item + # Artist signals that indicate "various artists". These are used at the # album level to determine whether a given release is likely a VA # release and also on the track level to to remove the penalty for diff --git a/beets/dbcore/db.py b/beets/dbcore/db.py index df2cbc099..72bd9f2b4 100755 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -26,8 +26,7 @@ from abc import ABC from collections import defaultdict from collections.abc import Generator, Iterable, Iterator, Mapping, Sequence from sqlite3 import Connection -from types import TracebackType -from typing import Any, AnyStr, Callable, Generic, TypeVar, cast +from typing import TYPE_CHECKING, Any, AnyStr, Callable, Generic, TypeVar, cast from unidecode import unidecode @@ -45,6 +44,9 @@ from .query import ( TrueQuery, ) +if TYPE_CHECKING: + from types import TracebackType + class DBAccessError(Exception): """The SQLite database became inaccessible. diff --git a/beets/dbcore/queryparse.py b/beets/dbcore/queryparse.py index 5335d64c0..f71f9c25c 100644 --- a/beets/dbcore/queryparse.py +++ b/beets/dbcore/queryparse.py @@ -18,10 +18,14 @@ from __future__ import annotations import itertools import re -from collections.abc import Collection, Sequence +from typing import TYPE_CHECKING from . import Model, query -from .query import Sort + +if TYPE_CHECKING: + from collections.abc import Collection, Sequence + + from .query import Sort PARSE_QUERY_PART_REGEX = re.compile( # Non-capturing optional segment for the keyword. diff --git a/beets/util/__init__.py b/beets/util/__init__.py index a929a3f7f..581e5cef7 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -28,7 +28,6 @@ import sys import tempfile import traceback from collections import Counter -from collections.abc import Iterator, Sequence from contextlib import suppress from enum import Enum from importlib import import_module @@ -50,6 +49,7 @@ from unidecode import unidecode from beets.util import hidden if TYPE_CHECKING: + from collections.abc import Iterator, Sequence from logging import Logger if sys.version_info >= (3, 10): diff --git a/beetsplug/autobpm.py b/beetsplug/autobpm.py index 96eccfb7b..9c953f711 100644 --- a/beetsplug/autobpm.py +++ b/beetsplug/autobpm.py @@ -16,14 +16,17 @@ from __future__ import annotations from collections.abc import Iterable +from typing import TYPE_CHECKING import librosa -from beets.importer import ImportTask -from beets.library import Item, Library from beets.plugins import BeetsPlugin from beets.ui import Subcommand, should_write +if TYPE_CHECKING: + from beets.importer import ImportTask + from beets.library import Item, Library + class AutoBPMPlugin(BeetsPlugin): def __init__(self) -> None: diff --git a/beetsplug/replaygain.py b/beetsplug/replaygain.py index 964b6d0a3..5ee9aa486 100644 --- a/beetsplug/replaygain.py +++ b/beetsplug/replaygain.py @@ -18,7 +18,6 @@ from __future__ import annotations import collections import enum import math -import optparse import os import queue import signal @@ -26,21 +25,25 @@ import subprocess import sys import warnings from abc import ABC, abstractmethod -from collections.abc import Sequence from dataclasses import dataclass -from logging import Logger from multiprocessing.pool import ThreadPool from threading import Event, Thread -from typing import Any, Callable, TypeVar, cast - -from confuse import ConfigView +from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast from beets import ui -from beets.importer import ImportSession, ImportTask -from beets.library import Album, Item, Library from beets.plugins import BeetsPlugin from beets.util import command_output, displayable_path, syspath +if TYPE_CHECKING: + import optparse + from collections.abc import Sequence + from logging import Logger + + from confuse import ConfigView + + from beets.importer import ImportSession, ImportTask + from beets.library import Album, Item, Library + # Utilities. diff --git a/pyproject.toml b/pyproject.toml index a65184837..2f730c9c3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -255,6 +255,7 @@ select = [ "PT", # flake8-pytest-style # "RUF", # ruff # "UP", # pyupgrade + "TCH", # flake8-type-checking "W", # pycodestyle ] [tool.ruff.lint.per-file-ignores] diff --git a/test/plugins/test_hook.py b/test/plugins/test_hook.py index d9de15283..993b95911 100644 --- a/test/plugins/test_hook.py +++ b/test/plugins/test_hook.py @@ -18,13 +18,15 @@ from __future__ import annotations import os.path import sys import unittest -from collections.abc import Iterator from contextlib import contextmanager -from typing import Callable +from typing import TYPE_CHECKING, Callable from beets import plugins from beets.test.helper import PluginTestCase, capture_log +if TYPE_CHECKING: + from collections.abc import Iterator + class HookTestCase(PluginTestCase): plugin = "hook"