1616from tidy3d .config import config
1717
1818from .exceptions import Tidy3dImportError
19- from .log import log
2019from .version import __version__
2120
2221vtk = {
@@ -192,37 +191,41 @@ def _check_tidy3d_extras_available():
192191 Tidy3dImportError
193192 If tidy3d-extras is not available or not properly initialized.
194193 """
195- if tidy3d_extras ["mod" ] is None :
196- try :
197- import tidy3d_extras as tidy3d_extras_mod
194+ if tidy3d_extras ["mod" ] is not None :
195+ return
198196
199- except ImportError as exc :
200- tidy3d_extras [ "mod" ] = None
201- raise Tidy3dImportError (
202- "The package 'tidy3d-extras' is required for this "
203- "operation. Please install the 'tidy3d-extras' package using, for "
204- "example, 'pip install tidy3d[extras]'."
205- ) from exc
197+ module_exists = find_spec ( "tidy3d_extras" ) is not None
198+ if not module_exists :
199+ raise Tidy3dImportError (
200+ "The package 'tidy3d-extras' is absent. "
201+ " Please install the 'tidy3d-extras' package using, for "
202+ "example, 'pip install tidy3d[extras]'."
203+ )
206204
207- else :
208- version = tidy3d_extras_mod . __version__
205+ try :
206+ import tidy3d_extras as tidy3d_extras_mod
209207
210- if version is None :
211- tidy3d_extras ["mod" ] = None
212- raise Tidy3dImportError (
213- "The package 'tidy3d-extras' did not initialize correctly, "
214- "likely due to an invalid API key."
215- )
208+ except ImportError as exc :
209+ raise Tidy3dImportError (
210+ "The package 'tidy3d-extras' did not initialize correctly."
211+ ) from exc
216212
217- if version != __version__ :
218- log . warning (
219- "The package 'tidy3d-extras' is required for this "
220- "operation. The version of 'tidy3d-extras' should match "
221- "the version of 'tidy3d'. You can install the correct "
222- "version using 'pip install tidy3d[extras]' ."
223- )
213+ version = tidy3d_extras_mod . __version__
214+
215+ if version is None :
216+ raise Tidy3dImportError (
217+ "The package 'tidy3d-extras' did not initialize correctly, "
218+ "likely due to an invalid API key ."
219+ )
224220
225- tidy3d_extras ["mod" ] = tidy3d_extras_mod
221+ if version != __version__ :
222+ raise Tidy3dImportError (
223+ f"The version of 'tidy3d-extras' is { version } , but the version of 'tidy3d' is { __version__ } . "
224+ "They must match. You can install the correct "
225+ "version using 'pip install tidy3d[extras]'."
226+ )
227+
228+ tidy3d_extras ["mod" ] = tidy3d_extras_mod
226229
227230
228231def check_tidy3d_extras_licensed_feature (feature_name : str ):
@@ -242,14 +245,16 @@ def check_tidy3d_extras_licensed_feature(feature_name: str):
242245 try :
243246 _check_tidy3d_extras_available ()
244247 except Tidy3dImportError as exc :
245- raise Tidy3dImportError (f"Failed to load 'tidy3d-extras'. { exc !s} " ) from exc
246- else :
247- features = tidy3d_extras ["mod" ].extension ._features ()
248- if feature_name not in features :
249- raise Tidy3dImportError (
250- f"The feature '{ feature_name } ' is not available with your license. "
251- "Please contact Tidy3D support, or upgrade your license."
252- )
248+ raise Tidy3dImportError (
249+ f"The package 'tidy3d-extras' is required for this feature '{ feature_name } '. { exc !s} "
250+ ) from exc
251+
252+ features = tidy3d_extras ["mod" ].extension ._features ()
253+ if feature_name not in features :
254+ raise Tidy3dImportError (
255+ f"The feature '{ feature_name } ' is not available with your license. "
256+ "Please contact Tidy3D support, or upgrade your license."
257+ )
253258
254259
255260def supports_local_subpixel (fn ):
@@ -262,18 +267,22 @@ def _fn(*args: Any, **kwargs: Any):
262267
263268 if preference is False :
264269 tidy3d_extras ["use_local_subpixel" ] = False
265- else :
266- try :
267- _check_tidy3d_extras_available ()
268- except Tidy3dImportError as exc :
269- tidy3d_extras ["use_local_subpixel" ] = False
270- if preference is True :
271- raise Tidy3dImportError (
272- f"Failed to load 'tidy3d-extras' for local subpixel support. { exc !s} "
273- ) from exc
274- else :
275- features = tidy3d_extras ["mod" ].extension ._features ()
276- tidy3d_extras ["use_local_subpixel" ] = "local_subpixel" in features
270+ return fn (* args , ** kwargs )
271+
272+ try :
273+ check_tidy3d_extras_licensed_feature ("local_subpixel" )
274+ except Tidy3dImportError as exc :
275+ tidy3d_extras ["use_local_subpixel" ] = False
276+ if preference is True :
277+ raise Tidy3dImportError (
278+ f"{ exc !s} To suppress this error, you can set "
279+ "'config.simulation.use_local_subpixel=False'."
280+ ) from exc
281+ else : # preference is None, so we can just return
282+ return fn (* args , ** kwargs )
283+
284+ # local_subpixel is available
285+ tidy3d_extras ["use_local_subpixel" ] = True
277286
278287 return fn (* args , ** kwargs )
279288
0 commit comments