From 9a8acd2d3f4da86cd4f1557f1ad0095e2c4c822a Mon Sep 17 00:00:00 2001 From: Sean Evans Date: Tue, 21 Apr 2026 12:04:20 -0400 Subject: [PATCH] Narrow optional import exception handling in package init --- pyisolate/__init__.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/pyisolate/__init__.py b/pyisolate/__init__.py index 4fcf92d..9c314b1 100644 --- a/pyisolate/__init__.py +++ b/pyisolate/__init__.py @@ -20,7 +20,17 @@ try: from .checkpoint import checkpoint, restore -except Exception: # pragma: no cover - optional dependency +except (ModuleNotFoundError, ImportError) as exc: # pragma: no cover - optional dependency + # Trap only dependency-related import failures; let unrelated import-time + # bugs in optional modules propagate so they remain visible to developers. + if ( + isinstance(exc, ModuleNotFoundError) + and exc.name + and exc.name.split(".", 1)[0] != "cryptography" + ): + raise + if isinstance(exc, ImportError) and "cryptography" not in str(exc): + raise def checkpoint(*args, **kwargs): # type: ignore[no-redef] raise ModuleNotFoundError("cryptography is required for checkpoint support") @@ -48,7 +58,17 @@ def restore(*args, **kwargs): # type: ignore[no-redef] try: from .migration import migrate -except Exception: # pragma: no cover - optional dependency +except (ModuleNotFoundError, ImportError) as exc: # pragma: no cover - optional dependency + # Trap only dependency-related import failures; let unrelated import-time + # bugs in optional modules propagate so they remain visible to developers. + if ( + isinstance(exc, ModuleNotFoundError) + and exc.name + and exc.name.split(".", 1)[0] != "cryptography" + ): + raise + if isinstance(exc, ImportError) and "cryptography" not in str(exc): + raise def migrate(*args, **kwargs): # type: ignore[no-redef] raise ModuleNotFoundError("cryptography is required for migration support")