Prepare a string containing the call info to `func`, e.g. argument names/values/expressions.
(func, args, kwargs, call_argument_expressions, target_args)
| 3798 | |
| 3799 | |
| 3800 | def _parse_call_info(func, args, kwargs, call_argument_expressions, target_args): |
| 3801 | """ |
| 3802 | Prepare a string containing the call info to `func`, e.g. argument names/values/expressions. |
| 3803 | """ |
| 3804 | signature = inspect.signature(func) |
| 3805 | signature_names = [param.name for param_name, param in signature.parameters.items()] |
| 3806 | |
| 3807 | # called as `self.method_name()` or `xxx.method_name()`. |
| 3808 | if len(args) == len(call_argument_expressions["positional_args"]) + 1: |
| 3809 | # We simply add "self" as the expression despite it might not be the actual argument name. |
| 3810 | # (This part is very unlikely what a user would be interest to know) |
| 3811 | call_argument_expressions["positional_args"] = ["self"] + call_argument_expressions["positional_args"] |
| 3812 | |
| 3813 | param_position_mapping = {param_name: idx for idx, param_name in enumerate(signature_names)} |
| 3814 | |
| 3815 | arg_info = {} |
| 3816 | for arg_name in target_args: |
| 3817 | if arg_name in kwargs: |
| 3818 | arg_value = kwargs[arg_name] |
| 3819 | arg_expr = call_argument_expressions["keyword_args"][arg_name] |
| 3820 | else: |
| 3821 | arg_pos = param_position_mapping[arg_name] |
| 3822 | arg_value = args[arg_pos] |
| 3823 | arg_expr = call_argument_expressions["positional_args"][arg_pos] |
| 3824 | |
| 3825 | arg_value_str = _format_py_obj(arg_value) |
| 3826 | arg_info[arg_name] = {"arg_expr": arg_expr, "arg_value_str": arg_value_str} |
| 3827 | |
| 3828 | info = "" |
| 3829 | for arg_name in arg_info: |
| 3830 | arg_expr, arg_value_str = arg_info[arg_name]["arg_expr"], arg_info[arg_name]["arg_value_str"] |
| 3831 | info += f"{'-' * 80}\n\nargument name: `{arg_name}`\nargument expression: `{arg_expr}`\n\nargument value:\n\n{arg_value_str}\n\n" |
| 3832 | |
| 3833 | # remove the trailing \n\n |
| 3834 | info = info[:-2] |
| 3835 | |
| 3836 | return info |
| 3837 | |
| 3838 | |
| 3839 | def patch_testing_methods_to_collect_info(): |
nothing calls this directly
no test coverage detected