{
  "id": "python/unittest-mock-spec-attribute",
  "signature": "AttributeError: Mock object has no attribute 'foo' when using spec=MyClass",
  "signature_zh": "属性错误：使用 spec=MyClass 时，模拟对象没有属性 'foo'",
  "regex": "AttributeError:\\ Mock\\ object\\ has\\ no\\ attribute\\ 'foo'\\ when\\ using\\ spec=MyClass",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Creating a mock with spec=MyClass restricts the mock to only have attributes that exist on MyClass, and accessing a non-existent attribute raises an AttributeError.",
  "root_cause_type": "generic",
  "root_cause_zh": "使用 spec=MyClass 创建模拟对象会限制模拟仅具有 MyClass 上存在的属性，访问不存在的属性会引发 AttributeError。",
  "versions": [
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Using spec_set=True instead of spec still raises the same error for non-existent attributes.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Removing the spec parameter entirely allows any attribute but may hide API mismatches.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.8,
      "how": "Add the missing attribute to MyClass or use a mock with spec=MyClass and then add the attribute dynamically: mock.foo = MagicMock()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "Use autospec=True instead of spec to automatically create a mock that matches the class's interface.",
      "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.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-10-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}