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:
parent
2f13c476ab
commit
41850eec99
|
@ -59,10 +59,17 @@ 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,
|
||||
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,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def get_resolver(urlconf=None):
|
||||
|
|
|
@ -1141,8 +1141,28 @@ 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}/')",
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue