allow a custom repr in format_invocation

This commit is contained in:
Mahmoud Hashemi 2020-07-12 19:01:28 -07:00
parent 5a47d92c54
commit f400b7e298
1 changed files with 6 additions and 3 deletions

View File

@ -298,7 +298,7 @@ class CachedInstancePartial(functools.partial):
partial = CachedInstancePartial partial = CachedInstancePartial
def format_invocation(name='', args=(), kwargs=None): def format_invocation(name='', args=(), kwargs=None, **kw):
"""Given a name, positional arguments, and keyword arguments, format """Given a name, positional arguments, and keyword arguments, format
a basic Python-style function call. a basic Python-style function call.
@ -310,13 +310,16 @@ def format_invocation(name='', args=(), kwargs=None):
kw_func(a=1, b=2) kw_func(a=1, b=2)
""" """
_repr = kw.pop('repr', repr)
if kw:
raise TypeError('unexpected keyword args: %r' % ', '.join(kw.keys()))
kwargs = kwargs or {} kwargs = kwargs or {}
a_text = ', '.join([repr(a) for a in args]) a_text = ', '.join([_repr(a) for a in args])
if isinstance(kwargs, dict): if isinstance(kwargs, dict):
kwarg_items = [(k, kwargs[k]) for k in sorted(kwargs)] kwarg_items = [(k, kwargs[k]) for k in sorted(kwargs)]
else: else:
kwarg_items = kwargs kwarg_items = kwargs
kw_text = ', '.join(['%s=%r' % (k, v) for k, v in kwarg_items]) kw_text = ', '.join(['%s=%s' % (k, _repr(v)) for k, v in kwarg_items])
all_args_text = a_text all_args_text = a_text
if all_args_text and kw_text: if all_args_text and kw_text: