Return the explanation for the diff between text. Unless --verbose is used this will skip leading and trailing characters which are identical to keep the diff minimal.
(left: str, right: str, verbose: int = 0)
| 213 | |
| 214 | |
| 215 | def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]: |
| 216 | """Return the explanation for the diff between text. |
| 217 | |
| 218 | Unless --verbose is used this will skip leading and trailing |
| 219 | characters which are identical to keep the diff minimal. |
| 220 | """ |
| 221 | from difflib import ndiff |
| 222 | |
| 223 | explanation: List[str] = [] |
| 224 | |
| 225 | if verbose < 1: |
| 226 | i = 0 # just in case left or right has zero length |
| 227 | for i in range(min(len(left), len(right))): |
| 228 | if left[i] != right[i]: |
| 229 | break |
| 230 | if i > 42: |
| 231 | i -= 10 # Provide some context |
| 232 | explanation = [ |
| 233 | "Skipping %s identical leading characters in diff, use -v to show" % i |
| 234 | ] |
| 235 | left = left[i:] |
| 236 | right = right[i:] |
| 237 | if len(left) == len(right): |
| 238 | for i in range(len(left)): |
| 239 | if left[-i] != right[-i]: |
| 240 | break |
| 241 | if i > 42: |
| 242 | i -= 10 # Provide some context |
| 243 | explanation += [ |
| 244 | "Skipping {} identical trailing " |
| 245 | "characters in diff, use -v to show".format(i) |
| 246 | ] |
| 247 | left = left[:-i] |
| 248 | right = right[:-i] |
| 249 | keepends = True |
| 250 | if left.isspace() or right.isspace(): |
| 251 | left = repr(str(left)) |
| 252 | right = repr(str(right)) |
| 253 | explanation += ["Strings contain only whitespace, escaping them using repr()"] |
| 254 | # "right" is the expected base against which we compare "left", |
| 255 | # see https://github.com/pytest-dev/pytest/issues/3333 |
| 256 | explanation += [ |
| 257 | line.strip("\n") |
| 258 | for line in ndiff(right.splitlines(keepends), left.splitlines(keepends)) |
| 259 | ] |
| 260 | return explanation |
| 261 | |
| 262 | |
| 263 | def _compare_eq_verbose(left: Any, right: Any) -> List[str]: |
no test coverage detected