Move deprecate_imports to beets.util.deprecation

This commit is contained in:
Šarūnas Nejus 2025-10-24 23:45:36 +01:00
parent 95b3364361
commit c79cad4ed1
No known key found for this signature in database
5 changed files with 29 additions and 27 deletions

View file

@ -17,7 +17,7 @@ from sys import stderr
import confuse import confuse
from .util import deprecate_imports from .util.deprecation import deprecate_imports
__version__ = "2.5.1" __version__ = "2.5.1"
__author__ = "Adrian Sampson <adrian@radbox.org>" __author__ = "Adrian Sampson <adrian@radbox.org>"

View file

@ -24,8 +24,8 @@ from beets import config, logging
# Parts of external interface. # Parts of external interface.
from beets.util import unique_list from beets.util import unique_list
from beets.util.deprecation import deprecate_imports
from ..util import deprecate_imports
from .hooks import AlbumInfo, AlbumMatch, TrackInfo, TrackMatch from .hooks import AlbumInfo, AlbumMatch, TrackInfo, TrackMatch
from .match import Proposal, Recommendation, tag_album, tag_item from .match import Proposal, Recommendation, tag_album, tag_item

View file

@ -1,4 +1,4 @@
from beets.util import deprecate_imports from beets.util.deprecation import deprecate_imports
from .exceptions import FileOperationError, ReadError, WriteError from .exceptions import FileOperationError, ReadError, WriteError
from .library import Library from .library import Library

View file

@ -27,7 +27,6 @@ import subprocess
import sys import sys
import tempfile import tempfile
import traceback import traceback
import warnings
from collections import Counter from collections import Counter
from collections.abc import Callable, Sequence from collections.abc import Callable, Sequence
from contextlib import suppress from contextlib import suppress
@ -1195,26 +1194,3 @@ def get_temp_filename(
def unique_list(elements: Iterable[T]) -> list[T]: def unique_list(elements: Iterable[T]) -> list[T]:
"""Return a list with unique elements in the original order.""" """Return a list with unique elements in the original order."""
return list(dict.fromkeys(elements)) return list(dict.fromkeys(elements))
def deprecate_imports(
old_module: str, new_module_by_name: dict[str, str], name: str, version: str
) -> Any:
"""Handle deprecated module imports by redirecting to new locations.
Facilitates gradual migration of module structure by intercepting import
attempts for relocated functionality. Issues deprecation warnings while
transparently providing access to the moved implementation, allowing
existing code to continue working during transition periods.
"""
if new_module := new_module_by_name.get(name):
warnings.warn(
(
f"'{old_module}.{name}' is deprecated and will be removed"
f" in {version}. Use '{new_module}.{name}' instead."
),
DeprecationWarning,
stacklevel=2,
)
return getattr(import_module(new_module), name)
raise AttributeError(f"module '{old_module}' has no attribute '{name}'")

26
beets/util/deprecation.py Normal file
View file

@ -0,0 +1,26 @@
import warnings
from importlib import import_module
from typing import Any
def deprecate_imports(
old_module: str, new_module_by_name: dict[str, str], name: str, version: str
) -> Any:
"""Handle deprecated module imports by redirecting to new locations.
Facilitates gradual migration of module structure by intercepting import
attempts for relocated functionality. Issues deprecation warnings while
transparently providing access to the moved implementation, allowing
existing code to continue working during transition periods.
"""
if new_module := new_module_by_name.get(name):
warnings.warn(
(
f"'{old_module}.{name}' is deprecated and will be removed"
f" in {version}. Use '{new_module}.{name}' instead."
),
DeprecationWarning,
stacklevel=2,
)
return getattr(import_module(new_module), name)
raise AttributeError(f"module '{old_module}' has no attribute '{name}'")