| 1273 | |
| 1274 | |
| 1275 | class _patch(object): |
| 1276 | |
| 1277 | attribute_name = None |
| 1278 | _active_patches = [] |
| 1279 | |
| 1280 | def __init__( |
| 1281 | self, getter, attribute, new, spec, create, |
| 1282 | spec_set, autospec, new_callable, kwargs, *, unsafe=False |
| 1283 | ): |
| 1284 | if new_callable is not None: |
| 1285 | if new is not DEFAULT: |
| 1286 | raise ValueError( |
| 1287 | "Cannot use 'new' and 'new_callable' together" |
| 1288 | ) |
| 1289 | if autospec is not None: |
| 1290 | raise ValueError( |
| 1291 | "Cannot use 'autospec' and 'new_callable' together" |
| 1292 | ) |
| 1293 | if not unsafe: |
| 1294 | _check_spec_arg_typos(kwargs) |
| 1295 | if _is_instance_mock(spec): |
| 1296 | raise InvalidSpecError( |
| 1297 | f'Cannot spec attr {attribute!r} as the spec ' |
| 1298 | f'has already been mocked out. [spec={spec!r}]') |
| 1299 | if _is_instance_mock(spec_set): |
| 1300 | raise InvalidSpecError( |
| 1301 | f'Cannot spec attr {attribute!r} as the spec_set ' |
| 1302 | f'target has already been mocked out. [spec_set={spec_set!r}]') |
| 1303 | |
| 1304 | self.getter = getter |
| 1305 | self.attribute = attribute |
| 1306 | self.new = new |
| 1307 | self.new_callable = new_callable |
| 1308 | self.spec = spec |
| 1309 | self.create = create |
| 1310 | self.has_local = False |
| 1311 | self.spec_set = spec_set |
| 1312 | self.autospec = autospec |
| 1313 | self.kwargs = kwargs |
| 1314 | self.additional_patchers = [] |
| 1315 | |
| 1316 | |
| 1317 | def copy(self): |
| 1318 | patcher = _patch( |
| 1319 | self.getter, self.attribute, self.new, self.spec, |
| 1320 | self.create, self.spec_set, |
| 1321 | self.autospec, self.new_callable, self.kwargs |
| 1322 | ) |
| 1323 | patcher.attribute_name = self.attribute_name |
| 1324 | patcher.additional_patchers = [ |
| 1325 | p.copy() for p in self.additional_patchers |
| 1326 | ] |
| 1327 | return patcher |
| 1328 | |
| 1329 | |
| 1330 | def __call__(self, func): |
| 1331 | if isinstance(func, type): |
| 1332 | return self.decorate_class(func) |
no outgoing calls
no test coverage detected