From 3af125a4aaef5b34d8abf9d50958077473706954 Mon Sep 17 00:00:00 2001 From: Michael Foord Date: Sat, 21 Apr 2012 18:22:28 +0100 Subject: Closes issue 14634. unittest.mock.create_autospec now supports keyword only arguments. --- Doc/library/inspect.rst | 13 +++++++++---- Lib/unittest/mock.py | 11 ++++++++--- Lib/unittest/test/testmock/testhelpers.py | 15 ++++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index ac6ae99..90b7220 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -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]) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 8378b0f..8f592ab 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -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 diff --git a/Lib/unittest/test/testmock/testhelpers.py b/Lib/unittest/test/testmock/testhelpers.py index 4c43f87..7a7145e 100644 --- a/Lib/unittest/test/testmock/testhelpers.py +++ b/Lib/unittest/test/testmock/testhelpers.py @@ -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): -- cgit v0.12