Skip to content

Commit 424198c

Browse files
committed
Add test that asserts torch is lazy-loaded
1 parent 5947f0a commit 424198c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tests/test_imports.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import importlib
44
import pkgutil
5+
import subprocess
6+
import sys
57

68
import pytest
79

@@ -21,3 +23,27 @@ def find_modules() -> list[str]:
2123
def test_imports(module: str):
2224
"""All modules can be imported without throwing errors."""
2325
importlib.import_module(module)
26+
27+
28+
WHITELIST = [
29+
"baybe.acquisition.adapter",
30+
"baybe.acquisition.partial",
31+
"baybe.utils.botorch_wrapper",
32+
"baybe.utils.torch",
33+
]
34+
"""List of modules that are allowed to import torch."""
35+
36+
37+
def test_torch_lazy_loading():
38+
"""Torch does not appear in the module list after loading BayBE modules."""
39+
imps = "\n".join([f"import {i}" for i in find_modules() if i not in WHITELIST])
40+
code = "\n".join(
41+
[
42+
"import sys",
43+
f"{imps}",
44+
"exit(int('torch' in sys.modules.keys()))",
45+
]
46+
)
47+
python_interpreter = sys.executable
48+
result = subprocess.call([python_interpreter, "-c", code])
49+
assert result == 0

0 commit comments

Comments
 (0)