MagicMock is a subclass of Mock with default implementations of most of the magic methods. You can use MagicMock without having to configure the magic methods yourself. If you use the `spec` or `spec_set` arguments then *only* magic methods that exist in the spec will be create
| 2223 | |
| 2224 | |
| 2225 | class MagicMock(MagicMixin, Mock): |
| 2226 | """ |
| 2227 | MagicMock is a subclass of Mock with default implementations |
| 2228 | of most of the magic methods. You can use MagicMock without having to |
| 2229 | configure the magic methods yourself. |
| 2230 | |
| 2231 | If you use the `spec` or `spec_set` arguments then *only* magic |
| 2232 | methods that exist in the spec will be created. |
| 2233 | |
| 2234 | Attributes and the return value of a `MagicMock` will also be `MagicMocks`. |
| 2235 | """ |
| 2236 | def mock_add_spec(self, spec, spec_set=False): |
| 2237 | """Add a spec to a mock. `spec` can either be an object or a |
| 2238 | list of strings. Only attributes on the `spec` can be fetched as |
| 2239 | attributes from the mock. |
| 2240 | |
| 2241 | If `spec_set` is True then only attributes on the spec can be set.""" |
| 2242 | self._mock_add_spec(spec, spec_set) |
| 2243 | self._mock_set_magics() |
| 2244 | |
| 2245 | def reset_mock(self, /, *args, return_value: bool = False, **kwargs): |
| 2246 | if ( |
| 2247 | return_value |
| 2248 | and self._mock_name |
| 2249 | and _is_magic(self._mock_name) |
| 2250 | ): |
| 2251 | # Don't reset return values for magic methods, |
| 2252 | # otherwise `m.__str__` will start |
| 2253 | # to return `MagicMock` instances, instead of `str` instances. |
| 2254 | return_value = False |
| 2255 | super().reset_mock(*args, return_value=return_value, **kwargs) |
| 2256 | |
| 2257 | |
| 2258 | class MagicProxy(Base): |
no outgoing calls
searching dependent graphs…