-
Notifications
You must be signed in to change notification settings - Fork 70
FXC-5153: making trimesh and matplotlib lazy #3213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
momchil-flex
wants to merge
5
commits into
develop
Choose a base branch
from
momchil/lazy_imports
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+273
−91
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| """Tests that heavy optional packages are not imported at top-level tidy3d import. | ||
| These tests ensure that packages like matplotlib, scipy, trimesh, and vtk | ||
| are only imported when actually needed (lazy imports), not at module load time. | ||
| This keeps the initial import time low for users who don't need these features. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import subprocess | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| # List of packages that should NOT be imported on top-level tidy3d import | ||
| LAZY_PACKAGES = [ | ||
| "matplotlib", | ||
| "scipy", | ||
| "trimesh", | ||
| "vtk", | ||
| "networkx", # transitive dependency of trimesh | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("package", LAZY_PACKAGES) | ||
| def test_package_not_imported_on_tidy3d_import(package: str) -> None: | ||
| """Test that a package is not imported when importing tidy3d. | ||
| We run this in a subprocess to ensure a clean Python environment | ||
| without any prior imports that might pollute sys.modules. | ||
| """ | ||
| # Create a script that imports tidy3d and checks if the package is in sys.modules | ||
| script = f""" | ||
| import sys | ||
| # Import tidy3d | ||
| import tidy3d | ||
| # Check if {package} was imported | ||
| if "{package}" in sys.modules: | ||
| print(f"FAIL: {package} was imported") | ||
| sys.exit(1) | ||
| else: | ||
| print(f"OK: {package} was not imported") | ||
| sys.exit(0) | ||
| """ | ||
| result = subprocess.run( | ||
| [sys.executable, "-c", script], | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
|
|
||
| # Print output for debugging | ||
| if result.stdout: | ||
| print(result.stdout) | ||
| if result.stderr: | ||
| print(result.stderr, file=sys.stderr) | ||
|
|
||
| assert result.returncode == 0, ( | ||
| f"Package '{package}' was imported on top-level tidy3d import. " | ||
| f"This increases import time. Consider making the import lazy." | ||
| ) | ||
|
|
||
|
|
||
| def test_all_lazy_packages_not_imported() -> None: | ||
| """Test that none of the lazy packages are imported when importing tidy3d. | ||
| This is a combined test that checks all packages at once, which is faster | ||
| than running separate subprocesses for each package. | ||
| """ | ||
| packages_str = ", ".join(f'"{p}"' for p in LAZY_PACKAGES) | ||
| script = f""" | ||
| import sys | ||
| # Import tidy3d | ||
| import tidy3d | ||
| # Check which packages were imported | ||
| lazy_packages = [{packages_str}] | ||
| imported = [p for p in lazy_packages if p in sys.modules] | ||
| if imported: | ||
| print(f"FAIL: These packages were imported: {{imported}}") | ||
| sys.exit(1) | ||
| else: | ||
| print(f"OK: None of the lazy packages were imported") | ||
| sys.exit(0) | ||
| """ | ||
| result = subprocess.run( | ||
| [sys.executable, "-c", script], | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
|
|
||
| # Print output for debugging | ||
| if result.stdout: | ||
| print(result.stdout) | ||
| if result.stderr: | ||
| print(result.stderr, file=sys.stderr) | ||
|
|
||
| assert result.returncode == 0, ( | ||
| f"Some lazy packages were imported on top-level tidy3d import: {result.stdout}" | ||
| ) | ||
|
|
||
|
|
||
| def test_matplotlib_imported_on_plot() -> None: | ||
| """Test that matplotlib IS imported when a plot function is called.""" | ||
| script = """ | ||
| import sys | ||
| import tidy3d as td | ||
| # matplotlib should not be imported yet | ||
| assert "matplotlib" not in sys.modules, "matplotlib imported too early" | ||
| # Create a simple simulation and try to plot it | ||
| sim = td.Simulation( | ||
| size=(1, 1, 1), | ||
| grid_spec=td.GridSpec.auto(wavelength=1.0), | ||
| run_time=1e-12, | ||
| ) | ||
| # This should trigger matplotlib import | ||
| import matplotlib | ||
| matplotlib.use('Agg') # Use non-interactive backend for testing | ||
| ax = sim.plot(z=0) | ||
| # Now matplotlib should be imported | ||
| assert "matplotlib" in sys.modules, "matplotlib should be imported after plotting" | ||
| print("OK: matplotlib imported only when needed") | ||
| """ | ||
| result = subprocess.run( | ||
| [sys.executable, "-c", script], | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
|
|
||
| # Print output for debugging | ||
| if result.stdout: | ||
| print(result.stdout) | ||
| if result.stderr: | ||
| print(result.stderr, file=sys.stderr) | ||
|
|
||
| assert result.returncode == 0, f"Test failed: {result.stderr}" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.