{
  "id": "python/unittest-mock-side-effect-iteration",
  "signature": "StopIteration: Caught StopIteration from side_effect; consider using side_effect with an iterable",
  "signature_zh": "停止迭代：从 side_effect 捕获到 StopIteration；考虑使用可迭代对象作为 side_effect",
  "regex": "StopIteration:\\ Caught\\ StopIteration\\ from\\ side_effect;\\ consider\\ using\\ side_effect\\ with\\ an\\ iterable",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Setting mock.side_effect to an iterable (e.g., a generator) that raises StopIteration when exhausted, and the test calls the mock more times than the number of items in the iterable.",
  "root_cause_type": "generic",
  "root_cause_zh": "将 mock.side_effect 设置为可迭代对象（如生成器），当迭代耗尽时引发 StopIteration，而测试调用模拟的次数超过了可迭代对象中的项数。",
  "versions": [
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Using a list instead of a generator for side_effect still raises StopIteration if the list is exhausted, but the error may be more predictable.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Setting side_effect to a function that returns a default value after exhaustion may mask the fact that the mock is called too many times.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.85,
      "how": "Ensure the number of mock calls matches the length of the side_effect iterable, or use side_effect with a function that handles arbitrary calls.",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.8,
      "how": "Set side_effect to a list with enough items to cover all expected calls, and use a sentinel value for extra calls if needed.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-11-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}