Skip to content

Commit c715d03

Browse files
authored
[Cherry-pick] Fix backward compatibility layer in backend module (#3595) (#3596)
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 075dfb8 commit c715d03

File tree

6 files changed

+44
-35
lines changed

6 files changed

+44
-35
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/backend/__init__.py

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

6-
from .utils import _init_backend, get_audio_backend, list_audio_backends, set_audio_backend
76

7+
from . import common, no_backend, soundfile_backend, sox_io_backend # noqa
8+
from .utils import _init_backend, get_audio_backend, list_audio_backends, set_audio_backend
89

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

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)