From f9023e8f5f5122919b01c1e48ccda44d2540323d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 26 Feb 2026 17:23:37 +0000 Subject: [PATCH] Refactor imghdr compatibility hack to a dedicated compat module Moved the imghdr compatibility shim from `src/seedpass/core/password_generation.py` to a new `src/seedpass/core/compat.py` module. This improves code cleanliness and separates concerns by centralizing Python version compatibility logic. The shim ensures that `imghdr` is available in `sys.modules` (using `utils.imghdr_stub` as a fallback for Python 3.13+) so that dependencies like `pgpy` can function correctly. Verified the change by: - Confirming `compat.py` correctly populates `sys.modules["imghdr"]`. - Verifying `password_generation.py` imports correctly and triggers the shim. - Ensuring syntax correctness via `py_compile`. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- src/seedpass/core/compat.py | 11 +++++++++++ src/seedpass/core/password_generation.py | 10 +--------- 2 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 src/seedpass/core/compat.py diff --git a/src/seedpass/core/compat.py b/src/seedpass/core/compat.py new file mode 100644 index 00000000..6f1b560b --- /dev/null +++ b/src/seedpass/core/compat.py @@ -0,0 +1,11 @@ +"""Compatibility layer for different Python versions.""" + +import sys + +# Ensure the ``imghdr`` module is available for ``pgpy`` on Python 3.13+ +try: # pragma: no cover - only executed on Python >= 3.13 + import imghdr # type: ignore +except ModuleNotFoundError: # pragma: no cover - fallback for removed module + from utils import imghdr_stub + + sys.modules.setdefault("imghdr", imghdr_stub) diff --git a/src/seedpass/core/password_generation.py b/src/seedpass/core/password_generation.py index 60d6a8de..08d69869 100644 --- a/src/seedpass/core/password_generation.py +++ b/src/seedpass/core/password_generation.py @@ -35,15 +35,7 @@ from cryptography.hazmat.primitives.asymmetric import ed25519 from cryptography.hazmat.backends import default_backend from bip_utils import Bip39SeedGenerator -# Ensure the ``imghdr`` module is available for ``pgpy`` on Python 3.13+ -try: # pragma: no cover - only executed on Python >= 3.13 - import imghdr # type: ignore -except ModuleNotFoundError: # pragma: no cover - fallback for removed module - from utils import imghdr_stub as imghdr # type: ignore - import sys - - sys.modules.setdefault("imghdr", imghdr) - +from . import compat # noqa: F401 from local_bip85.bip85 import BIP85 from constants import (