# AttributeError: <class 'mypkg.Client'> 没有属性 'endpoint'

- **ID:** `python/pytest-monkeypatch-setattr-class`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

monkeypatch.setattr(Client, 'endpoint', ...) 要求属性已存在，除非传入 raising=False；monkeypatch 有意拒绝创建新属性。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   def test_client(monkeypatch):
    monkeypatch.setattr(Client, 'endpoint', 'http://test', raising=False)
   ```
2. **** (90% 成功率)
   ```
   client = Client()
monkeypatch.setattr(client, 'endpoint', 'http://test')
   ```

## 无效尝试

- **** — Bypasses monkeypatch's automatic undo; the attribute leaks into subsequent tests. (75% 失败率)
- **** — The code under test instantiates the original Client, not the subclass, so the override has no effect. (80% 失败率)
