{
  "id": "python/unittest-mock-side-effect-exhausted",
  "signature": "StopIteration: \n# raised inside test where side_effect list was consumed",
  "signature_zh": "StopIteration: \n# 在测试中 side_effect 列表耗尽后引发",
  "regex": "StopIteration:\\ \\\n\\#\\ raised\\ inside\\ test\\ where\\ side_effect\\ list\\ was\\ consumed",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A Mock's side_effect was set to a finite list, but the code under test calls the mock more times than the list has entries; the extra call raises StopIteration.",
  "root_cause_type": "generic",
  "root_cause_zh": "Mock 的 side_effect 被设置为有限列表，但被测代码调用 mock 的次数超过列表长度；多余的调用引发 StopIteration。",
  "versions": [
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.11",
      "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": "Fixes the current call count but breaks again when the code loops over a different-sized input.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Loses the ability to return different values per call, which is often the whole point of the test.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "mock.get.side_effect = lambda key: {'a': 1, 'b': 2}.get(key, 0)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "from itertools import chain, repeat\nmock.get.side_effect = chain([1, 2], repeat(0))",
      "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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-05-14",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}