[Python-checkins] cpython: Closes issue 14634. unittest.mock.create_autospec now supports keyword only

michael.foord python-checkins at python.org
Sat Apr 21 19:22:46 CEST 2012


http://hg.python.org/cpython/rev/6f478a4aa137
changeset:   76447:6f478a4aa137
user:        Michael Foord <michael at voidspace.org.uk>
date:        Sat Apr 21 18:22:28 2012 +0100
summary:
  Closes issue 14634. unittest.mock.create_autospec now supports keyword only arguments.

files:
  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
--- a/Doc/library/inspect.rst
+++ b/Doc/library/inspect.rst
@@ -440,11 +440,16 @@
    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
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -78,11 +78,15 @@
             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 @@
         # 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
--- a/Lib/unittest/test/testmock/testhelpers.py
+++ b/Lib/unittest/test/testmock/testhelpers.py
@@ -367,7 +367,7 @@
 
 
     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 @@
         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):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list