{
  "id": "python/pytest-monkeypatch-env-not-restored",
  "signature": "KeyError: 'API_KEY'\nDuring handling of the above exception, another exception occurred:\nAssertionError: assert 'test-key' == os.environ['API_KEY']",
  "signature_zh": "KeyError: 'API_KEY'\n处理上述异常时又出现另一个异常:\nAssertionError: assert 'test-key' == os.environ['API_KEY']",
  "regex": "KeyError:\\ 'API_KEY'\\\nDuring\\ handling\\ of\\ the\\ above\\ exception,\\ another\\ exception\\ occurred:\\\nAssertionError:\\ assert\\ 'test\\-key'\\ ==\\ os\\.environ\\['API_KEY'\\]",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "Test mutated os.environ directly (os.environ['API_KEY']='...') instead of using monkeypatch.setenv. Changes leak between tests, and teardown in another test deletes the key.",
  "root_cause_type": "generic",
  "root_cause_zh": "测试直接修改了 os.environ（os.environ['API_KEY']='...'）而非使用 monkeypatch.setenv。改动在测试间泄漏，另一个测试的 teardown 删除了该键。",
  "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": "Doesn't restore the original value; subsequent tests see a missing variable.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Pollutes the whole session and hides order-dependent bugs.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "Use monkeypatch:\n  def test_x(monkeypatch):\n      monkeypatch.setenv('API_KEY', 'test-key')\n      ...\nmonkeypatch auto-restores on teardown.",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Use monkeypatch.delenv with raising=False for removal tests:\n  monkeypatch.delenv('API_KEY', raising=False)",
      "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.9,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-05-19",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}