python
type_error
ai_generated
true
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%Fix Rate
81%Confidence
0Evidence
2024-05-28First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
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`.
generic中文
自定义 Mock 子类覆盖了 __init__ 但未将 **kwargs 转发给 super().__init__,导致 autospec/spec_set 等属性被静默丢弃,或在配合 `create_autospec` 时引发错误。
Workarounds
-
95% success
from unittest.mock import Mock class MyMock(Mock): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) -
92% success
from unittest.mock import create_autospec mock_client = create_autospec(MyClient, instance=True) mock_client.fetch.return_value = {"ok": True}
Dead Ends
Common approaches that don't work:
-
75% fail
Loses signature checking; tests pass with typos in method names, defeating the purpose.
-
80% fail
The subclass __init__ swallows the kwarg, so autospec is not applied.