# AttributeError: Mock 对象没有属性 'fetch'
# 使用 autospec=True 时
AttributeError: Mock 对象没有属性 'fetch'

- **ID:** `python/unittest-mock-autospec-attribute`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

autospec=True 根据目标对象构建规范。如果目标是模块或类，而 'fetch' 是在 __init__ 中或通过 setattr 动态添加的，autospec 无法识别，mock 缺少该属性。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Autospec the instance instead of the class:

instance = MyClient()
with patch.object(instance, 'fetch', autospec=True) as m:
    ...
   ```
2. **** (88% 成功率)
   ```
   Use create_autospec on an instance with spec_set for stricter checks:

from unittest.mock import create_autospec
m = create_autospec(MyClient(), spec_set=True)
   ```

## 无效尝试

- **** — Loses signature validation and lets typos in call arguments pass silently. (70% 失败率)
- **** — Works but bypasses autospec and does not validate the signature of fetch. (60% 失败率)
