{
  "id": "python/unittest-mock-patch-object-not-found",
  "signature": "AttributeError: <class 'myapp.models.User'> does not have the attribute 'save'",
  "signature_zh": "AttributeError: <class 'myapp.models.User'> 没有属性 'save'",
  "regex": "AttributeError:\\ <class\\ 'myapp\\.models\\.User'>\\ does\\ not\\ have\\ the\\ attribute\\ 'save'",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "patch.object is used to patch an attribute that does not exist on the target object. This can happen if the method is inherited or defined dynamically.",
  "root_cause_type": "generic",
  "root_cause_zh": "patch.object 用于修补目标对象上不存在的属性。如果方法是继承的或动态定义的，就会发生这种情况。",
  "versions": [
    {
      "version": "3.8",
      "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": "patch also requires the attribute to exist unless create=True is used.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This changes production code to satisfy a test, which is not ideal.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "with patch.object(User, 'save', create=True) as mock_save:\n    ...",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "If save is inherited from BaseModel, patch that: patch.object(BaseModel, 'save')",
      "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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-20",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}