diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index 780bbcc3a1..3fa80c6f35 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 eac6ef44ce..d3d3c016fc 100644 --- a/beets/autotag/match.py +++ b/beets/autotag/match.py @@ -22,7 +22,7 @@ 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 @@ 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 df2cbc0990..72bd9f2b47 100755 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -26,8 +26,7 @@ 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 @@ 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 5335d64c03..f71f9c25c8 100644 --- a/beets/dbcore/queryparse.py +++ b/beets/dbcore/queryparse.py @@ -18,10 +18,14 @@ 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 a929a3f7f9..581e5cef76 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -28,7 +28,6 @@ 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 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 96eccfb7b8..9c953f7117 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 964b6d0a3a..5ee9aa4869 100644 --- a/beetsplug/replaygain.py +++ b/beetsplug/replaygain.py @@ -18,7 +18,6 @@ import collections import enum import math -import optparse import os import queue import signal @@ -26,21 +25,25 @@ 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 c91b01820a..126b97de4b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -238,6 +238,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 d9de152838..993b95911a 100644 --- a/test/plugins/test_hook.py +++ b/test/plugins/test_hook.py @@ -18,13 +18,15 @@ 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"