Skip to content

Commit 152748e

Browse files
Use find_spec instead of try/except for htmx module discovery
The previous try/except ImportError approach would silently catch import errors from within the htmx module itself, masking real bugs. Now we use find_spec to check if the module exists before importing, allowing any ImportError from within the module to propagate properly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 008a63d commit 152748e

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/djhtmx/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib
2+
import importlib.util
23
import pkgutil
34
import typing as t
45
from urllib.parse import urlparse
@@ -130,10 +131,10 @@ def autodiscover_htmx_modules():
130131
"""
131132
for app_config in apps.get_app_configs():
132133
module_name = f"{app_config.module.__name__}.htmx"
133-
try:
134-
module = importlib.import_module(module_name)
135-
except ImportError:
134+
spec = importlib.util.find_spec(module_name)
135+
if spec is None:
136136
continue
137+
module = importlib.import_module(module_name)
137138
if hasattr(module, "__path__"):
138139
# If it's a package, recursively walk it importing all modules and packages.
139140
for info in pkgutil.walk_packages(module.__path__, prefix=module_name + "."):

0 commit comments

Comments
 (0)