{
  "id": "python/unittest-mock-spec-missing-attribute",
  "signature": "AttributeError: Mock object has no attribute 'commit'",
  "signature_zh": "AttributeError: Mock 对象没有属性 'commit'",
  "regex": "AttributeError:\\ Mock\\ object\\ has\\ no\\ attribute\\ 'commit'",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "A Mock created with spec= or autospec= restricts attributes to the spec target. The code under test accesses an attribute not present on the spec, raising AttributeError.",
  "root_cause_type": "generic",
  "root_cause_zh": "使用 spec= 或 autospec= 创建的 Mock 将属性限制为 spec 目标上的属性。被测代码访问 spec 上不存在的属性时抛出 AttributeError。",
  "versions": [
    {
      "version": "3.11",
      "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": "Disables typo detection and signature validation; tests pass with wrong method names, hiding real bugs.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Violates spec contract; the mock no longer reflects the real interface and future signature changes go unnoticed.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "from unittest.mock import patch\n@patch('app.repo.Connection', autospec=True)\ndef test_save(mock_conn_cls):\n    inst = mock_conn_cls.return_value\n    inst.commit.return_value = None\n    save()\n    inst.commit.assert_called_once()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "from unittest.mock import create_autospec\nmock_conn = create_autospec(Connection, instance=True)\nmock_conn.commit()  # allowed because Connection.commit exists",
      "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.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-12-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}