{
  "id": "python/unittest-mock-side-effect-type-error",
  "signature": "TypeError: 'int' object is not callable when using mock.side_effect with an iterable",
  "signature_zh": "类型错误：当使用可迭代对象作为mock.side_effect时，'int'对象不可调用",
  "regex": "TypeError:\\ 'int'\\ object\\ is\\ not\\ callable\\ when\\ using\\ mock\\.side_effect\\ with\\ an\\ iterable",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "The side_effect attribute of a mock is set to an iterable (e.g., a list) that contains non-callable elements, but the mock is called as a function, causing a TypeError when trying to call the element.",
  "root_cause_type": "generic",
  "root_cause_zh": "mock的side_effect属性被设置为包含不可调用元素的可迭代对象（如列表），但mock作为函数被调用，导致尝试调用元素时出现TypeError。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Setting side_effect to a single value instead of an iterable",
      "why_fails": "This changes the behavior; the mock will return that value for every call, which may not match the expected test scenario.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using return_value instead of side_effect",
      "why_fails": "This only sets a single return value for all calls, not allowing different responses per call.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure all elements in the side_effect iterable are callable (e.g., functions or exceptions)",
      "success_rate": 0.9,
      "how": "mock.side_effect = [lambda: 1, lambda: 2, ValueError('error')]\nNow each call to mock() will call the respective lambda or raise the exception.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use side_effect with a function that generates values dynamically",
      "success_rate": 0.85,
      "how": "def side_effect_func():\n    return 42\nmock.side_effect = side_effect_func\nThis ensures the mock is callable.",
      "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.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-10-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}