MCPcopy Index your code
hub / github.com/python/cpython / _get_child_mock

Method _get_child_mock

Lib/unittest/mock.py:1062–1097  ·  view source on GitHub ↗

Create the child mocks for attributes and return value. By default child mocks will be the same type as the parent. Subclasses of Mock may want to override this to customize the way child mocks are made. For non-callable mocks the callable variant will be used (rathe

(self, /, **kw)

Source from the content-addressed store, hash-verified

1060
1061
1062 def _get_child_mock(self, /, **kw):
1063 """Create the child mocks for attributes and return value.
1064 By default child mocks will be the same type as the parent.
1065 Subclasses of Mock may want to override this to customize the way
1066 child mocks are made.
1067
1068 For non-callable mocks the callable variant will be used (rather than
1069 any custom subclass)."""
1070 if self._mock_sealed:
1071 attribute = f".{kw['name']}" if "name" in kw else "()"
1072 mock_name = self._extract_mock_name() + attribute
1073 raise AttributeError(mock_name)
1074
1075 _new_name = kw.get("_new_name")
1076 if _new_name in self.__dict__['_spec_asyncs']:
1077 return AsyncMock(**kw)
1078
1079 _type = type(self)
1080 if issubclass(_type, MagicMock) and _new_name in _async_method_magics:
1081 # Any asynchronous magic becomes an AsyncMock
1082 klass = AsyncMock
1083 elif issubclass(_type, AsyncMockMixin):
1084 if (_new_name in _all_sync_magics or
1085 self._mock_methods and _new_name in self._mock_methods):
1086 # Any synchronous method on AsyncMock becomes a MagicMock
1087 klass = MagicMock
1088 else:
1089 klass = AsyncMock
1090 elif not issubclass(_type, CallableMixin):
1091 if issubclass(_type, NonCallableMagicMock):
1092 klass = MagicMock
1093 elif issubclass(_type, NonCallableMock):
1094 klass = Mock
1095 else:
1096 klass = _type.__mro__[1]
1097 return klass(**kw)
1098
1099
1100 def _calls_repr(self):

Callers 2

__get_return_valueMethod · 0.95
__getattr__Method · 0.95

Calls 3

_extract_mock_nameMethod · 0.95
AsyncMockClass · 0.85
getMethod · 0.45

Tested by

no test coverage detected