Skip to content

Commit

Permalink
Add a regression test for lazy imports
Browse files Browse the repository at this point in the history
  • Loading branch information
dean0x7d committed Aug 9, 2023
1 parent ea0b7dd commit 2a50480
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lumicks/pylake/tests/test_import_time.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import sys
import subprocess
from textwrap import dedent
Expand Down Expand Up @@ -26,3 +27,33 @@ def test_disabling_capturing(report_line):
report_line(
f"Module import time: {np.mean(times):.2f} +- {np.std(times):.2f} seconds (N={repeats})"
)


def test_lazy_imports():
"""Ensure that specific third-party modules are lazily imported so that `pylake` stays fast"""

expected_lazy_imports = [
"h5py",
"sklearn",
"matplotlib",
"scipy.stats",
"scipy.signal",
"scipy.ndimage",
"scipy.special",
"scipy.optimize",
"scipy.interpolate",
]

# `sys.modules` in this new Python process will only contain what `pylake` imports
code = dedent(
"""\
import sys
import lumicks.pylake
print(list(sys.modules.keys()))
"""
)
output = subprocess.check_output([sys.executable, "-c", code], text=True)

imported_modules = ast.literal_eval(output)
for lazy_import in expected_lazy_imports:
assert not any(lazy_import in m for m in imported_modules)

0 comments on commit 2a50480

Please sign in to comment.