{
  "id": "python/unittest-mock-return-value-override",
  "signature": "AttributeError: Mock object has no attribute 'return_value' when setting return_value on a non-callable mock",
  "signature_zh": "属性错误：当在不可调用的模拟对象上设置return_value时，模拟对象没有'return_value'属性",
  "regex": "AttributeError:\\ Mock\\ object\\ has\\ no\\ attribute\\ 'return_value'\\ when\\ setting\\ return_value\\ on\\ a\\ non\\-callable\\ mock",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Attempting to set return_value on a Mock instance that was created with spec=None and not as a callable, but the mock is being used as if it were a function.",
  "root_cause_type": "generic",
  "root_cause_zh": "尝试在创建时spec=None且非可调用的Mock实例上设置return_value，但该mock被当作函数使用。",
  "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": "Creating a new MagicMock instead of Mock",
      "why_fails": "MagicMock is callable by default, but the underlying issue may be that the mock is not intended to be callable.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "Setting return_value directly without checking if the mock is callable",
      "why_fails": "This will still raise an AttributeError if the mock is not callable.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure the mock is created as a callable by using MagicMock or by passing spec=SomeCallable",
      "success_rate": 0.9,
      "how": "from unittest.mock import MagicMock\nmock = MagicMock()\nmock.return_value = 42\nresult = mock()  # Works\nOr: mock = Mock(spec=lambda: None)\nmock.return_value = 42",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use side_effect instead of return_value for non-callable mocks",
      "success_rate": 0.0,
      "how": "",
      "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.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-01-10",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}