Guard a block of code to suppress ImportErrors
A context manager to temporarily suppress ImportErrors. Use this to attempt imports without failing immediately on missing modules.
Example:
with optional_import_block():
import some_module
import some_other_module
Source code in autogen/import_utils.py
| @contextmanager
def optional_import_block() -> Generator[Result, None, None]:
"""Guard a block of code to suppress ImportErrors
A context manager to temporarily suppress ImportErrors.
Use this to attempt imports without failing immediately on missing modules.
Example:
```python
with optional_import_block():
import some_module
import some_other_module
```
"""
result = Result()
try:
yield result
result._failed = False
except ImportError as e:
# Ignore ImportErrors during this context
logger.debug(f"Ignoring ImportError: {e}")
result._failed = True
|