{
  "id": "python/unittest-mock-autospec-attribute",
  "signature": "AttributeError: Mock object has no attribute 'fetch'\n# with autospec=True\nAttributeError: Mock object has no attribute 'fetch'",
  "signature_zh": "AttributeError: Mock 对象没有属性 'fetch'\n# 使用 autospec=True 时\nAttributeError: Mock 对象没有属性 'fetch'",
  "regex": "AttributeError:\\ Mock\\ object\\ has\\ no\\ attribute\\ 'fetch'\\\n\\#\\ with\\ autospec=True\\\nAttributeError:\\ Mock\\ object\\ has\\ no\\ attribute\\ 'fetch'",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "autospec=True builds a spec from the target object. If the target is a module or class where 'fetch' is defined dynamically (e.g., added in __init__ or via setattr), autospec cannot see it and the mock lacks the attribute.",
  "root_cause_type": "generic",
  "root_cause_zh": "autospec=True 根据目标对象构建规范。如果目标是模块或类，而 'fetch' 是在 __init__ 中或通过 setattr 动态添加的，autospec 无法识别，mock 缺少该属性。",
  "versions": [
    {
      "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": "Loses signature validation and lets typos in call arguments pass silently.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Works but bypasses autospec and does not validate the signature of fetch.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Autospec the instance instead of the class:\n\ninstance = MyClient()\nwith patch.object(instance, 'fetch', autospec=True) as m:\n    ...",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "Use create_autospec on an instance with spec_set for stricter checks:\n\nfrom unittest.mock import create_autospec\nm = create_autospec(MyClient(), spec_set=True)",
      "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-10-28",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}