Fixed #32572 -- Improved ResolverMatch.__repr__().

When a partial function was passed as the view, the __repr__() would
show the `func` argument as `functools.partial` which isn't very
helpful, especially as it doesn't reveal the underlying function or
arguments provided.
This commit is contained in:
Nick Pope 2021-03-19 14:44:03 +00:00 committed by Mariusz Felisiak
parent 2f13c476ab
commit 41850eec99
2 changed files with 32 additions and 5 deletions

View File

@ -59,9 +59,16 @@ class ResolverMatch:
return (self.func, self.args, self.kwargs)[index]
def __repr__(self):
return "ResolverMatch(func=%s, args=%s, kwargs=%s, url_name=%s, app_names=%s, namespaces=%s, route=%s)" % (
self._func_path, self.args, self.kwargs, self.url_name,
self.app_names, self.namespaces, self.route,
if isinstance(self.func, functools.partial):
func = repr(self.func)
else:
func = self._func_path
return (
'ResolverMatch(func=%s, args=%r, kwargs=%r, url_name=%r, '
'app_names=%r, namespaces=%r, route=%r)' % (
func, self.args, self.kwargs, self.url_name,
self.app_names, self.namespaces, self.route,
)
)

View File

@ -1141,10 +1141,30 @@ class ResolverMatchTests(SimpleTestCase):
self.assertEqual(
repr(resolve('/no_kwargs/42/37/')),
"ResolverMatch(func=urlpatterns_reverse.views.empty_view, "
"args=('42', '37'), kwargs={}, url_name=no-kwargs, app_names=[], "
"namespaces=[], route=^no_kwargs/([0-9]+)/([0-9]+)/$)",
"args=('42', '37'), kwargs={}, url_name='no-kwargs', app_names=[], "
"namespaces=[], route='^no_kwargs/([0-9]+)/([0-9]+)/$')",
)
@override_settings(ROOT_URLCONF='urlpatterns_reverse.urls')
def test_repr_functools_partial(self):
tests = [
('partial', 'template.html'),
('partial_nested', 'nested_partial.html'),
('partial_wrapped', 'template.html'),
]
for name, template_name in tests:
with self.subTest(name=name):
func = (
f"functools.partial({views.empty_view!r}, "
f"template_name='{template_name}')"
)
self.assertEqual(
repr(resolve(f'/{name}/')),
f"ResolverMatch(func={func}, args=(), kwargs={{}}, "
f"url_name='{name}', app_names=[], namespaces=[], "
f"route='{name}/')",
)
@override_settings(ROOT_URLCONF='urlpatterns_reverse.erroneous_urls')
class ErroneousViewTests(SimpleTestCase):