{
  "id": "python/pytest-freezegun-datetime-conflict",
  "signature": "TypeError: can't set attributes of built-in/extension type 'datetime.datetime'",
  "signature_zh": "TypeError: 无法设置内置/扩展类型 'datetime.datetime' 的属性",
  "regex": "TypeError:\\ can't\\ set\\ attributes\\ of\\ built\\-in/extension\\ type\\ 'datetime\\.datetime'",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "freezegun tried to patch datetime.datetime but a C-extension module (e.g. pandas, psycopg2) imported datetime earlier and holds a C-level reference that cannot be monkeypatched.",
  "root_cause_type": "generic",
  "root_cause_zh": "freezegun 尝试 patch datetime.datetime，但某个 C 扩展模块（如 pandas、psycopg2）更早导入了 datetime 并持有 C 层引用，无法被 monkeypatch。",
  "versions": [
    {
      "version": "1.5.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Import order in tests does not control the order inside application code imported at collection time.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Fails for the same reason: the C extension holds a direct reference to the built-in type.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "from unittest.mock import patch\nimport mypkg.service\n\n@patch('mypkg.service.datetime')\ndef test_now(mock_dt):\n    mock_dt.utcnow.return_value = datetime(2024, 1, 1)\n    ...",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "# service takes a clock=datetime.utcnow parameter\ndef test_now():\n    svc = Service(clock=lambda: datetime(2024, 1, 1))\n    assert svc.today() == date(2024, 1, 1)",
      "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.82,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-08-30",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}