#27887 closed Bug (fixed)
TypeError when namespaced urlconf included from non-namespaced one
| Reported by: | Matt Westcott | Owned by: | nobody | 
|---|---|---|---|
| Component: | Core (System checks) | Version: | 1.11 | 
| Severity: | Release blocker | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
The fix for #27874 (on latest stable/1.11.x) causes TypeError: sequence item 0: expected str instance, NoneType found upon running ./manage.py check when a namespaced urlconf is included within a non-namespaced one. To replicate:
project/urls.py:
from django.conf.urls import url, include
from foo import urls as foo_urls
urlpatterns = [
    url(r'x/', include(foo_urls)),
]
foo/urls.py:
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
    url(r'^admin/', admin.site.urls),
]
Traceback:
Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vagrant/libs/django/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/vagrant/libs/django/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vagrant/libs/django/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/vagrant/libs/django/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/vagrant/libs/django/django/core/management/commands/check.py", line 68, in handle
    fail_level=getattr(checks, options['fail_level']),
  File "/home/vagrant/libs/django/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/vagrant/libs/django/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/vagrant/libs/django/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/vagrant/libs/django/django/core/checks/urls.py", line 43, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/home/vagrant/libs/django/django/core/checks/urls.py", line 67, in _load_all_namespaces
    namespaces.extend(_load_all_namespaces(pattern, current))
  File "/home/vagrant/libs/django/django/core/checks/urls.py", line 62, in _load_all_namespaces
    ':'.join(parents + (url.namespace,)) for url in url_patterns
  File "/home/vagrant/libs/django/django/core/checks/urls.py", line 63, in <listcomp>
    if getattr(url, 'namespace', None) is not None
TypeError: sequence item 0: expected str instance, NoneType found
It looks like this is due to a logic error in the line:
If pattern.namespace is defined and equal to None, then getattr(pattern, 'namespace', ()), returns None, which fails on the subsequent join operation.
Change History (5)
comment:1 by , 9 years ago
| Component: | Uncategorized → Core (System checks) | 
|---|---|
| Severity: | Normal → Release blocker | 
| Triage Stage: | Unreviewed → Accepted | 
comment:2 by , 9 years ago
Small nitpick but getattr(pattern, 'namespace', None) is not None should be preferred over hasattr + getattr.
A
RegexURLResolveralways has anamespaceattribute, but it may beNone.RegexURLPatterndoesn't have anamespaceattribute.It should be something like this:
if hasattr(pattern, 'namespace') and pattern.namespace is not None: current = parents + (pattern.namespace,) else: current = parents