Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3af125a

Browse files
committed
Closes issue 14634. unittest.mock.create_autospec now supports keyword only arguments.
1 parent 2cd4873 commit 3af125a

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

Doc/library/inspect.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,16 @@ Classes and functions
440440
locals dictionary of the given frame.
441441

442442

443-
.. function:: formatargspec(args[, varargs, varkw, defaults, formatarg, formatvarargs, formatvarkw, formatvalue])
443+
.. function:: formatargspec(args[, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations, formatarg, formatvarargs, formatvarkw, formatvalue, formatreturns, formatannotations])
444444

445-
Format a pretty argument spec from the four values returned by
446-
:func:`getargspec`. The format\* arguments are the corresponding optional
447-
formatting functions that are called to turn names and values into strings.
445+
Format a pretty argument spec from the values returned by
446+
:func:`getargspec` or :func:`getfullargspec`.
447+
448+
The first seven arguments are (``args``, ``varargs``, ``varkw``,
449+
``defaults``, ``kwonlyargs``, ``kwonlydefaults``, ``annotations``). The
450+
other five arguments are the corresponding optional formatting functions
451+
that are called to turn names and values into strings. The last argument
452+
is an optional function to format the sequence of arguments.
448453

449454

450455
.. function:: formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue])

Lib/unittest/mock.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,25 @@ def _getsignature(func, skipfirst, instance=False):
7878
return
7979

8080
try:
81-
regargs, varargs, varkwargs, defaults = inspect.getargspec(func)
81+
argspec = inspect.getfullargspec(func)
8282
except TypeError:
8383
# C function / method, possibly inherited object().__init__
8484
return
8585

86+
# not using annotations
87+
regargs, varargs, varkw, defaults, kwonly, kwonlydef, ann = argspec
88+
89+
8690
# instance methods and classmethods need to lose the self argument
8791
if getattr(func, '__self__', None) is not None:
8892
regargs = regargs[1:]
8993
if skipfirst:
9094
# this condition and the above one are never both True - why?
9195
regargs = regargs[1:]
9296

93-
signature = inspect.formatargspec(regargs, varargs, varkwargs, defaults,
94-
formatvalue=lambda value: "")
97+
signature = inspect.formatargspec(
98+
regargs, varargs, varkw, defaults,
99+
kwonly, kwonlydef, ann, formatvalue=lambda value: "")
95100
return signature[1:-1], func
96101

97102

Lib/unittest/test/testmock/testhelpers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def foo(self, foo):
367367

368368

369369
def test_create_autospec_unbound_methods(self):
370-
# see issue 128
370+
# see mock issue 128
371371
# this is expected to fail until the issue is fixed
372372
return
373373
class Foo(object):
@@ -391,6 +391,19 @@ class Foo(object):
391391
self.assertEqual(m.a, '3')
392392

393393

394+
def test_create_autospec_keyword_only_arguments(self):
395+
def foo(a, *, b=None):
396+
pass
397+
398+
m = create_autospec(foo)
399+
m(1)
400+
m.assert_called_with(1)
401+
self.assertRaises(TypeError, m, 1, 2)
402+
403+
m(2, b=3)
404+
m.assert_called_with(2, b=3)
405+
406+
394407
def test_function_as_instance_attribute(self):
395408
obj = SomeClass()
396409
def f(a):

0 commit comments

Comments
 (0)