Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock cannot autospec functions with keyword-only arguments. #58839

Closed
bitdancer opened this issue Apr 20, 2012 · 4 comments
Closed

Mock cannot autospec functions with keyword-only arguments. #58839

bitdancer opened this issue Apr 20, 2012 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@bitdancer
Copy link
Member

BPO 14634
Nosy @ezio-melotti, @bitdancer, @voidspace

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/voidspace'
closed_at = <Date 2012-04-21.17:22:47.546>
created_at = <Date 2012-04-20.18:08:03.677>
labels = ['type-feature', 'library']
title = 'Mock cannot autospec functions with keyword-only arguments.'
updated_at = <Date 2012-04-21.17:22:47.544>
user = 'https://github.com/bitdancer'

bugs.python.org fields:

activity = <Date 2012-04-21.17:22:47.544>
actor = 'python-dev'
assignee = 'michael.foord'
closed = True
closed_date = <Date 2012-04-21.17:22:47.546>
closer = 'python-dev'
components = ['Library (Lib)']
creation = <Date 2012-04-20.18:08:03.677>
creator = 'r.david.murray'
dependencies = []
files = []
hgrepos = []
issue_num = 14634
keywords = []
message_count = 4.0
messages = ['158864', '158924', '158927', '158928']
nosy_count = 4.0
nosy_names = ['ezio.melotti', 'r.david.murray', 'michael.foord', 'python-dev']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue14634'
versions = ['Python 3.3']

@bitdancer
Copy link
Member Author

The following code:

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

  unittest.mock.create_autospec(foo)

fails with this traceback:

    Traceback (most recent call last):
      File "temp.py", line 6, in <module>
        unittest.mock.create_autospec(foo)
      File "/home/rdmurray/python/p33/Lib/unittest/mock.py", line 2026, in create_autospec
        mock = _set_signature(mock, spec)
      File "/home/rdmurray/python/p33/Lib/unittest/mock.py", line 162, in _set_signature
        result = _getsignature(original, skipfirst, instance)
      File "/home/rdmurray/python/p33/Lib/unittest/mock.py", line 81, in _getsignature
        regargs, varargs, varkwargs, defaults = inspect.getargspec(func)
      File "/home/rdmurray/python/p33/Lib/inspect.py", 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

@bitdancer bitdancer added easy type-feature A feature request or enhancement labels Apr 20, 2012
@ezio-melotti ezio-melotti added the stdlib Python modules in the Lib dir label Apr 20, 2012
@voidspace
Copy link
Contributor

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.

@voidspace voidspace self-assigned this Apr 21, 2012
@voidspace voidspace removed the easy label Apr 21, 2012
@voidspace
Copy link
Contributor

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.

@python-dev
Copy link
Mannequin

python-dev mannequin commented Apr 21, 2012

New changeset 6f478a4aa137 by Michael Foord in branch 'default':
Closes bpo-14634. unittest.mock.create_autospec now supports keyword only arguments.
http://hg.python.org/cpython/rev/6f478a4aa137

@python-dev python-dev mannequin closed this as completed Apr 21, 2012
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants