{
  "id": "python/unittest-mock-magic-method-not-supported",
  "signature": "TypeError: unsupported operand type(s) for +: 'Mock' and 'int'",
  "signature_zh": "TypeError: 不支持的操作数类型 +: 'Mock' 和 'int'",
  "regex": "TypeError:\\ unsupported\\ operand\\ type\\(s\\)\\ for\\ \\+:\\ 'Mock'\\ and\\ 'int'",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "A plain unittest.mock.Mock does not implement magic methods like __add__, __len__, __iter__. Use MagicMock (or AsyncMock) when the code under test relies on dunder methods.",
  "root_cause_type": "generic",
  "root_cause_zh": "普通的 unittest.mock.Mock 不实现 __add__、__len__、__iter__ 等魔法方法。当被测代码依赖 dunder 方法时，应使用 MagicMock（或 AsyncMock）。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.10",
      "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": "Plain Mock ignores magic method assignments; you must use MagicMock for them to take effect.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "int(Mock()) raises TypeError because Mock has no __int__; converting does not help.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "from unittest.mock import MagicMock\nm = MagicMock()\nm.__add__.return_value = 42\nassert m + 1 == 42",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "from unittest.mock import AsyncMock\nm = AsyncMock()\nm.__aenter__.return_value = resource",
      "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.89,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-02-14",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}