python module_error ai_generated true

属性错误:模块 module1 中没有属性 'func'

AttributeError: <module 'module1' from '/path/module1.py'> does not have the attribute 'func'

ID: python/unittest-mock-patch-error

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-03-03首次发现

版本兼容性

版本状态引入弃用备注
3.12 active

根因分析

使用 unittest.mock.patch 时,目标属性不存在或路径错误

English

使用 unittest.mock.patch 时,目标属性不存在或路径错误

generic

解决方案

  1. 90% 成功率
    @patch('module1.func', return_value=42)
  2. 70% 成功率
    @patch('module1.func', create=True)

无效尝试

常见但无效的做法:

  1. 50% 失败

    属性可能确实存在但路径错误

  2. 40% 失败

    如果目标错误,同样会失败