88
99import functools
1010from importlib import import_module
11+ from importlib .util import find_spec
1112from typing import Any , Literal
1213
1314import numpy as np
@@ -185,7 +186,7 @@ def get_numpy_major_version(module=np):
185186
186187def supports_local_subpixel (fn ):
187188 """When decorating a method, checks that 'tidy3d-extras' is available,
188- conditioned on 'config.use_local_subpixel'."""
189+ conditioned on 'config.simulation. use_local_subpixel'."""
189190
190191 @functools .wraps (fn )
191192 def _fn (* args : Any , ** kwargs : Any ):
@@ -195,31 +196,41 @@ def _fn(*args: Any, **kwargs: Any):
195196 tidy3d_extras ["use_local_subpixel" ] = False
196197 tidy3d_extras ["mod" ] = None
197198 else :
198- # first try to import the module
199199 if tidy3d_extras ["mod" ] is None :
200- try :
201- import tidy3d_extras as tidy3d_extras_mod
202-
203- except ImportError as exc :
204- tidy3d_extras ["mod" ] = None
205- tidy3d_extras ["use_local_subpixel" ] = False
206- if preference is True :
200+ tidy3d_extras ["use_local_subpixel" ] = False
201+ tidy3d_extras ["mod" ] = None
202+ module_exists = find_spec ("tidy3d_extras" ) is not None
203+
204+ if preference is True and not module_exists :
205+ raise Tidy3dImportError (
206+ "The package 'tidy3d-extras' is required for this "
207+ "operation when 'config.simulation.use_local_subpixel' is 'True'. "
208+ "Please install the 'tidy3d-extras' package using, for "
209+ "example, 'pip install tidy3d[extras]'."
210+ )
211+
212+ if module_exists :
213+ try :
214+ import tidy3d_extras as tidy3d_extras_mod
215+
216+ except ImportError as exc :
217+ tidy3d_extras ["mod" ] = None
218+ tidy3d_extras ["use_local_subpixel" ] = False
207219 raise Tidy3dImportError (
208- "The package 'tidy3d-extras' is required for this "
209- "operation when 'config.use_local_subpixel' is 'True'. "
210- "Please install the 'tidy3d-extras' package using, for "
211- "example, 'pip install tidy3d[extras]'."
220+ "The package 'tidy3d-extras' did not initialize correctly. "
221+ "To suppress this error, you can set "
222+ "'config.simulation.use_local_subpixel=False'."
212223 ) from exc
213224
214- else :
215225 version = tidy3d_extras_mod .__version__
216226
217227 if version is None :
218228 tidy3d_extras ["mod" ] = None
219229 tidy3d_extras ["use_local_subpixel" ] = False
220230 raise Tidy3dImportError (
221231 "The package 'tidy3d-extras' did not initialize correctly, "
222- "likely due to an invalid API key."
232+ "likely due to an invalid API key. To suppress this error, "
233+ "you can set 'config.simulation.use_local_subpixel=False'."
223234 )
224235
225236 if version != __version__ :
0 commit comments