{
  "id": "python/unittest-mock-side-effect",
  "signature": "StopIteration: raised by side_effect list exhausted",
  "signature_zh": "StopIteration: 由 side_effect 列表耗尽引发",
  "regex": "StopIteration:\\ raised\\ by\\ side_effect\\ list\\ exhausted",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A mock's side_effect was set to a finite list, and the code under test called the mock more times than the list length, raising StopIteration.",
  "root_cause_type": "generic",
  "root_cause_zh": "mock 的 side_effect 被设置为有限列表，而被测代码调用 mock 的次数超过列表长度，引发 StopIteration。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This hides the real issue: the test exercises more calls than expected, or the list is too short.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Adding one None only delays the problem; a subsequent call raises StopIteration again.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Use a callable for side_effect that handles any number of calls:\n\nm.side_effect = lambda x: x * 2",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "Extend the list to match the expected call count and assert:\n\nm.side_effect = [1, 2, 3]\nrun()\nassert m.call_count == 3",
      "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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-11",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}