diff --git a/lumicks/pylake/tests/test_import_time.py b/lumicks/pylake/tests/test_import_time.py index b23178b96..c7054ff1e 100644 --- a/lumicks/pylake/tests/test_import_time.py +++ b/lumicks/pylake/tests/test_import_time.py @@ -1,3 +1,4 @@ +import ast import sys import subprocess from textwrap import dedent @@ -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)