diff options
Diffstat (limited to 'Lib')
| -rw-r--r-- | Lib/unittest/mock.py | 11 | ||||
| -rw-r--r-- | Lib/unittest/test/testmock/testhelpers.py | 15 |
2 files changed, 22 insertions, 4 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 8378b0ff95..8f592ab764 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 4c43f87dbc..7a7145ecf6 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): |
