python
type_error
ai_generated
true
TypeError: _mock_children() 收到意外的关键字参数 'autospec' 文件 ".../unittest/mock.py",行 ...,在 __init__ 中 self._mock_children = {}
TypeError: _mock_children() got an unexpected keyword argument 'autospec' File ".../unittest/mock.py", line ..., in __init__ self._mock_children = {}
ID: python/unittest-mock-autospec-call-arg
80%修复率
81%置信度
0证据数
2024-05-28首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
自定义 Mock 子类覆盖了 __init__ 但未将 **kwargs 转发给 super().__init__,导致 autospec/spec_set 等属性被静默丢弃,或在配合 `create_autospec` 时引发错误。
English
A custom Mock subclass overrides __init__ without forwarding **kwargs to super().__init__, so attributes like autospec/spec_set are silently dropped or cause errors when combined with `create_autospec`.
解决方案
-
95% 成功率
from unittest.mock import Mock class MyMock(Mock): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) -
92% 成功率
from unittest.mock import create_autospec mock_client = create_autospec(MyClient, instance=True) mock_client.fetch.return_value = {"ok": True}
无效尝试
常见但无效的做法:
-
75% 失败
Loses signature checking; tests pass with typos in method names, defeating the purpose.
-
80% 失败
The subclass __init__ swallows the kwarg, so autospec is not applied.