Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Cannot pickle Mock or MagicMock

未关闭
#457 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
tooling

调研方向

复现 test_pickle.py 中的四个案例,然后检查 cloudpickle/cloudpickle_fast.py 中 Pickler.dump 附近的代码,以及涉及 unittest.mock 的报告失败。对于示例中所示的 plain、simple、specced 和 autospecced mock 案例,cloudpickle.dumps 均成功即表示完成。

由索引模型根据 Issue 内容生成。

描述

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)
主要语言
Python
星标
1.9k
派生
197
平均合并
1 天 10 小时
30 天内合并 PR
1

环境准备

我们还没有检查这个项目的环境配置文件。先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

cloudpipe/cloudpickle 的其他 Issue

查看 cloudpipe/cloudpickle 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。