{
  "id": "python/pytest-mock-magic-method-not-mocked",
  "signature": "TypeError: object of type 'MagicMock' has no len()\nDuring handling of the above exception, another exception occurred:\nTypeError: unsupported operand type(s) for +: 'MagicMock' and 'MagicMock'",
  "signature_zh": "TypeError: 'MagicMock' 类型的对象没有 len()\n在处理上述异常时，又发生了一个异常:\nTypeError: 不支持 'MagicMock' 与 'MagicMock' 之间的 + 运算",
  "regex": "TypeError:\\ object\\ of\\ type\\ 'MagicMock'\\ has\\ no\\ len\\(\\)\\\nDuring\\ handling\\ of\\ the\\ above\\ exception,\\ another\\ exception\\ occurred:\\\nTypeError:\\ unsupported\\ operand\\ type\\(s\\)\\ for\\ \\+:\\ 'MagicMock'\\ and\\ 'MagicMock'",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "A regular mock.Mock was used where a MagicMock is required; only MagicMock supports magic methods like __len__, __add__, and __iter__.",
  "root_cause_type": "generic",
  "root_cause_zh": "在需要 MagicMock 的地方使用了普通的 mock.Mock；只有 MagicMock 支持 __len__、__add__、__iter__ 等魔术方法。",
  "versions": [
    {
      "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": "Works for one magic method but breaks as soon as another (e.g., __iter__) is used; the mock is still not a full MagicMock.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "configure_mock only sets named attributes; it does not add magic method support to a plain Mock.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.96,
      "how": "from unittest.mock import MagicMock, patch\n\nwith patch('myapp.container', new=MagicMock()) as m:\n    m.__len__.return_value = 3\n    assert len(container) == 3",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.93,
      "how": "with patch('myapp.container', autospec=True) as m:\n    m.__len__.return_value = 3\n    assert len(container) == 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.89,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-05-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}