From bd926a898da3e55a4c20c73dc66ec4445bf82347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=A1zquez=20Acosta?= Date: Wed, 7 Jan 2026 11:43:09 +0100 Subject: [PATCH] Don't supress import errors while auto discovering HTMX modules This can hide import issues inside the user code. --- Makefile | 2 +- src/djhtmx/utils.py | 31 +++++++++++++++++-------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index cf3346c..321bf91 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ SHELL := /bin/bash PROJECT_NAME := djhtmx UV ?= uv -UV_RUN ?= uv run +UV_RUN ?= $(UV) run UV_PYTHON_PREFERENCE ?= only-managed RUN ?= $(UV_RUN) diff --git a/src/djhtmx/utils.py b/src/djhtmx/utils.py index 3454350..29ce7fc 100644 --- a/src/djhtmx/utils.py +++ b/src/djhtmx/utils.py @@ -1,5 +1,5 @@ -import contextlib import importlib +import logging import pkgutil import typing as t from urllib.parse import urlparse @@ -129,17 +129,20 @@ def autodiscover_htmx_modules(): - htmx.py files (like standard autodiscover_modules("htmx")) - All Python files under htmx/ directories in apps (recursively) """ - - def _import_modules_recursively(module_name): - """Recursively import a module and all its submodules.""" - with contextlib.suppress(ImportError): - module = importlib.import_module(module_name) - - # If this is a package, recursively import all modules in it - if hasattr(module, "__path__"): - for _finder, submodule_name, _is_pkg in pkgutil.iter_modules(module.__path__): - _import_modules_recursively(f"{module_name}.{submodule_name}") - for app_config in apps.get_app_configs(): - # Import htmx module and all its submodules recursively - _import_modules_recursively(f"{app_config.name}.htmx") + module_name = f"{app_config.module.__name__}.htmx" + try: + module = importlib.import_module(module_name) + except ImportError: + logger.warning("Could not import %s", module_name) + continue + if hasattr(module, "__path__"): + # If it's a package, recursively walk it importing all modules and packages. + for info in pkgutil.walk_packages(module.__path__, prefix=module_name + "."): + if not info.ispkg: + # `walk_packages` only imports packages, not modules; we need to import them + # all. + importlib.import_module(info.name) + + +logger = logging.getLogger(__name__)