[Django] #31166: Fix context for ImproperlyConfigured error

120 views
Skip to first unread message

Django

unread,
Jan 14, 2020, 9:21:58 AM1/14/20
to django-...@googlegroups.com
#31166: Fix context for ImproperlyConfigured error
---------------------------------------+-----------------------------
Reporter: Ram Rachum | Owner: Shai Berger
Type: Bug | Status: assigned
Component: Core (URLs) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+-----------------------------
The patch is ready here: https://github.com/django/django/pull/12263

This will make it have a text of "this exception is the direct result of"
instead of "During handling of the above exception, another exception
occurred"

This is more accurate for the case of this exception.

If this change will be merged, there are a couple more place in this
module where it can be added.

When it happened to me a few weeks ago, it was due to a circular import,
which I then fixed. Now of course, when I tried to reproduce the circular
import to show you, I couldn't. But I could achieve the same thing by
renaming `urlpatterns` to `xurlpatterns`, so it wouldn't be available.
This is the error I get:

{{{
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\site-
packages\django\urls\resolvers.py", line 590, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files\Python38\lib\threading.py", line 932, in
_bootstrap_inner
self.run()
File "C:\Program Files\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Program Files\Python38\lib\site-
packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Program Files\Python38\lib\site-
packages\django\core\management\commands\runserver.py", line 117, in
inner_run
self.check(display_num_errors=True)
File "C:\Program Files\Python38\lib\site-
packages\django\core\management\base.py", line 392, in check
all_issues = self._run_checks(
File "C:\Program Files\Python38\lib\site-
packages\django\core\management\base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Program Files\Python38\lib\site-
packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Program Files\Python38\lib\site-
packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Program Files\Python38\lib\site-
packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Program Files\Python38\lib\site-
packages\django\urls\resolvers.py", line 408, in check
messages.extend(check_resolver(pattern))
File "C:\Program Files\Python38\lib\site-
packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Program Files\Python38\lib\site-
packages\django\urls\resolvers.py", line 407, in check
for pattern in self.url_patterns:
File "C:\Program Files\Python38\lib\site-
packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Program Files\Python38\lib\site-
packages\django\urls\resolvers.py", line 597, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module
'barb.urls' from
'C:\\Users\\Administrator\\Desktop\\foof\\barb\\urls.py'>' does not appear
to have any patterns in it. If you see valid patterns in the file then the
issue is probably caused by a circular import.
}}}

The bad part is `During handling of the above exception, another exception
occurred`.

After my fix, it looks like this:

{{{
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\site-
packages\django\urls\resolvers.py", line 590, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Program Files\Python38\lib\threading.py", line 932, in
_bootstrap_inner
self.run()
File "C:\Program Files\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Program Files\Python38\lib\site-
packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Program Files\Python38\lib\site-
packages\django\core\management\commands\runserver.py", line 117, in
inner_run
self.check(display_num_errors=True)
File "C:\Program Files\Python38\lib\site-
packages\django\core\management\base.py", line 392, in check
all_issues = self._run_checks(
File "C:\Program Files\Python38\lib\site-
packages\django\core\management\base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Program Files\Python38\lib\site-
packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Program Files\Python38\lib\site-
packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Program Files\Python38\lib\site-
packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Program Files\Python38\lib\site-
packages\django\urls\resolvers.py", line 408, in check
messages.extend(check_resolver(pattern))
File "C:\Program Files\Python38\lib\site-
packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Program Files\Python38\lib\site-
packages\django\urls\resolvers.py", line 407, in check
for pattern in self.url_patterns:
File "C:\Program Files\Python38\lib\site-
packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Program Files\Python38\lib\site-
packages\django\urls\resolvers.py", line 597, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from
type_error # Ram hack, remove
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module
'barb.urls' from
'C:\\Users\\Administrator\\Desktop\\foof\\barb\\urls.py'>' does not appear
to have any patterns in it. If you see valid patterns in the file then the
issue is probably caused by a circular import.
}}}

Now the text is corrected to `The above exception was the direct cause of
the following exception`

--
Ticket URL: <https://code.djangoproject.com/ticket/31166>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 15, 2020, 2:54:44 AM1/15/20
to django-...@googlegroups.com
#31166: Provide context for ImproperlyConfigured exceptions in URL resolver.
-------------------------------------+-------------------------------------

Reporter: Ram Rachum | Owner: Shai
Type: | Berger
Cleanup/optimization | Status: assigned

Component: Core (URLs) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

Hi Ram. Thanks for the report.

I'm inclined to accept this as a cleanup.

> there are a couple more place in this module where it can be added.

OK. Can we do the whole module at once?

Notes on the PR:

* Commit messages should be in the past tense, so "Provided... "
* I'll have a think about how/whether we need tests for this change. (🤔)

--
Ticket URL: <https://code.djangoproject.com/ticket/31166#comment:1>

Django

unread,
Jan 15, 2020, 3:11:49 AM1/15/20
to django-...@googlegroups.com
#31166: Provide context for ImproperlyConfigured exceptions in URL resolver.
-------------------------------------+-------------------------------------
Reporter: Ram Rachum | Owner: Ram
Type: | Rachum
Cleanup/optimization | Status: assigned

Component: Core (URLs) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* owner: Shai Berger => Ram Rachum


--
Ticket URL: <https://code.djangoproject.com/ticket/31166#comment:2>

Django

unread,
Jan 17, 2020, 5:37:05 AM1/17/20
to django-...@googlegroups.com
#31166: Provide context for ImproperlyConfigured exceptions in URL resolver.
-------------------------------------+-------------------------------------
Reporter: Ram Rachum | Owner: Ram
Type: | Rachum
Cleanup/optimization | Status: assigned

Component: Core (URLs) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Shai Berger):

* cc: Shai Berger (added)
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/31166#comment:3>

Django

unread,
Jan 17, 2020, 6:21:07 AM1/17/20
to django-...@googlegroups.com
#31166: Provide context for ImproperlyConfigured exceptions in URL resolver.
-------------------------------------+-------------------------------------
Reporter: Ram Rachum | Owner: Ram
Type: | Rachum
Cleanup/optimization | Status: closed

Component: Core (URLs) | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"bf3e8227a902ca0828c9a66130064a23d5f75163" bf3e822]:
{{{
#!CommitTicketReference repository=""
revision="bf3e8227a902ca0828c9a66130064a23d5f75163"
Fixed #31166 -- Used "raise from" when raising ImproperlyConfigured
exceptions in django.urls.resolvers.

This change sets the __cause__ attribute to raised exceptions and makes
small cleanups in error messages.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31166#comment:4>

Reply all
Reply to author
Forward
0 new messages