Closes issue 14634. unittest.mock.create_autospec now supports keyword only arguments.
This commit is contained in:
parent
2cd48738ba
commit
3af125a4aa
|
@ -440,11 +440,16 @@ Classes and functions
|
|||
locals dictionary of the given frame.
|
||||
|
||||
|
||||
.. function:: formatargspec(args[, varargs, varkw, defaults, formatarg, formatvarargs, formatvarkw, formatvalue])
|
||||
.. function:: formatargspec(args[, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations, formatarg, formatvarargs, formatvarkw, formatvalue, formatreturns, formatannotations])
|
||||
|
||||
Format a pretty argument spec from the four values returned by
|
||||
:func:`getargspec`. The format\* arguments are the corresponding optional
|
||||
formatting functions that are called to turn names and values into strings.
|
||||
Format a pretty argument spec from the values returned by
|
||||
:func:`getargspec` or :func:`getfullargspec`.
|
||||
|
||||
The first seven arguments are (``args``, ``varargs``, ``varkw``,
|
||||
``defaults``, ``kwonlyargs``, ``kwonlydefaults``, ``annotations``). The
|
||||
other five arguments are the corresponding optional formatting functions
|
||||
that are called to turn names and values into strings. The last argument
|
||||
is an optional function to format the sequence of arguments.
|
||||
|
||||
|
||||
.. function:: formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue])
|
||||
|
|
|
@ -78,11 +78,15 @@ def _getsignature(func, skipfirst, instance=False):
|
|||
return
|
||||
|
||||
try:
|
||||
regargs, varargs, varkwargs, defaults = inspect.getargspec(func)
|
||||
argspec = inspect.getfullargspec(func)
|
||||
except TypeError:
|
||||
# C function / method, possibly inherited object().__init__
|
||||
return
|
||||
|
||||
# not using annotations
|
||||
regargs, varargs, varkw, defaults, kwonly, kwonlydef, ann = argspec
|
||||
|
||||
|
||||
# instance methods and classmethods need to lose the self argument
|
||||
if getattr(func, '__self__', None) is not None:
|
||||
regargs = regargs[1:]
|
||||
|
@ -90,8 +94,9 @@ def _getsignature(func, skipfirst, instance=False):
|
|||
# this condition and the above one are never both True - why?
|
||||
regargs = regargs[1:]
|
||||
|
||||
signature = inspect.formatargspec(regargs, varargs, varkwargs, defaults,
|
||||
formatvalue=lambda value: "")
|
||||
signature = inspect.formatargspec(
|
||||
regargs, varargs, varkw, defaults,
|
||||
kwonly, kwonlydef, ann, formatvalue=lambda value: "")
|
||||
return signature[1:-1], func
|
||||
|
||||
|
||||
|
|
|
@ -367,7 +367,7 @@ class SpecSignatureTest(unittest.TestCase):
|
|||
|
||||
|
||||
def test_create_autospec_unbound_methods(self):
|
||||
# see issue 128
|
||||
# see mock issue 128
|
||||
# this is expected to fail until the issue is fixed
|
||||
return
|
||||
class Foo(object):
|
||||
|
@ -391,6 +391,19 @@ class SpecSignatureTest(unittest.TestCase):
|
|||
self.assertEqual(m.a, '3')
|
||||
|
||||
|
||||
def test_create_autospec_keyword_only_arguments(self):
|
||||
def foo(a, *, b=None):
|
||||
pass
|
||||
|
||||
m = create_autospec(foo)
|
||||
m(1)
|
||||
m.assert_called_with(1)
|
||||
self.assertRaises(TypeError, m, 1, 2)
|
||||
|
||||
m(2, b=3)
|
||||
m.assert_called_with(2, b=3)
|
||||
|
||||
|
||||
def test_function_as_instance_attribute(self):
|
||||
obj = SomeClass()
|
||||
def f(a):
|
||||
|
|
Loading…
Reference in New Issue