{
  "id": "python/pytest-fixture-scope-conflict",
  "signature": "ScopeMismatch: You tried to access the 'function' scoped fixture 'db' with a 'session' scoped request object",
  "signature_zh": "作用域不匹配：您尝试使用'session'作用域的请求对象访问'function'作用域的fixture 'db'",
  "regex": "ScopeMismatch:\\ You\\ tried\\ to\\ access\\ the\\ 'function'\\ scoped\\ fixture\\ 'db'\\ with\\ a\\ 'session'\\ scoped\\ request\\ object",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "在pytest中，一个较高作用域的fixture（如session）尝试请求一个较低作用域的fixture（如function），导致pytest无法提供正确的实例，因为较低作用域的fixture会在测试结束后销毁。",
  "root_cause_type": "generic",
  "root_cause_zh": "pytest中，高作用域fixture（如session）请求低作用域fixture（如function）时，pytest无法保证低作用域fixture的生命周期，从而报错。",
  "versions": [
    {
      "version": "3.6",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "如果fixture本身需要每个测试独立状态，改为session会导致状态污染，且仍然无法解决根本的依赖关系错误。",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "autouse只影响自动应用，不改变作用域依赖，错误依旧存在。",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "在fixture定义中修改scope参数，例如：@pytest.fixture(scope='session') def db(): ... 改为 @pytest.fixture(scope='function') def db(): ...",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.7,
      "how": "def session_fixture(request):\n    db = request.getfixturevalue('db')\n    return db",
      "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-03-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}