@@ -183,6 +183,74 @@ def get_numpy_major_version(module=np):
183183 return major_version
184184
185185
186+ def _check_tidy3d_extras_available ():
187+ """Helper function to check if 'tidy3d-extras' is available and version matched.
188+
189+ Raises
190+ ------
191+ Tidy3dImportError
192+ If tidy3d-extras is not available or not properly initialized.
193+ """
194+ if tidy3d_extras ["mod" ] is None :
195+ try :
196+ import tidy3d_extras as tidy3d_extras_mod
197+
198+ except ImportError as exc :
199+ tidy3d_extras ["mod" ] = None
200+ raise Tidy3dImportError (
201+ "The package 'tidy3d-extras' is required for this "
202+ "operation. Please install the 'tidy3d-extras' package using, for "
203+ "example, 'pip install tidy3d[extras]'."
204+ ) from exc
205+
206+ else :
207+ version = tidy3d_extras_mod .__version__
208+
209+ if version is None :
210+ tidy3d_extras ["mod" ] = None
211+ raise Tidy3dImportError (
212+ "The package 'tidy3d-extras' did not initialize correctly, "
213+ "likely due to an invalid API key."
214+ )
215+
216+ if version != __version__ :
217+ log .warning (
218+ "The package 'tidy3d-extras' is required for this "
219+ "operation. The version of 'tidy3d-extras' should match "
220+ "the version of 'tidy3d'. You can install the correct "
221+ "version using 'pip install tidy3d[extras]'."
222+ )
223+
224+ tidy3d_extras ["mod" ] = tidy3d_extras_mod
225+
226+
227+ def check_tidy3d_extras_licensed_feature (feature_name : str ):
228+ """Helper function to check if a specific feature is licensed in 'tidy3d-extras'.
229+
230+ Parameters
231+ ----------
232+ feature_name : str
233+ The name of the feature to check for.
234+
235+ Raises
236+ ------
237+ Tidy3dImportError
238+ If the feature is not available with your license.
239+ """
240+
241+ try :
242+ _check_tidy3d_extras_available ()
243+ except Tidy3dImportError as exc :
244+ raise Tidy3dImportError (f"Failed to load 'tidy3d-extras'. { exc !s} " ) from exc
245+ else :
246+ features = tidy3d_extras ["mod" ].extension ._features ()
247+ if feature_name not in features :
248+ raise Tidy3dImportError (
249+ f"The feature '{ feature_name } ' is not available with your license. "
250+ "Please contact Tidy3D support, or upgrade your license."
251+ )
252+
253+
186254def supports_local_subpixel (fn ):
187255 """When decorating a method, checks that 'tidy3d-extras' is available,
188256 conditioned on 'config.use_local_subpixel'."""
@@ -193,47 +261,14 @@ def _fn(*args: Any, **kwargs: Any):
193261
194262 if preference is False :
195263 tidy3d_extras ["use_local_subpixel" ] = False
196- tidy3d_extras ["mod" ] = None
197264 else :
198- # first try to import the module
199- 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 :
207- 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]'."
212- ) from exc
213-
214- else :
215- version = tidy3d_extras_mod .__version__
216-
217- if version is None :
218- tidy3d_extras ["mod" ] = None
219- tidy3d_extras ["use_local_subpixel" ] = False
220- raise Tidy3dImportError (
221- "The package 'tidy3d-extras' did not initialize correctly, "
222- "likely due to an invalid API key."
223- )
224-
225- if version != __version__ :
226- log .warning (
227- "The package 'tidy3d-extras' is required for this "
228- "operation. The version of 'tidy3d-extras' should match "
229- "the version of 'tidy3d'. You can install the correct "
230- "version using 'pip install tidy3d[extras]'."
231- )
232-
233- features = tidy3d_extras_mod .extension ._features ()
234-
235- tidy3d_extras ["mod" ] = tidy3d_extras_mod
236- tidy3d_extras ["use_local_subpixel" ] = "local_subpixel" in features
265+ try :
266+ _check_tidy3d_extras_available ()
267+ except Tidy3dImportError as exc :
268+ tidy3d_extras ["use_local_subpixel" ] = False
269+ raise Tidy3dImportError (
270+ f"Failed to load 'tidy3d-extras' for local subpixel support. { exc !s} "
271+ ) from exc
237272 else :
238273 features = tidy3d_extras ["mod" ].extension ._features ()
239274 tidy3d_extras ["use_local_subpixel" ] = "local_subpixel" in features
0 commit comments