Create a mock object using another object as a spec. Attributes on the mock will use the corresponding attribute on the `spec` object as their spec. Functions or methods being mocked will have their arguments checked to check that they are called with the correct signature.
(spec, spec_set=False, instance=False, _parent=None,
_name=None, *, unsafe=False, **kwargs)
| 2653 | |
| 2654 | |
| 2655 | def create_autospec(spec, spec_set=False, instance=False, _parent=None, |
| 2656 | _name=None, *, unsafe=False, **kwargs): |
| 2657 | """Create a mock object using another object as a spec. Attributes on the |
| 2658 | mock will use the corresponding attribute on the `spec` object as their |
| 2659 | spec. |
| 2660 | |
| 2661 | Functions or methods being mocked will have their arguments checked |
| 2662 | to check that they are called with the correct signature. |
| 2663 | |
| 2664 | If `spec_set` is True then attempting to set attributes that don't exist |
| 2665 | on the spec object will raise an `AttributeError`. |
| 2666 | |
| 2667 | If a class is used as a spec then the return value of the mock (the |
| 2668 | instance of the class) will have the same spec. You can use a class as the |
| 2669 | spec for an instance object by passing `instance=True`. The returned mock |
| 2670 | will only be callable if instances of the mock are callable. |
| 2671 | |
| 2672 | `create_autospec` will raise a `RuntimeError` if passed some common |
| 2673 | misspellings of the arguments autospec and spec_set. Pass the argument |
| 2674 | `unsafe` with the value True to disable that check. |
| 2675 | |
| 2676 | `create_autospec` also takes arbitrary keyword arguments that are passed to |
| 2677 | the constructor of the created mock.""" |
| 2678 | if _is_list(spec): |
| 2679 | # can't pass a list instance to the mock constructor as it will be |
| 2680 | # interpreted as a list of strings |
| 2681 | spec = type(spec) |
| 2682 | |
| 2683 | is_type = isinstance(spec, type) |
| 2684 | if _is_instance_mock(spec): |
| 2685 | raise InvalidSpecError(f'Cannot autospec a Mock object. ' |
| 2686 | f'[object={spec!r}]') |
| 2687 | is_async_func = _is_async_func(spec) |
| 2688 | _kwargs = {'spec': spec} |
| 2689 | if spec_set: |
| 2690 | _kwargs = {'spec_set': spec} |
| 2691 | elif spec is None: |
| 2692 | # None we mock with a normal mock without a spec |
| 2693 | _kwargs = {} |
| 2694 | if _kwargs and instance: |
| 2695 | _kwargs['_spec_as_instance'] = True |
| 2696 | if not unsafe: |
| 2697 | _check_spec_arg_typos(kwargs) |
| 2698 | |
| 2699 | _kwargs.update(kwargs) |
| 2700 | |
| 2701 | Klass = MagicMock |
| 2702 | if inspect.isdatadescriptor(spec): |
| 2703 | # descriptors don't have a spec |
| 2704 | # because we don't know what type they return |
| 2705 | _kwargs = {} |
| 2706 | elif is_async_func: |
| 2707 | if instance: |
| 2708 | raise RuntimeError("Instance can not be True when create_autospec " |
| 2709 | "is mocking an async function") |
| 2710 | Klass = AsyncMock |
| 2711 | elif not _callable(spec): |
| 2712 | Klass = NonCallableMagicMock |