{
  "id": "python/pytest-session-scoped-fixture-with-function-scope",
  "signature": "ScopeMismatch: You tried to access the function scoped fixture 'tmp_path' with a session scoped request object, involved factories",
  "signature_zh": "ScopeMismatch: 你尝试以会话级请求对象访问函数级 fixture 'tmp_path'，涉及的工厂",
  "regex": "ScopeMismatch:\\ You\\ tried\\ to\\ access\\ the\\ function\\ scoped\\ fixture\\ 'tmp_path'\\ with\\ a\\ session\\ scoped\\ request\\ object,\\ involved\\ factories",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "A session-scoped fixture depends on a function-scoped fixture (like tmp_path or monkeypatch), which pytest forbids because the narrower fixture cannot outlive the wider one.",
  "root_cause_type": "generic",
  "root_cause_zh": "会话级 fixture 依赖函数级 fixture（如 tmp_path 或 monkeypatch），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": "Defeats the purpose of sharing expensive setup; tests slow down dramatically.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Raises the same ScopeMismatch at runtime; getfixturevalue does not change scopes.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "@pytest.fixture(scope='session')\ndef data_dir(tmp_path_factory):\n    d = tmp_path_factory.mktemp('data')\n    (d / 'file.txt').write_text('hi')\n    return d",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "@pytest.fixture(scope='session')\ndef cache():\n    return {}\n\n@pytest.fixture\ndef resource(cache, tmp_path):\n    if 'r' not in cache:\n        cache['r'] = build(tmp_path)\n    yield cache['r']",
      "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-05-21",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}