Call the pytest_assertrepr_compare hook and prepare the result. This uses the first result from the hook and then ensures the following: * Overly verbose explanations are truncated unless configured otherwise (eg. if running in verbose mode). * Embedded new
(op, left: object, right: object)
| 124 | ihook = item.ihook |
| 125 | |
| 126 | def callbinrepr(op, left: object, right: object) -> Optional[str]: |
| 127 | """Call the pytest_assertrepr_compare hook and prepare the result. |
| 128 | |
| 129 | This uses the first result from the hook and then ensures the |
| 130 | following: |
| 131 | * Overly verbose explanations are truncated unless configured otherwise |
| 132 | (eg. if running in verbose mode). |
| 133 | * Embedded newlines are escaped to help util.format_explanation() |
| 134 | later. |
| 135 | * If the rewrite mode is used embedded %-characters are replaced |
| 136 | to protect later % formatting. |
| 137 | |
| 138 | The result can be formatted by util.format_explanation() for |
| 139 | pretty printing. |
| 140 | """ |
| 141 | hook_result = ihook.pytest_assertrepr_compare( |
| 142 | config=item.config, op=op, left=left, right=right |
| 143 | ) |
| 144 | for new_expl in hook_result: |
| 145 | if new_expl: |
| 146 | new_expl = truncate.truncate_if_required(new_expl, item) |
| 147 | new_expl = [line.replace("\n", "\\n") for line in new_expl] |
| 148 | res = "\n~".join(new_expl) |
| 149 | if item.config.getvalue("assertmode") == "rewrite": |
| 150 | res = res.replace("%", "%%") |
| 151 | return res |
| 152 | return None |
| 153 | |
| 154 | saved_assert_hooks = util._reprcompare, util._assertion_pass |
| 155 | util._reprcompare = callbinrepr |