{
  "id": "python/pytest-scope-mismatch-session-in-function",
  "signature": "ScopeMismatch: You tried to access the function scoped fixture 'tmp_path' with a session scoped request object",
  "signature_zh": "ScopeMismatch: 您试图用 session 作用域的 request 对象访问 function 作用域的 fixture 'tmp_path'",
  "regex": "ScopeMismatch:\\ You\\ tried\\ to\\ access\\ the\\ function\\ scoped\\ fixture\\ 'tmp_path'\\ with\\ a\\ session\\ scoped\\ request\\ object",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "A session/module-scoped fixture depends on a narrower-scoped fixture. pytest forbids a wider scope from consuming a narrower one because the narrower fixture would be torn down before the wider one.",
  "root_cause_type": "generic",
  "root_cause_zh": "session/module 作用域的 fixture 依赖了更窄作用域的 fixture。pytest 禁止宽作用域消费窄作用域，因为窄 fixture 会在宽 fixture 之前被拆除。",
  "versions": [
    {
      "version": "7.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "autouse doesn't change scope; the mismatch remains and pytest still raises ScopeMismatch.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The lambda still references the same fixture object; pytest resolves dependencies before invocation.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.93,
      "how": "@pytest.fixture(scope='session')\ndef tmp_root(tmp_path_factory):\n    return tmp_path_factory.mktemp('root')\n\n@pytest.fixture(scope='session')\ndef settings(tmp_root):\n    return load(tmp_root)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "@pytest.fixture(scope='session')\ndef db(tmp_path_factory):\n    path = tmp_path_factory.mktemp('db')\n    return init_db(path)",
      "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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-04-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}