(
left: Sequence[Any], right: Sequence[Any], verbose: int = 0
)
| 316 | |
| 317 | |
| 318 | def _compare_eq_sequence( |
| 319 | left: Sequence[Any], right: Sequence[Any], verbose: int = 0 |
| 320 | ) -> List[str]: |
| 321 | comparing_bytes = isinstance(left, bytes) and isinstance(right, bytes) |
| 322 | explanation: List[str] = [] |
| 323 | len_left = len(left) |
| 324 | len_right = len(right) |
| 325 | for i in range(min(len_left, len_right)): |
| 326 | if left[i] != right[i]: |
| 327 | if comparing_bytes: |
| 328 | # when comparing bytes, we want to see their ascii representation |
| 329 | # instead of their numeric values (#5260) |
| 330 | # using a slice gives us the ascii representation: |
| 331 | # >>> s = b'foo' |
| 332 | # >>> s[0] |
| 333 | # 102 |
| 334 | # >>> s[0:1] |
| 335 | # b'f' |
| 336 | left_value = left[i : i + 1] |
| 337 | right_value = right[i : i + 1] |
| 338 | else: |
| 339 | left_value = left[i] |
| 340 | right_value = right[i] |
| 341 | |
| 342 | explanation += [f"At index {i} diff: {left_value!r} != {right_value!r}"] |
| 343 | break |
| 344 | |
| 345 | if comparing_bytes: |
| 346 | # when comparing bytes, it doesn't help to show the "sides contain one or more |
| 347 | # items" longer explanation, so skip it |
| 348 | |
| 349 | return explanation |
| 350 | |
| 351 | len_diff = len_left - len_right |
| 352 | if len_diff: |
| 353 | if len_diff > 0: |
| 354 | dir_with_more = "Left" |
| 355 | extra = saferepr(left[len_right]) |
| 356 | else: |
| 357 | len_diff = 0 - len_diff |
| 358 | dir_with_more = "Right" |
| 359 | extra = saferepr(right[len_left]) |
| 360 | |
| 361 | if len_diff == 1: |
| 362 | explanation += [f"{dir_with_more} contains one more item: {extra}"] |
| 363 | else: |
| 364 | explanation += [ |
| 365 | "%s contains %d more items, first extra item: %s" |
| 366 | % (dir_with_more, len_diff, extra) |
| 367 | ] |
| 368 | return explanation |
| 369 | |
| 370 | |
| 371 | def _compare_eq_set( |
no test coverage detected