@@ -192,12 +192,12 @@ def assertrepr_compare(
192
192
right_repr = saferepr (right , maxsize = maxsize , use_ascii = use_ascii )
193
193
194
194
summary = f"{ left_repr } { op } { right_repr } "
195
+ highlighter = config .get_terminal_writer ()._highlight
195
196
196
197
explanation = None
197
198
try :
198
199
if op == "==" :
199
- writer = config .get_terminal_writer ()
200
- explanation = _compare_eq_any (left , right , writer ._highlight , verbose )
200
+ explanation = _compare_eq_any (left , right , highlighter , verbose )
201
201
elif op == "not in" :
202
202
if istext (left ) and istext (right ):
203
203
explanation = _notin_text (left , right , verbose )
@@ -206,16 +206,16 @@ def assertrepr_compare(
206
206
explanation = ["Both sets are equal" ]
207
207
elif op == ">=" :
208
208
if isset (left ) and isset (right ):
209
- explanation = _compare_gte_set (left , right , verbose )
209
+ explanation = _compare_gte_set (left , right , highlighter , verbose )
210
210
elif op == "<=" :
211
211
if isset (left ) and isset (right ):
212
- explanation = _compare_lte_set (left , right , verbose )
212
+ explanation = _compare_lte_set (left , right , highlighter , verbose )
213
213
elif op == ">" :
214
214
if isset (left ) and isset (right ):
215
- explanation = _compare_gt_set (left , right , verbose )
215
+ explanation = _compare_gt_set (left , right , highlighter , verbose )
216
216
elif op == "<" :
217
217
if isset (left ) and isset (right ):
218
- explanation = _compare_lt_set (left , right , verbose )
218
+ explanation = _compare_lt_set (left , right , highlighter , verbose )
219
219
220
220
except outcomes .Exit :
221
221
raise
@@ -259,11 +259,11 @@ def _compare_eq_any(
259
259
# used in older code bases before dataclasses/attrs were available.
260
260
explanation = _compare_eq_cls (left , right , highlighter , verbose )
261
261
elif issequence (left ) and issequence (right ):
262
- explanation = _compare_eq_sequence (left , right , verbose )
262
+ explanation = _compare_eq_sequence (left , right , highlighter , verbose )
263
263
elif isset (left ) and isset (right ):
264
- explanation = _compare_eq_set (left , right , verbose )
264
+ explanation = _compare_eq_set (left , right , highlighter , verbose )
265
265
elif isdict (left ) and isdict (right ):
266
- explanation = _compare_eq_dict (left , right , verbose )
266
+ explanation = _compare_eq_dict (left , right , highlighter , verbose )
267
267
268
268
if isiterable (left ) and isiterable (right ):
269
269
expl = _compare_eq_iterable (left , right , highlighter , verbose )
@@ -350,7 +350,10 @@ def _compare_eq_iterable(
350
350
351
351
352
352
def _compare_eq_sequence (
353
- left : Sequence [Any ], right : Sequence [Any ], verbose : int = 0
353
+ left : Sequence [Any ],
354
+ right : Sequence [Any ],
355
+ highlighter : _HighlightFunc ,
356
+ verbose : int = 0 ,
354
357
) -> List [str ]:
355
358
comparing_bytes = isinstance (left , bytes ) and isinstance (right , bytes )
356
359
explanation : List [str ] = []
@@ -373,7 +376,10 @@ def _compare_eq_sequence(
373
376
left_value = left [i ]
374
377
right_value = right [i ]
375
378
376
- explanation += [f"At index { i } diff: { left_value !r} != { right_value !r} " ]
379
+ explanation .append (
380
+ f"At index { i } diff:"
381
+ f" { highlighter (repr (left_value ))} != { highlighter (repr (right_value ))} "
382
+ )
377
383
break
378
384
379
385
if comparing_bytes :
@@ -393,68 +399,91 @@ def _compare_eq_sequence(
393
399
extra = saferepr (right [len_left ])
394
400
395
401
if len_diff == 1 :
396
- explanation += [f"{ dir_with_more } contains one more item: { extra } " ]
402
+ explanation += [
403
+ f"{ dir_with_more } contains one more item: { highlighter (extra )} "
404
+ ]
397
405
else :
398
406
explanation += [
399
407
"%s contains %d more items, first extra item: %s"
400
- % (dir_with_more , len_diff , extra )
408
+ % (dir_with_more , len_diff , highlighter ( extra ) )
401
409
]
402
410
return explanation
403
411
404
412
405
413
def _compare_eq_set (
406
- left : AbstractSet [Any ], right : AbstractSet [Any ], verbose : int = 0
414
+ left : AbstractSet [Any ],
415
+ right : AbstractSet [Any ],
416
+ highlighter : _HighlightFunc ,
417
+ verbose : int = 0 ,
407
418
) -> List [str ]:
408
419
explanation = []
409
- explanation .extend (_set_one_sided_diff ("left" , left , right ))
410
- explanation .extend (_set_one_sided_diff ("right" , right , left ))
420
+ explanation .extend (_set_one_sided_diff ("left" , left , right , highlighter ))
421
+ explanation .extend (_set_one_sided_diff ("right" , right , left , highlighter ))
411
422
return explanation
412
423
413
424
414
425
def _compare_gt_set (
415
- left : AbstractSet [Any ], right : AbstractSet [Any ], verbose : int = 0
426
+ left : AbstractSet [Any ],
427
+ right : AbstractSet [Any ],
428
+ highlighter : _HighlightFunc ,
429
+ verbose : int = 0 ,
416
430
) -> List [str ]:
417
- explanation = _compare_gte_set (left , right , verbose )
431
+ explanation = _compare_gte_set (left , right , highlighter )
418
432
if not explanation :
419
433
return ["Both sets are equal" ]
420
434
return explanation
421
435
422
436
423
437
def _compare_lt_set (
424
- left : AbstractSet [Any ], right : AbstractSet [Any ], verbose : int = 0
438
+ left : AbstractSet [Any ],
439
+ right : AbstractSet [Any ],
440
+ highlighter : _HighlightFunc ,
441
+ verbose : int = 0 ,
425
442
) -> List [str ]:
426
- explanation = _compare_lte_set (left , right , verbose )
443
+ explanation = _compare_lte_set (left , right , highlighter )
427
444
if not explanation :
428
445
return ["Both sets are equal" ]
429
446
return explanation
430
447
431
448
432
449
def _compare_gte_set (
433
- left : AbstractSet [Any ], right : AbstractSet [Any ], verbose : int = 0
450
+ left : AbstractSet [Any ],
451
+ right : AbstractSet [Any ],
452
+ highlighter : _HighlightFunc ,
453
+ verbose : int = 0 ,
434
454
) -> List [str ]:
435
- return _set_one_sided_diff ("right" , right , left )
455
+ return _set_one_sided_diff ("right" , right , left , highlighter )
436
456
437
457
438
458
def _compare_lte_set (
439
- left : AbstractSet [Any ], right : AbstractSet [Any ], verbose : int = 0
459
+ left : AbstractSet [Any ],
460
+ right : AbstractSet [Any ],
461
+ highlighter : _HighlightFunc ,
462
+ verbose : int = 0 ,
440
463
) -> List [str ]:
441
- return _set_one_sided_diff ("left" , left , right )
464
+ return _set_one_sided_diff ("left" , left , right , highlighter )
442
465
443
466
444
467
def _set_one_sided_diff (
445
- posn : str , set1 : AbstractSet [Any ], set2 : AbstractSet [Any ]
468
+ posn : str ,
469
+ set1 : AbstractSet [Any ],
470
+ set2 : AbstractSet [Any ],
471
+ highlighter : _HighlightFunc ,
446
472
) -> List [str ]:
447
473
explanation = []
448
474
diff = set1 - set2
449
475
if diff :
450
476
explanation .append (f"Extra items in the { posn } set:" )
451
477
for item in diff :
452
- explanation .append (saferepr (item ))
478
+ explanation .append (highlighter ( saferepr (item ) ))
453
479
return explanation
454
480
455
481
456
482
def _compare_eq_dict (
457
- left : Mapping [Any , Any ], right : Mapping [Any , Any ], verbose : int = 0
483
+ left : Mapping [Any , Any ],
484
+ right : Mapping [Any , Any ],
485
+ highlighter : _HighlightFunc ,
486
+ verbose : int = 0 ,
458
487
) -> List [str ]:
459
488
explanation : List [str ] = []
460
489
set_left = set (left )
@@ -465,12 +494,16 @@ def _compare_eq_dict(
465
494
explanation += ["Omitting %s identical items, use -vv to show" % len (same )]
466
495
elif same :
467
496
explanation += ["Common items:" ]
468
- explanation += pprint .pformat (same ).splitlines ()
497
+ explanation += highlighter ( pprint .pformat (same ) ).splitlines ()
469
498
diff = {k for k in common if left [k ] != right [k ]}
470
499
if diff :
471
500
explanation += ["Differing items:" ]
472
501
for k in diff :
473
- explanation += [saferepr ({k : left [k ]}) + " != " + saferepr ({k : right [k ]})]
502
+ explanation += [
503
+ highlighter (saferepr ({k : left [k ]}))
504
+ + " != "
505
+ + highlighter (saferepr ({k : right [k ]}))
506
+ ]
474
507
extra_left = set_left - set_right
475
508
len_extra_left = len (extra_left )
476
509
if len_extra_left :
@@ -479,7 +512,7 @@ def _compare_eq_dict(
479
512
% (len_extra_left , "" if len_extra_left == 1 else "s" )
480
513
)
481
514
explanation .extend (
482
- pprint .pformat ({k : left [k ] for k in extra_left }).splitlines ()
515
+ highlighter ( pprint .pformat ({k : left [k ] for k in extra_left }) ).splitlines ()
483
516
)
484
517
extra_right = set_right - set_left
485
518
len_extra_right = len (extra_right )
@@ -489,7 +522,7 @@ def _compare_eq_dict(
489
522
% (len_extra_right , "" if len_extra_right == 1 else "s" )
490
523
)
491
524
explanation .extend (
492
- pprint .pformat ({k : right [k ] for k in extra_right }).splitlines ()
525
+ highlighter ( pprint .pformat ({k : right [k ] for k in extra_right }) ).splitlines ()
493
526
)
494
527
return explanation
495
528
@@ -528,17 +561,17 @@ def _compare_eq_cls(
528
561
explanation .append ("Omitting %s identical items, use -vv to show" % len (same ))
529
562
elif same :
530
563
explanation += ["Matching attributes:" ]
531
- explanation += pprint .pformat (same ).splitlines ()
564
+ explanation += highlighter ( pprint .pformat (same ) ).splitlines ()
532
565
if diff :
533
566
explanation += ["Differing attributes:" ]
534
- explanation += pprint .pformat (diff ).splitlines ()
567
+ explanation += highlighter ( pprint .pformat (diff ) ).splitlines ()
535
568
for field in diff :
536
569
field_left = getattr (left , field )
537
570
field_right = getattr (right , field )
538
571
explanation += [
539
572
"" ,
540
- "Drill down into differing attribute %s:" % field ,
541
- ( "%s%s: %r != %r" ) % ( indent , field , field_left , field_right ),
573
+ f "Drill down into differing attribute { field } :" ,
574
+ f" { indent } { field } : { highlighter ( repr ( field_left )) } != { highlighter ( repr ( field_right )) } " ,
542
575
]
543
576
explanation += [
544
577
indent + line
0 commit comments