| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from collections.abc import Iterator |
| 4 |
import dataclasses |
| 5 |
import pprint |
| 6 |
|
| 7 |
from _pytest.assertion._compare_mapping import _compare_eq_mapping |
| 8 |
from _pytest.assertion._compare_sequence import _compare_eq_iterable |
| 9 |
from _pytest.assertion._compare_sequence import _compare_eq_sequence |
| 10 |
from _pytest.assertion._compare_set import _compare_eq_set |
| 11 |
from _pytest.assertion._guards import has_default_eq |
| 12 |
from _pytest.assertion._guards import isattrs |
| 13 |
from _pytest.assertion._guards import isdatacls |
| 14 |
from _pytest.assertion._guards import isiterable |
| 15 |
from _pytest.assertion._guards import ismapping |
| 16 |
from _pytest.assertion._guards import isnamedtuple |
| 17 |
from _pytest.assertion._guards import issequence |
| 18 |
from _pytest.assertion._guards import isset |
| 19 |
from _pytest.assertion._guards import istext |
| 20 |
from _pytest.assertion._typing import _AssertionTextDiffStyle |
| 21 |
from _pytest.assertion._typing import _HighlightFunc |
| 22 |
from _pytest.assertion.compare_text import _compare_eq_text |
| 23 |
|
| 24 |
|
| 25 |
def _compare_eq_any( |
| 26 |
left: object, |
| 27 |
right: object, |
| 28 |
highlighter: _HighlightFunc, |
| 29 |
verbose: int, |
| 30 |
assertion_text_diff_style: _AssertionTextDiffStyle, |
| 31 |
) -> Iterator[str]: |
| 32 |
"""Yield the per-line explanation for ``left == right`` (without summary). |
| 33 |
|
| 34 |
Yields nothing when no specialised explanation applies, so consumers |
| 35 |
can stream the output and bail out early (e.g. for truncation) without |
| 36 |
materialising the entire diff first. |
| 37 |
""" |
| 38 |
if istext(left) and istext(right): |
| 39 |
yield from _compare_eq_text( |
| 40 |
left, |
| 41 |
right, |
| 42 |
highlighter, |
| 43 |
verbose, |
| 44 |
assertion_text_diff_style, |
| 45 |
) |
| 46 |
else: |
| 47 |
from _pytest.python_api import ApproxBase |
| 48 |
|
| 49 |
# Although the common order should be obtained == approx(...), allow both ways. |
| 50 |
if isinstance(right, ApproxBase): |
| 51 |
yield from right._repr_compare(left) |
| 52 |
elif isinstance(left, ApproxBase): |
| 53 |
yield from left._repr_compare(right) |
| 54 |
elif type(left) is type(right) and ( |
| 55 |
isdatacls(left) or isattrs(left) or isnamedtuple(left) |
| 56 |
): |
| 57 |
# Note: unlike dataclasses/attrs, namedtuples compare only the |
| 58 |
# field values, not the type or field names. But this branch |
| 59 |
# intentionally only handles the same-type case, which was often |
| 60 |
# used in older code bases before dataclasses/attrs were available. |
| 61 |
yield from _compare_eq_cls( |
| 62 |
left, |
| 63 |
right, |
| 64 |
highlighter, |
| 65 |
verbose, |
| 66 |
assertion_text_diff_style, |
| 67 |
) |
| 68 |
elif issequence(left) and issequence(right): |
| 69 |
yield from _compare_eq_sequence(left, right, highlighter, verbose) |
| 70 |
elif isset(left) and isset(right): |
| 71 |
yield from _compare_eq_set(left, right, highlighter, verbose) |
| 72 |
elif ismapping(left) and ismapping(right): |
| 73 |
yield from _compare_eq_mapping(left, right, highlighter, verbose) |
| 74 |
|
| 75 |
if isiterable(left) and isiterable(right): |
| 76 |
yield from _compare_eq_iterable(left, right, highlighter, verbose) |
| 77 |
|
| 78 |
|
| 79 |
def _compare_eq_cls( |
| 80 |
left: object, |
| 81 |
right: object, |
| 82 |
highlighter: _HighlightFunc, |
| 83 |
verbose: int, |
| 84 |
assertion_text_diff_style: _AssertionTextDiffStyle, |
| 85 |
) -> Iterator[str]: |
| 86 |
if not has_default_eq(left): |
| 87 |
return |
| 88 |
if isdatacls(left): |
| 89 |
all_fields = dataclasses.fields(left) |
| 90 |
fields_to_check = [info.name for info in all_fields if info.compare] |
| 91 |
elif isattrs(left): |
| 92 |
all_fields = left.__attrs_attrs__ # type: ignore[attr-defined] |
| 93 |
fields_to_check = [field.name for field in all_fields if getattr(field, "eq")] |
| 94 |
elif isnamedtuple(left): |
| 95 |
fields_to_check = left._fields # type: ignore[attr-defined] |
| 96 |
else: |
| 97 |
assert False |
| 98 |
|
| 99 |
indent = " " |
| 100 |
same = [] |
| 101 |
diff = [] |
| 102 |
for field in fields_to_check: |
| 103 |
if getattr(left, field) == getattr(right, field): |
| 104 |
same.append(field) |
| 105 |
else: |
| 106 |
diff.append(field) |
| 107 |
|
| 108 |
if same or diff: |
| 109 |
yield "" |
| 110 |
if same and verbose < 2: |
| 111 |
yield f"Omitting {len(same)} identical items, use -vv to show" |
| 112 |
elif same: |
| 113 |
yield "Matching attributes:" |
| 114 |
yield from highlighter(pprint.pformat(same)).splitlines() |
| 115 |
if diff: |
| 116 |
yield "Differing attributes:" |
| 117 |
yield from highlighter(pprint.pformat(diff)).splitlines() |
| 118 |
for field in diff: |
| 119 |
field_left = getattr(left, field) |
| 120 |
field_right = getattr(right, field) |
| 121 |
yield "" |
| 122 |
yield f"Drill down into differing attribute {field}:" |
| 123 |
yield f"{indent}{field}: {highlighter(repr(field_left))} != {highlighter(repr(field_right))}" |
| 124 |
for line in _compare_eq_any( |
| 125 |
field_left, |
| 126 |
field_right, |
| 127 |
highlighter, |
| 128 |
verbose, |
| 129 |
assertion_text_diff_style, |
| 130 |
): |
| 131 |
yield indent + line |