Skip to content

Commit ebc0709

Browse files
committed
Deprecate beets.autotag.Distance beets.autotag.current_metadata
1 parent 3dd18dc commit ebc0709

File tree

4 files changed

+56
-25
lines changed

4 files changed

+56
-25
lines changed

beets/autotag/__init__.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616

1717
from __future__ import annotations
1818

19+
import warnings
20+
from importlib import import_module
1921
from typing import TYPE_CHECKING, Union
2022

2123
from beets import config, logging
22-
from beets.util import get_most_common_tags as current_metadata
2324

2425
# Parts of external interface.
2526
from beets.util import unique_list
2627

27-
from .distance import Distance
28+
from ..util import deprecate_imports
2829
from .hooks import AlbumInfo, AlbumMatch, TrackInfo, TrackMatch
2930
from .match import Proposal, Recommendation, tag_album, tag_item
3031

@@ -33,18 +34,34 @@
3334

3435
from beets.library import Album, Item, LibModel
3536

37+
38+
def __getattr__(name: str):
39+
if name == "current_metadata":
40+
warnings.warn(
41+
(
42+
f"'beets.autotag.{name}' is deprecated and will be removed in"
43+
" 3.0.0. Use 'beets.util.get_most_common_tags' instead."
44+
),
45+
DeprecationWarning,
46+
stacklevel=2,
47+
)
48+
return import_module("beets.util").get_most_common_tags
49+
50+
return deprecate_imports(
51+
__name__, {"Distance": "beets.autotag.distance"}, name, "3.0.0"
52+
)
53+
54+
3655
__all__ = [
3756
"AlbumInfo",
3857
"AlbumMatch",
39-
"Distance", # for backwards compatibility
4058
"Proposal",
4159
"Recommendation",
4260
"TrackInfo",
4361
"TrackMatch",
4462
"apply_album_metadata",
4563
"apply_item_metadata",
4664
"apply_metadata",
47-
"current_metadata", # for backwards compatibility
4865
"tag_album",
4966
"tag_item",
5067
]

beets/library/__init__.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import warnings
2-
from importlib import import_module
1+
from beets.util import deprecate_imports
32

43
from .exceptions import FileOperationError, ReadError, WriteError
54
from .library import Library
@@ -14,18 +13,7 @@
1413

1514

1615
def __getattr__(name: str):
17-
if name in NEW_MODULE_BY_NAME:
18-
new_module = NEW_MODULE_BY_NAME[name]
19-
warnings.warn(
20-
(
21-
f"'beets.library.{name}' import is deprecated and will be removed"
22-
f"in v3.0.0; import '{new_module}.{name}' instead."
23-
),
24-
DeprecationWarning,
25-
stacklevel=2,
26-
)
27-
return getattr(import_module(new_module), name)
28-
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
16+
return deprecate_imports(__name__, NEW_MODULE_BY_NAME, name, "3.0.0")
2917

3018

3119
__all__ = [

beets/util/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import sys
2828
import tempfile
2929
import traceback
30+
import warnings
3031
from collections import Counter
3132
from collections.abc import Sequence
3233
from contextlib import suppress
@@ -1191,3 +1192,26 @@ def get_temp_filename(
11911192
def unique_list(elements: Iterable[T]) -> list[T]:
11921193
"""Return a list with unique elements in the original order."""
11931194
return list(dict.fromkeys(elements))
1195+
1196+
1197+
def deprecate_imports(
1198+
old_module: str, new_module_by_name: dict[str, str], name: str, version: str
1199+
) -> Any:
1200+
"""Handle deprecated module imports by redirecting to new locations.
1201+
1202+
Facilitates gradual migration of module structure by intercepting import
1203+
attempts for relocated functionality. Issues deprecation warnings while
1204+
transparently providing access to the moved implementation, allowing
1205+
existing code to continue working during transition periods.
1206+
"""
1207+
if new_module := new_module_by_name.get(name):
1208+
warnings.warn(
1209+
(
1210+
f"'{old_module}.{name}' is deprecated and will be removed"
1211+
f" in {version}. Use '{new_module}.{name}' instead."
1212+
),
1213+
DeprecationWarning,
1214+
stacklevel=2,
1215+
)
1216+
return getattr(import_module(new_module), name)
1217+
raise AttributeError(f"module '{old_module}' has no attribute '{name}'")

docs/changelog.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ For plugin developers:
7676
``album_for_id``, ``candidates``, ``item_candidates``, ``album_distance``, ``track_distance`` methods,
7777
please update your plugin to inherit from the new baseclass, as otherwise your plugin will
7878
stop working with the next major release.
79-
* Several definitions have been moved away from ``beets.library`` module:
80-
* ``BLOB_TYPE`` constant, ``PathQuery`` and ``SingletonQuery`` queries have moved
81-
to ``beets.dbcore.queries`` module
82-
* ``DateType``, ``DurationType``, ``PathType`` types and ``MusicalKey`` class have
83-
moved to ``beets.dbcore.types`` module.
84-
Old imports are now deprecated and will be removed in version ``3.0.0``.
85-
79+
* Several definitions have been moved:
80+
* ``BLOB_TYPE`` constant, ``PathQuery`` and ``SingletonQuery`` queries have
81+
moved from ``beets.library`` to ``beets.dbcore.query`` module
82+
* ``DateType``, ``DurationType``, ``PathType`` types and ``MusicalKey``
83+
class have moved from ``beets.library`` to ``beets.dbcore.types`` module.
84+
* ``Distance`` has moved from ``beets.autotag`` to
85+
``beets.autotag.distance`` module.
86+
* ``beets.autotag.current_metadata`` has been renamed to
87+
``beets.util.get_most_common_tags``.
8688

8789
Other changes:
8890

0 commit comments

Comments
 (0)