{
  "id": "python/pytest-monkeypatch-undo",
  "signature": "AttributeError: 'MonkeyPatch' object has no attribute 'undo'",
  "signature_zh": "属性错误：'MonkeyPatch'对象没有'undo'属性",
  "regex": "AttributeError:\\ 'MonkeyPatch'\\ object\\ has\\ no\\ attribute\\ 'undo'",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The monkeypatch fixture's undo method was removed in pytest 6.0+, but older code may still try to call it. The correct way to undo patches is to use context managers or let the fixture automatically revert.",
  "root_cause_type": "generic",
  "root_cause_zh": "monkeypatch fixture的undo方法在pytest 6.0+中被移除，但旧代码可能仍尝试调用它。正确的撤销补丁方法是使用上下文管理器或让fixture自动恢复。",
  "versions": [
    {
      "version": "7.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Downgrading pytest to version 5.x to restore the undo method",
      "why_fails": "This is a temporary fix and prevents using newer pytest features.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Creating a custom undo function manually",
      "why_fails": "This duplicates functionality that already exists in the fixture's automatic teardown.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Remove calls to monkeypatch.undo() and let the fixture revert changes automatically",
      "success_rate": 0.95,
      "how": "def test_example(monkeypatch):\n    monkeypatch.setattr('os.getcwd', lambda: '/tmp')\n    # No need to call undo; fixture reverts on teardown",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use monkeypatch as a context manager for explicit control",
      "success_rate": 0.9,
      "how": "with monkeypatch.context() as m:\n    m.setattr('os.getcwd', lambda: '/tmp')\n    # Changes are reverted after the with block",
      "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.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-10-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}