@@ -35,23 +35,25 @@ def _get_function_source(func):
35
35
return None
36
36
37
37
38
- def _format_args (args ):
39
- """Format function arguments.
38
+ def _format_args_and_kwargs (args , kwargs ):
39
+ """Format function arguments and keyword arguments .
40
40
41
41
Special case for a single parameter: ('hello',) is formatted as ('hello').
42
42
"""
43
43
# use reprlib to limit the length of the output
44
- args_repr = reprlib .repr (args )
45
- if len (args ) == 1 and args_repr .endswith (',)' ):
46
- args_repr = args_repr [:- 2 ] + ')'
47
- return args_repr
44
+ items = []
45
+ if args :
46
+ items .extend (reprlib .repr (arg ) for arg in args )
47
+ if kwargs :
48
+ items .extend ('{}={}' .format (k , reprlib .repr (v ))
49
+ for k , v in kwargs .items ())
50
+ return '(' + ', ' .join (items ) + ')'
48
51
49
52
50
- def _format_callback (func , args , suffix = '' ):
53
+ def _format_callback (func , args , kwargs , suffix = '' ):
51
54
if isinstance (func , functools .partial ):
52
- if args is not None :
53
- suffix = _format_args (args ) + suffix
54
- return _format_callback (func .func , func .args , suffix )
55
+ suffix = _format_args_and_kwargs (args , kwargs ) + suffix
56
+ return _format_callback (func .func , func .args , func .keywords , suffix )
55
57
56
58
if hasattr (func , '__qualname__' ):
57
59
func_repr = getattr (func , '__qualname__' )
@@ -60,14 +62,13 @@ def _format_callback(func, args, suffix=''):
60
62
else :
61
63
func_repr = repr (func )
62
64
63
- if args is not None :
64
- func_repr += _format_args (args )
65
+ func_repr += _format_args_and_kwargs (args , kwargs )
65
66
if suffix :
66
67
func_repr += suffix
67
68
return func_repr
68
69
69
70
def _format_callback_source (func , args ):
70
- func_repr = _format_callback (func , args )
71
+ func_repr = _format_callback (func , args , None )
71
72
source = _get_function_source (func )
72
73
if source :
73
74
func_repr += ' at %s:%s' % source
0 commit comments