python
config_error
ai_generated
true
ScopeMismatch: You tried to access the function scoped fixture monkeypatch with a session scoped request object, involved factories
ID: python/pytest-monkeypatch-scope-session
80%Fix Rate
88%Confidence
0Evidence
2024-04-26First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
Root Cause
A session-scoped fixture requested monkeypatch (which is function-scoped), violating pytest's scope ordering rules.
generic中文
会话作用域的 fixture 请求了 monkeypatch(函数作用域),违反了 pytest 的作用域排序规则。
Workarounds
-
95% success
@pytest.fixture(scope="session") def config(): return {"url": "http://x"} @pytest.fixture def patched(config, monkeypatch): monkeypatch.setitem(config, "url", "http://test") return config -
90% success
@pytest.fixture(scope="session") def patched(): with pytest.MonkeyPatch.context() as mp: mp.setenv("X", "1") yield
Dead Ends
Common approaches that don't work:
-
60% fail
May break expensive setup assumptions and hides the real scope design issue.
-
90% fail
Does not change scope mismatch behavior.
-
85% fail
Markers do not alter fixture scope rules.