{
  "id": "python/unittest-mock-autospec-attribute-error",
  "signature": "AttributeError: Mock object has no attribute 'non_existent_method' when using autospec",
  "signature_zh": "属性错误：使用 autospec 时，模拟对象没有属性 'non_existent_method'",
  "regex": "AttributeError:\\ Mock\\ object\\ has\\ no\\ attribute\\ 'non_existent_method'\\ when\\ using\\ autospec",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Using autospec=True in patch or create_autospec restricts the mock to only have attributes that exist on the original object, and accessing a non-existent attribute raises an AttributeError.",
  "root_cause_type": "generic",
  "root_cause_zh": "在 patch 或 create_autospec 中使用 autospec=True 会将模拟限制为仅具有原始对象上存在的属性，访问不存在的属性会引发 AttributeError。",
  "versions": [
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Disabling autospec (autospec=False) may allow the test to pass but hides potential mismatches between the mock and the real API.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Adding the missing attribute to the original object may not be feasible if it's a third-party library.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.85,
      "how": "Ensure the attribute exists on the original object, or use autospec with a spec object that includes the attribute: create_autospec(MyClass, instance=True)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.75,
      "how": "Use spec_set instead of autospec to allow additional attributes while still checking existing ones.",
      "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": "2026-07-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}