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
Provide args and kwargs attributes on mock call objects #65468
Comments
The unittest.mock.call object could have args/kwargs attributes to easily access the arguments it was called with. |
args and kwargs property can be introduced on the call object to return the args and kwargs stored in the tuple. Currently wrapping call object with tuple can get a tuple of args and kwargs but a property would be helpful. A first attempt patch on this. Feedback on the API would be helpful. $ ./python.exe
Python 3.8.0a1+ (heads/master:8a03ff2ff4, Feb 9 2019, 10:42:29)
[Clang 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest.mock import Mock
>>> m = Mock()
>>> m(1, a=1)
<Mock name='mock()' id='4325199504'>
>>> m.call_args_list[0]
call(1, a=1)
>>> tuple(m.call_args_list[0]) # wrapping it with tuple can give args and kwargs currently
((1,), {'a': 1})
>>> m.call_args_list[0].args # With patch return args
(1,)
>>> m.call_args_list[0].kwargs # With patch return kwargs
{'a': 1} A simple patch would be as below : ➜ cpython git:(master) ✗ git diff | cat + @Property |
Hey @XTreak, if you're not working on this, can I submit the patch? |
@kakshay, I am not working on it so feel free to pick it up. I stumbled upon this while looking into mock issues and I just posted a patch to gather API feedback. I guess it would be helpful if someone confirms it would be a good addition so that there is less rework. Thanks |
Thanks @XTreak! ➜ cpython git:(fix-issue-21269) ✗ ./python.exe
Python 3.8.0a0 (heads/fix-issue-21269-dirty:2433a2ab70, Feb 10 2019, 14:24:54)
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest.mock import Mock
>>> m = Mock()
>>> m(1, a=23)
<Mock name='mock()' id='4552188368'>
>>> m.call_args
call(1, a=23)
>>> m.call_args.args #after this patch
(1,)
>>> m.call_args.kwargs #after this patch
{'a': 23}
>>> m.call_args_list[0].args #after this patch
(1,)
>>> m.call_args_list[0].kwargs #after this patch
{'a': 23} |
I like this patch, working with calls often feels weird and this change simplify attribute access. |
@XTreak, couldn't we have made |
This feels safer to me with respect to backwards compatibility and also that it might be easier to backport this to mock on GitHub which works with Python 2.7. I have less knowledge on difference between tuple and namedtuple internals so I might be wrong here. |
ping.. |
Thanks @kakshay for the patch. |
From the mock package from PyPI, insead of the standard library unittest.mock. The standalone module backports new unittest.mock features. This helps with Python 3.7, where unittest.mock._Call objects (that is, objects returned by unittest.mock.call) don't have args attributes. (More precisely, they don't know about args attributes or give them any speical meaning, so it just represents accessing an "args" attribute on a hypothetical object returned by the call that the _Call object represents.) See: - https://stackoverflow.com/a/60119333 - https://bugs.python.org/issue21269 - python/cpython#65468 Using the standalone mock module fixes a number of text failures or errors. The tests now all pass on Python 3.7 (other than the ones already marked xfail).
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: