A non-callable version of `Mock`
| 433 | |
| 434 | |
| 435 | class NonCallableMock(Base): |
| 436 | """A non-callable version of `Mock`""" |
| 437 | |
| 438 | # Store a mutex as a class attribute in order to protect concurrent access |
| 439 | # to mock attributes. Using a class attribute allows all NonCallableMock |
| 440 | # instances to share the mutex for simplicity. |
| 441 | # |
| 442 | # See https://github.com/python/cpython/issues/98624 for why this is |
| 443 | # necessary. |
| 444 | _lock = RLock() |
| 445 | |
| 446 | def __new__( |
| 447 | cls, spec=None, wraps=None, name=None, spec_set=None, |
| 448 | parent=None, _spec_state=None, _new_name='', _new_parent=None, |
| 449 | _spec_as_instance=False, _eat_self=None, unsafe=False, **kwargs |
| 450 | ): |
| 451 | # every instance has its own class |
| 452 | # so we can create magic methods on the |
| 453 | # class without stomping on other mocks |
| 454 | bases = (cls,) |
| 455 | if not issubclass(cls, AsyncMockMixin): |
| 456 | # Check if spec is an async object or function |
| 457 | spec_arg = spec_set or spec |
| 458 | if spec_arg is not None and _is_async_obj(spec_arg): |
| 459 | bases = (AsyncMockMixin, cls) |
| 460 | new = type(cls.__name__, bases, {'__doc__': cls.__doc__}) |
| 461 | instance = _safe_super(NonCallableMock, cls).__new__(new) |
| 462 | return instance |
| 463 | |
| 464 | |
| 465 | def __init__( |
| 466 | self, spec=None, wraps=None, name=None, spec_set=None, |
| 467 | parent=None, _spec_state=None, _new_name='', _new_parent=None, |
| 468 | _spec_as_instance=False, _eat_self=None, unsafe=False, **kwargs |
| 469 | ): |
| 470 | if _new_parent is None: |
| 471 | _new_parent = parent |
| 472 | |
| 473 | __dict__ = self.__dict__ |
| 474 | __dict__['_mock_parent'] = parent |
| 475 | __dict__['_mock_name'] = name |
| 476 | __dict__['_mock_new_name'] = _new_name |
| 477 | __dict__['_mock_new_parent'] = _new_parent |
| 478 | __dict__['_mock_sealed'] = False |
| 479 | |
| 480 | if spec_set is not None: |
| 481 | spec = spec_set |
| 482 | spec_set = True |
| 483 | if _eat_self is None: |
| 484 | _eat_self = parent is not None |
| 485 | |
| 486 | self._mock_add_spec(spec, spec_set, _spec_as_instance, _eat_self) |
| 487 | |
| 488 | __dict__['_mock_children'] = {} |
| 489 | __dict__['_mock_wraps'] = wraps |
| 490 | __dict__['_mock_delegate'] = None |
| 491 | |
| 492 | __dict__['_mock_called'] = False |
searching dependent graphs…