Asked 4 months ago by OrbitalScout128
Should Django add explicit import tests to improve error messages for misconfigured settings?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 4 months ago by OrbitalScout128
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm often frustrated when a mistyped class string leads to an ImportError
, with a stack trace that doesn’t clearly indicate where the incorrect string came from. In the case of configuration-loading errors, particularly with lazy loading, debugging becomes even more challenging. For example, when the ROOT_URLCONF
has a typo, the error trace looks like this:
PYTHONTraceback (most recent call last): File "/Users/rtpg/proj/django/django/core/checks/urls.py", line 136, in check_custom_error_handlers handler = resolver.resolve_error_handler(status_code) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/rtpg/proj/django/django/urls/resolvers.py", line 743, in resolve_error_handler callback = getattr(self.urlconf_module, "handler%s" % view_type, None) ^^^^^^^^^^^^^^^^^^^ File "/Users/rtpg/proj/django/django/utils/functional.py", line 47, in __get__ res = instance.__dict__[self.name] = self.func(instance) ^^^^^^^^^^^^^^^^^^^ File "/Users/rtpg/proj/django/django/urls/resolvers.py", line 711, in urlconf_module return import_module(self.urlconf_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/rtpg/.local/share/uv/python/cpython-3.12.6-macos-aarch64-none/lib/python3.12/importlib/__init__.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1387, in _gcd_import File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 1387, in _gcd_import File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked ModuleNotFoundError: No module named 'foo' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/rtpg/proj/saasfarm/backend/manage.py", line 22, in <module> main() File "/Users/rtpg/proj/saasfarm/backend/manage.py", line 18, in main execute_from_command_line(sys.argv) File "/Users/rtpg/proj/django/django/core/management/__init__.py", line 442, in execute_from_command_line utility.execute() File "/Users/rtpg/proj/django/django/core/management/__init__.py", line 436, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/rtpg/proj/django/django/core/management/base.py", line 413, in run_from_argv self.execute(*args, **cmd_options) File "/Users/rtpg/proj/django/django/core/management/base.py", line 454, in execute self.check() File "/Users/rtpg/proj/django/django/core/management/base.py", line 486, in check all_issues = checks.run_checks( ^^^^^^^^^^^^^^^^^^ File "/Users/rtpg/proj/django/django/core/checks/registry.py", line 89, in run_checks new_errors = check(app_configs=app_configs, databases=databases) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/rtpg/proj/django/django/core/checks/urls.py", line 138, in check_custom_error_handlers path = getattr(resolver.urlconf_module, "handler%s" % status_code) ^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/rtpg/proj/django/django/utils/functional.py", line 47, in __get__ res = instance.__dict__[self.name] = self.func(instance) ^^^^^^^^^^^^^^^^^^^ File "/Users/rtpg/proj/django/django/urls/resolvers.py", line 711, in urlconf_module return import_module(self.urlconf_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/rtpg/.local/share/uv/python/cpython-3.12.6-macos-aarch64-none/lib/python3.12/importlib/__init__.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1387, in _gcd_import File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 1387, in _gcd_import File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked ModuleNotFoundError: No module named 'foo'
The attached patch shows some ad-hoc fixes and raises the question: would it make sense to add an explicit check that attempts all relevant imports for common settings? This could provide a clearer, more direct error message, but at the cost of some extra CPU overhead every time a setting is mistyped. I'm curious to know if anyone else considers this a worthwhile improvement or if it's just a non-issue.
Comments 0
•Answers 0
•Views 113
No comments yet.