Skip to content

Commit 90e6a80

Browse files
committed
feat: add a try_import helper for optional imports
Add a small try_import(module) helper that returns the imported module or None, so optional dependencies can be handled gracefully at the call site.
1 parent 6c01e04 commit 90e6a80

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

lumen/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,20 @@ def normalize_table_name(name: str) -> str:
493493
return re.sub(r'\W+', '_', name).strip('_').lower()
494494

495495

496+
def try_import(module: str):
497+
"""Attempt to import a module.
498+
499+
Returns
500+
-------
501+
module | None
502+
The imported module if successful, otherwise None.
503+
"""
504+
try:
505+
return importlib.import_module(module)
506+
except ImportError:
507+
return None
508+
509+
496510
def try_import_xarray():
497511
"""Import and return xarray, or None if xarray or xarray-sql is unavailable."""
498512
try:

0 commit comments

Comments
 (0)