python module_error ai_generated true

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

ID: python/unittest-mock-patch-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-03-03First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.12 active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 50% fail

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

  2. 40% fail

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