question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a helper to copy args

See original GitHub issue

mock will store references in call_args and call_args_list (see https://docs.python.org/3/library/unittest.mock-examples.html#coping-with-mutable-arguments).

I think that pytest-mock could provide a helper based on the example from the doc:

from copy import deepcopy
>>> class CopyingMock(MagicMock):
...     def __call__(self, *args, **kwargs):
...         args = deepcopy(args)
...         kwargs = deepcopy(kwargs)
...         return super(CopyingMock, self).__call__(*args, **kwargs)

The following works (by extending the pytest-mock mocker fixture).

@pytest.fixture
def mocker(mocker):
    from copy import deepcopy
    from mock import MagicMock

    class CopyingMock(MagicMock):
        def __call__(self, *args, **kwargs):
            args = deepcopy(args)
            kwargs = deepcopy(kwargs)
            return super(CopyingMock, self).__call__(*args, **kwargs)

    mocker.CopyingMock = CopyingMock
    return mocker
    patched = mocker.patch('foo.bar', new_callable=mocker.CopyingMock)

Not sure if that’s helpful enough and/or if there could be a mocker_copy fixture instead, which would handle new_callable not only for patch().

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
nicoddemuscommented, Mar 23, 2016

OK, thanks for the suggestion and patch. 😄

0reactions
blueyedcommented, Mar 22, 2016

@nicoddemus Nope.

Feel free to take my patch from above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Provide a helper to copy args - - Bountysource
Provide a helper to copy args ... mock will store references in call_args and call_args_list (see https://docs.python.org/3/library/unittest.mock-examples.html# ...
Read more >
Ember.js: Passing arguments to render helper - Stack Overflow
I looked around on the internet, and it looks like the {{render}} helper used to allow for an optional options hash (which is...
Read more >
Function.prototype.bind() - JavaScript - MDN Web Docs - Mozilla
A copy of the given function with the specified this value, and initial arguments (if provided). Description. The bind() function creates a new ......
Read more >
Caller argument expression - C# 10.0 draft specifications
Allow developers to capture the expressions passed to a method, to enable better ... but can be made use of through a helper...
Read more >
How to use sys.argv in Python with examples - KnowledgeHut
Command Line argument is a way of managing the script or program externally by providing the script name and the input parameters from ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found