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

Cannot pickle Mock or MagicMock #457

Open
LoveIsGrief opened this issue Nov 1, 2021 · 0 comments
Open

Cannot pickle Mock or MagicMock #457

LoveIsGrief opened this issue Nov 1, 2021 · 0 comments

Comments

@LoveIsGrief
Copy link

Scenario

It happens that in tests somewhere deep in the object hierarchies and structures something has to be mocked. This especially happens with external services and their clients.

Problem

If cloudpickle happens to stumble upon a mock, it fails and it's very difficult to track down which object and which attribute contains the mocked object (especially when there are a bunch of mocks).

Example

requirements.txt
cloudpickle==2.0.0

test_pickle.py

import unittest
from unittest.mock import MagicMock

import cloudpickle


class ToMock:
    a: str

    def __init__(self):
        self.something = "something"


class Mockable:

    def __init__(self, to_mock=None):
        self.not_mocked = "this is not mocked"
        self.to_be_mocked = to_mock or ToMock()


class TestPickling(unittest.TestCase):

    def test_no_mocking(self):
        self.assertIsInstance(cloudpickle.dumps(Mockable()), bytes)

    def test_simple_mock(self):
        self.assertIsInstance(cloudpickle.dumps(Mockable(MagicMock())), bytes)

    def test_specced_mock(self):
        self.assertIsInstance(cloudpickle.dumps(Mockable(MagicMock(spec=ToMock))), bytes)

    def test_autospecced_mock(self):
        self.assertIsInstance(cloudpickle.dumps(Mockable(unittest.mock.create_autospec(ToMock))), bytes)


if __name__ == '__main__':
    unittest.main()

output

python test_pickle.py
Testing started at 19:05 ...
Launching unittests with arguments python -m unittest test_pickle.TestPickling in /home/bumbum/PycharmProjects/pythonProject


Error
Traceback (most recent call last):
  File "/home/bumbum/PycharmProjects/pythonProject/test_pickle.py", line 33, in test_autospecced_mock
    self.assertIsInstance(cloudpickle.dumps(Mockable(unittest.mock.create_autospec(ToMock))), bytes)
  File "/home/python/cloudpickle-magic-mock/lib/python3.9/site-packages/cloudpickle/cloudpickle_fast.py", line 73, in dumps
    cp.dump(obj)
  File "/home/python/cloudpickle-magic-mock/lib/python3.9/site-packages/cloudpickle/cloudpickle_fast.py", line 602, in dump
    return Pickler.dump(self, obj)
_pickle.PicklingError: args[0] from __newobj__ args has the wrong class


Error
Traceback (most recent call last):
  File "/home/python/cloudpickle-magic-mock/lib/python3.9/site-packages/cloudpickle/cloudpickle_fast.py", line 602, in dump
    return Pickler.dump(self, obj)
  File "/usr/lib64/python3.9/unittest/mock.py", line 2500, in __call__
    return _Call((self._mock_name, args, kwargs), name=name, parent=self)
  File "/usr/lib64/python3.9/unittest/mock.py", line 2404, in __new__
    _len = len(value)
RecursionError: maximum recursion depth exceeded while calling a Python object

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/bumbum/PycharmProjects/pythonProject/test_pickle.py", line 27, in test_simple_mock
    self.assertIsInstance(cloudpickle.dumps(Mockable(MagicMock())), bytes)
  File "/home/python/cloudpickle-magic-mock/lib/python3.9/site-packages/cloudpickle/cloudpickle_fast.py", line 73, in dumps
    cp.dump(obj)
  File "/home/python/cloudpickle-magic-mock/lib/python3.9/site-packages/cloudpickle/cloudpickle_fast.py", line 609, in dump
    raise pickle.PicklingError(msg) from e
_pickle.PicklingError: Could not pickle object as excessively deep recursion required.


Error
Traceback (most recent call last):
  File "/home/bumbum/PycharmProjects/pythonProject/test_pickle.py", line 30, in test_specced_mock
    self.assertIsInstance(cloudpickle.dumps(Mockable(MagicMock(spec=ToMock))), bytes)
  File "/home/python/cloudpickle-magic-mock/lib/python3.9/site-packages/cloudpickle/cloudpickle_fast.py", line 73, in dumps
    cp.dump(obj)
  File "/home/python/cloudpickle-magic-mock/lib/python3.9/site-packages/cloudpickle/cloudpickle_fast.py", line 602, in dump
    return Pickler.dump(self, obj)
_pickle.PicklingError: args[0] from __newobj__ args has the wrong class







Ran 4 tests in 0.043s

FAILED (errors=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant