# AttributeError: 'NoneType' 对象没有属性 'get'

- **ID:** `python/pytest-patch-stopall-early`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

测试在 `finally` 块中调用 `patch.stopall()`，这会移除同一类中 `setUp` 为后续测试设置的 patch。后续测试看到的是真实对象（或 None），而不是 mock。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.10 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Use context managers or decorators so patches are scoped automatically:

@patch('mymod.dep')
def test_x(mock_dep):
    ...
   ```
2. **** (90% 成功率)
   ```
   Use `addCleanup` to stop patches at the right time:

self.addCleanup(patcher.stop)
   ```

## 无效尝试

- **** — tearDown runs after each test, so the same problem persists across tests in the class. (80% 失败率)
- **** — Hard to detect failure reliably; patches still leak between tests. (70% 失败率)
