This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Mock cannot autospec functions with keyword-only arguments.
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.3
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: ezio.melotti, michael.foord, python-dev, r.david.murray
Priority: normal Keywords:

Created on 2012-04-20 18:08 by r.david.murray, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg158864 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-04-20 18:08
The following code:

  def foo(a, *, b=None):


fails with this traceback:

    Traceback (most recent call last):
      File "", line 6, in <module>
      File "/home/rdmurray/python/p33/Lib/unittest/", line 2026, in create_autospec
        mock = _set_signature(mock, spec)
      File "/home/rdmurray/python/p33/Lib/unittest/", line 162, in _set_signature
        result = _getsignature(original, skipfirst, instance)
      File "/home/rdmurray/python/p33/Lib/unittest/", line 81, in _getsignature
        regargs, varargs, varkwargs, defaults = inspect.getargspec(func)
      File "/home/rdmurray/python/p33/Lib/", line 808, in getargspec
        raise ValueError("Function has keyword-only arguments or annotations"
    ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them
msg158924 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-04-21 16:49
This is non-trivial to fix. Although inspect.getfullargspec can be used, which does support keyword only arguments, inspect.formatargspec *doesn't* support them. (mock.create_autospec uses these to rebuild a compatible signature for generated mocks.)

The easiest route to fixing would be to extend formatargspec to optionally take extra arguments for kwonlyargs and kwonlydefaults.
msg158927 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-04-21 17:10
Hmmm... looks like formatargspec does support these features but they aren't documented. If it works out I'll update the docs for inspect.formatargspec too.
msg158928 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-04-21 17:22
New changeset 6f478a4aa137 by Michael Foord in branch 'default':
Closes issue 14634. unittest.mock.create_autospec now supports keyword only arguments.
Date User Action Args
2022-04-11 14:57:29adminsetgithub: 58839
2012-04-21 17:22:47python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg158928

resolution: fixed
stage: test needed -> resolved
2012-04-21 17:10:56michael.foordsetmessages: + msg158927
2012-04-21 17:03:30michael.foordsetkeywords: - easy
2012-04-21 16:49:52michael.foordsetassignee: michael.foord
messages: + msg158924
2012-04-20 18:10:38ezio.melottisetnosy: + ezio.melotti

components: + Library (Lib)
stage: test needed
2012-04-20 18:08:03r.david.murraycreate