{
  "id": "python/fastapi-dependency-override-error",
  "signature": "ValueError: Dependency 'get_db' has already been overridden",
  "signature_zh": "值错误：依赖项'get_db'已被覆盖",
  "regex": "ValueError:\\ Dependency\\ 'get_db'\\ has\\ already\\ been\\ overridden",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "在测试中多次覆盖同一个FastAPI依赖项",
  "root_cause_type": "generic",
  "root_cause_zh": "在测试中多次覆盖同一个FastAPI依赖项",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "尝试使用不同的依赖项名称",
      "why_fails": "原始依赖项仍被覆盖，新名称不匹配",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "在每次测试前重置依赖项",
      "why_fails": "app.dependency_overrides是全局状态，重置可能导致测试间污染",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "使用context manager管理依赖项覆盖",
      "success_rate": 0.9,
      "how": "with TestClient(app) as client:\n    app.dependency_overrides[get_db] = override_get_db\n    response = client.get('/')",
      "condition": "",
      "sources": []
    },
    {
      "action": "在测试fixture中清理覆盖",
      "success_rate": 0.85,
      "how": "@pytest.fixture\ndef client():\n    app.dependency_overrides = {}\n    yield TestClient(app)",
      "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.81,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-30",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}