python type_error ai_generated true

ValueError: side_effect must be a callable or iterable, got <class 'int'>

ID: python/unittest-mock-side-effect-exception

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.9 active
3.10 active

Root Cause

在 Mock 中设置 side_effect 时提供了非法的类型,如整数或字符串。

generic

中文

在 Mock 中设置 side_effect 时提供了非法的类型,如整数或字符串。

Workarounds

  1. 100% success
    `mock.side_effect = lambda x: x + 1` 或 `mock.side_effect = [1, 2, 3]` 用于序列返回值。
  2. 95% success
    `mock.side_effect = Exception('错误')` 模拟异常抛出。

Dead Ends

Common approaches that don't work:

  1. 70% fail

    可能无法模拟异常或序列行为。

  2. 50% fail

    列表会被迭代,如果元素不是可调用,仍会出错。