Skip to content

Commit c881db3

Browse files
authored
improve lazy import test (#30733)
Signed-off-by: Boyuan Feng <[email protected]>
1 parent 3bd9c49 commit c881db3

File tree

1 file changed

+6
-25
lines changed

1 file changed

+6
-25
lines changed

tests/standalone_tests/lazy_imports.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
# The utility function cannot be placed in `vllm.utils`
66
# this needs to be a standalone script
77
import sys
8-
from contextlib import nullcontext
9-
10-
from vllm_test_utils import BlameResult, blame
118

129
# List of modules that should not be imported too early.
1310
# Lazy import `torch._inductor.async_compile` to avoid creating
@@ -16,26 +13,10 @@
1613
# `cv2` can easily mess up the environment.
1714
module_names = ["torch._inductor.async_compile", "cv2"]
1815

16+
# set all modules in `module_names` to be None.
17+
# if we import any modules during `import vllm`, there would be a
18+
# hard error and nice stacktrace on the first import.
19+
for module_name in module_names:
20+
sys.modules[module_name] = None # type: ignore[assignment]
1921

20-
def any_module_imported():
21-
return any(module_name in sys.modules for module_name in module_names)
22-
23-
24-
# In CI, we only check finally if the module is imported.
25-
# If it is indeed imported, we can rerun the test with `use_blame=True`,
26-
# which will trace every function call to find the first import location,
27-
# and help find the root cause.
28-
# We don't run it in CI by default because it is slow.
29-
use_blame = False
30-
context = blame(any_module_imported) if use_blame else nullcontext()
31-
with context as result:
32-
import vllm # noqa
33-
34-
if use_blame:
35-
assert isinstance(result, BlameResult)
36-
print(f"the first import location is:\n{result.trace_stack}")
37-
38-
assert not any_module_imported(), (
39-
f"Some the modules in {module_names} are imported. To see the first"
40-
f" import location, run the test with `use_blame=True`."
41-
)
22+
import vllm # noqa

0 commit comments

Comments
 (0)