def __validate_and_wrap_mock_value()

in testslide/strict_mock.py [0:0]


    def __validate_and_wrap_mock_value(self, name: str, value: Any) -> Any:
        if self._template:
            if not self.__template_has_attr(name):
                if not (
                    name.startswith(f"_{type(self).__name__}__")
                    and not name.endswith("__")
                ):
                    raise NonExistentAttribute(self, name)

            self.__validate_attribute_type(name, value)

            if hasattr(self._template, name):
                template_value = getattr(self._template, name)
                if callable(template_value):
                    if not callable(value):
                        raise NonCallableValue(self, name)
                    if self.__dict__["_type_validation"]:
                        signature_validation_wrapper = (
                            testslide.lib._wrap_signature_and_type_validation(
                                value,
                                self._template,
                                name,
                                self.__dict__["_type_validation"],
                            )
                        )

                        if inspect.iscoroutinefunction(template_value):

                            async def awaitable_return_validation_wrapper(
                                *args, **kwargs
                            ):
                                result_awaitable = signature_validation_wrapper(
                                    *args, **kwargs
                                )
                                if not inspect.isawaitable(result_awaitable):
                                    raise NonAwaitableReturn(self, name)

                                return_value = await result_awaitable
                                if not testslide.lib._is_wrapped_for_signature_and_type_validation(
                                    # The original value was already wrapped for type
                                    # validation. Skipping additional validation to
                                    # allow, for example, mock_callable to disable
                                    # validation for a very specific mock call rather
                                    # for the whole StrictMock instance
                                    value
                                ) and not isinstance(
                                    # If the return value is a _BaseRunner then type
                                    # validation, if needed, has already been performed
                                    return_value,
                                    testslide.mock_callable._BaseRunner,
                                ):
                                    testslide.lib._validate_return_type(
                                        template_value,
                                        return_value,
                                        self.__dict__["_caller_frame_info"],
                                    )
                                return return_value

                            callable_value = awaitable_return_validation_wrapper
                        else:

                            def return_validation_wrapper(*args, **kwargs):
                                return_value = signature_validation_wrapper(
                                    *args, **kwargs
                                )
                                if not testslide.lib._is_wrapped_for_signature_and_type_validation(
                                    # The original value was already wrapped for type
                                    # validation. Skipping additional validation to
                                    # allow, for example, mock_callable to disable
                                    # validation for a very specific mock call rather
                                    # for the whole StrictMock instance
                                    value
                                ) and not isinstance(
                                    # If the return value is a _BaseRunner then type
                                    # validation, if needed, has already been performed
                                    return_value,
                                    testslide.mock_callable._BaseRunner,
                                ):
                                    testslide.lib._validate_return_type(
                                        template_value,
                                        return_value,
                                        self.__dict__["_caller_frame_info"],
                                    )
                                return return_value

                            callable_value = return_validation_wrapper
                    else:
                        callable_value = None
                    return _MethodProxy(value=value, callable_value=callable_value)
            else:
                if callable(value):
                    # We don't really need the proxy here, but it serves the
                    # double purpose of swallowing self / cls when needed.
                    return _MethodProxy(value=value)
        else:
            if callable(value):
                # We don't really need the proxy here, but it serves the
                # double purpose of swallowing self / cls when needed.
                return _MethodProxy(value=value)

        return value