Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions marimo/_runtime/packages/import_error_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ def extract_missing_module_from_cause_chain(
e.g., via `raise ImportError("helpful message") from err`
"""
current: None | BaseException = error
# Cache ModuleNotFoundError and str for attribute check optimization
ModuleNotFoundErrorType = ModuleNotFoundError
while current is not None:
if (
isinstance(current, ModuleNotFoundError)
and hasattr(current, "name")
and current.name
):
return current.name
# Use type() identity check instead of isinstance() for performance,
# given that ModuleNotFoundError is unlikely to be subclassed and Python's
# type identity check is much cheaper than isinstance() when traversing deep cause chains.
if type(current) is ModuleNotFoundErrorType:
# Access .name directly and check truthiness;
# ModuleNotFoundError always has 'name' attribute.
name = current.name # type: ignore[attr-defined]
if name:
return name
current = current.__cause__
return None

Expand Down