{
  "id": "python/pytest-mock-fixture-scope-mismatch",
  "signature": "ScopeMismatch: You tried to access the function scoped fixture mocker with a module scoped request object, involved factories:\nmodule:  conftest.py:15: def module_mock(request)\nfunction:  pytest_mock/plugin.py:40: def mocker()",
  "signature_zh": "ScopeMismatch: 你试图在模块作用域的 request 对象中访问函数作用域的 fixture mocker，涉及的工厂：\nmodule:  conftest.py:15: def module_mock(request)\nfunction:  pytest_mock/plugin.py:40: def mocker()",
  "regex": "ScopeMismatch:\\ You\\ tried\\ to\\ access\\ the\\ function\\ scoped\\ fixture\\ mocker\\ with\\ a\\ module\\ scoped\\ request\\ object,\\ involved\\ factories:\\\nmodule:\\ \\ conftest\\.py:15:\\ def\\ module_mock\\(request\\)\\\nfunction:\\ \\ pytest_mock/plugin\\.py:40:\\ def\\ mocker\\(\\)",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "A module- or session-scoped fixture depends on a function-scoped fixture (mocker), which pytest forbids because the broader-scoped fixture outlives the narrower one.",
  "root_cause_type": "generic",
  "root_cause_zh": "模块或会话作用域的 fixture 依赖函数作用域的 fixture（mocker），pytest 禁止此做法，因为作用域更广的 fixture 生命周期长于更窄的 fixture。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "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": "mocker is provided by pytest-mock and its scope is not configurable via pytest.ini; attempting to override it in conftest raises the same ScopeMismatch.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Decorator patching applies per-function, not per-module; it doesn't solve the need for a module-scoped mock and can conflict with other patches.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "@pytest.fixture\ndef module_mock(mocker):  # function scope\n    return mocker.patch('myapp.client')\n\n# or use @pytest.fixture(scope='function')",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "@pytest.fixture(scope='module')\ndef module_mock():\n    from unittest.mock import patch\n    with patch('myapp.client') as m:\n        yield m",
      "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.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-04-30",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}