Skip to content

Commit 931598c

Browse files
mthrokfacebook-github-bot
authored andcommitted
Fix backward compatibility layer in backend module (#3595)
Summary: The PR #3549 re-organized the backend implementations and deprecated the direct access to torchaudio.backend. The change was supposed to be BC-compatible while issuing a warning to users, but the implementation of module-level `__getattr__` was not quite right. See an issue pyannote/pyannote-audio#1456. This commit fixes it so that the following imports work; ```python from torchaudio.backend.common import AudioMetaData from torchaudio.backend import sox_io_backend from torchaudio.backend.sox_io_backend import save, load, info from torchaudio.backend import no_backend from torchaudio.backend.no_backend import save, load, info from torchaudio.backend import soundfile_backend from torchaudio.backend.soundfile_backend import save, load, info ``` Pull Request resolved: #3595 Reviewed By: nateanl Differential Revision: D48957446 Pulled By: mthrok fbshipit-source-id: ebb256461dd3032025fd27d0455ce980888f7778
1 parent 454418d commit 931598c

File tree

8 files changed

+61
-40
lines changed

8 files changed

+61
-40
lines changed

test/torchaudio_unittest/backend/soundfile/info_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ class MockSoundFileInfo:
117117
with patch("soundfile.info", _mock_info_func):
118118
with warnings.catch_warnings(record=True) as w:
119119
info = soundfile_backend.info("foo")
120-
assert len(w) == 1
121120
assert "UNSEEN_SUBTYPE subtype is unknown to TorchAudio" in str(w[-1].message)
122121
assert info.bits_per_sample == 0
123122

torchaudio/__init__.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# Initialize extension and backend first
2-
from . import ( # noqa # usort: skip
3-
_extension,
4-
_backend,
2+
from . import _extension # noqa # usort: skip
3+
from ._backend import ( # noqa # usort: skip
4+
AudioMetaData,
5+
get_audio_backend,
6+
info,
7+
list_audio_backends,
8+
load,
9+
save,
10+
set_audio_backend,
511
)
12+
613
from . import ( # noqa: F401
7-
backend, # For BC
814
compliance,
915
datasets,
1016
functional,
@@ -16,7 +22,9 @@
1622
transforms,
1723
utils,
1824
)
19-
from ._backend import AudioMetaData, get_audio_backend, info, list_audio_backends, load, save, set_audio_backend
25+
26+
# For BC
27+
from . import backend # noqa # usort: skip
2028

2129
try:
2230
from .version import __version__, git_version # noqa: F401

torchaudio/backend/__init__.py

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,6 @@
33
# New things should be added to `torchaudio._backend`.
44
# Only things related to backward compatibility should be placed here.
55

6+
from . import common, no_backend, soundfile_backend, sox_io_backend # noqa
67

7-
def __getattr__(name: str):
8-
if name == "common":
9-
from . import _common
10-
11-
return _common
12-
13-
if name in ["no_backend", "sox_io_backend", "soundfile_backend"]:
14-
import warnings
15-
16-
warnings.warn(
17-
"Torchaudio's I/O functions now support par-call bakcend dispatch. "
18-
"Importing backend implementation directly is no longer guaranteed to work. "
19-
"Please use `backend` keyword with load/save/info function, instead of "
20-
"calling the udnerlying implementation directly.",
21-
stacklevel=2,
22-
)
23-
24-
if name == "sox_io_backend":
25-
from . import _sox_io_backend
26-
27-
return _sox_io_backend
28-
if name == "soundfile_backend":
29-
from torchaudio._backend import soundfile_backend
30-
31-
return soundfile_backend
32-
33-
if name == "no_backend":
34-
from . import _no_backend
35-
36-
return _no_backend
37-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
8+
__all__ = []

torchaudio/backend/_no_backend.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Callable, Optional, Tuple, Union
33

44
from torch import Tensor
5+
from torchaudio import AudioMetaData
56

67

78
def load(
@@ -20,5 +21,5 @@ def save(filepath: str, src: Tensor, sample_rate: int, precision: int = 16, chan
2021
raise RuntimeError("No audio I/O backend is available.")
2122

2223

23-
def info(filepath: str) -> None:
24+
def info(filepath: str) -> AudioMetaData:
2425
raise RuntimeError("No audio I/O backend is available.")

torchaudio/backend/_common.py renamed to torchaudio/backend/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
def __getattr__(name: str):
2-
import warnings
3-
42
if name == "AudioMetaData":
3+
import warnings
4+
55
warnings.warn(
66
"`torchaudio.backend.common.AudioMetaData` has been moved to "
77
"`torchaudio.AudioMetaData`. Please update the import path.",

torchaudio/backend/no_backend.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def __getattr__(name: str):
2+
import warnings
3+
4+
warnings.warn(
5+
"Torchaudio's I/O functions now support par-call bakcend dispatch. "
6+
"Importing backend implementation directly is no longer guaranteed to work. "
7+
"Please use `backend` keyword with load/save/info function, instead of "
8+
"calling the udnerlying implementation directly.",
9+
stacklevel=2,
10+
)
11+
12+
from . import _no_backend
13+
14+
return getattr(_no_backend, name)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def __getattr__(name: str):
2+
import warnings
3+
4+
warnings.warn(
5+
"Torchaudio's I/O functions now support par-call bakcend dispatch. "
6+
"Importing backend implementation directly is no longer guaranteed to work. "
7+
"Please use `backend` keyword with load/save/info function, instead of "
8+
"calling the udnerlying implementation directly.",
9+
stacklevel=2,
10+
)
11+
12+
from torchaudio._backend import soundfile_backend
13+
14+
return getattr(soundfile_backend, name)

torchaudio/backend/sox_io_backend.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def __getattr__(name: str):
2+
import warnings
3+
4+
warnings.warn(
5+
"Torchaudio's I/O functions now support par-call bakcend dispatch. "
6+
"Importing backend implementation directly is no longer guaranteed to work. "
7+
"Please use `backend` keyword with load/save/info function, instead of "
8+
"calling the udnerlying implementation directly.",
9+
stacklevel=2,
10+
)
11+
12+
from . import _sox_io_backend
13+
14+
return getattr(_sox_io_backend, name)

0 commit comments

Comments
 (0)