# AttributeError: 'MonkeyPatch' object has no attribute 'undo'

- **ID:** `python/pytest-monkeypatch-undo`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 7.0.0 | active | — | — |
| 8.0.0 | active | — | — |

## Workarounds

1. **Remove calls to monkeypatch.undo() and let the fixture revert changes automatically** (95% success)
   ```
   def test_example(monkeypatch):
    monkeypatch.setattr('os.getcwd', lambda: '/tmp')
    # No need to call undo; fixture reverts on teardown
   ```
2. **Use monkeypatch as a context manager for explicit control** (90% success)
   ```
   with monkeypatch.context() as m:
    m.setattr('os.getcwd', lambda: '/tmp')
    # Changes are reverted after the with block
   ```

## Dead Ends

- **Downgrading pytest to version 5.x to restore the undo method** — This is a temporary fix and prevents using newer pytest features. (60% fail)
- **Creating a custom undo function manually** — This duplicates functionality that already exists in the fixture's automatic teardown. (40% fail)
