| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from collections.abc import Callable |
| 4 |
from collections.abc import Iterator |
| 5 |
from collections.abc import Set as AbstractSet |
| 6 |
from typing import TypeAlias |
| 7 |
|
| 8 |
from _pytest._io.saferepr import saferepr |
| 9 |
from _pytest.assertion._typing import _HighlightFunc |
| 10 |
|
| 11 |
|
| 12 |
def _set_one_sided_diff( |
| 13 |
posn: str, |
| 14 |
set1: AbstractSet[object], |
| 15 |
set2: AbstractSet[object], |
| 16 |
highlighter: _HighlightFunc, |
| 17 |
) -> Iterator[str]: |
| 18 |
diff = set1 - set2 |
| 19 |
if diff: |
| 20 |
yield f"Extra items in the {posn} set:" |
| 21 |
for item in diff: |
| 22 |
yield highlighter(saferepr(item)) |
| 23 |
|
| 24 |
|
| 25 |
def _compare_eq_set( |
| 26 |
left: AbstractSet[object], |
| 27 |
right: AbstractSet[object], |
| 28 |
highlighter: _HighlightFunc, |
| 29 |
verbose: int = 0, |
| 30 |
) -> Iterator[str]: |
| 31 |
yield from _set_one_sided_diff("left", left, right, highlighter) |
| 32 |
yield from _set_one_sided_diff("right", right, left, highlighter) |
| 33 |
|
| 34 |
|
| 35 |
def _compare_gte_set( |
| 36 |
left: AbstractSet[object], |
| 37 |
right: AbstractSet[object], |
| 38 |
highlighter: _HighlightFunc, |
| 39 |
verbose: int = 0, |
| 40 |
) -> Iterator[str]: |
| 41 |
yield from _set_one_sided_diff("right", right, left, highlighter) |
| 42 |
|
| 43 |
|
| 44 |
def _compare_lte_set( |
| 45 |
left: AbstractSet[object], |
| 46 |
right: AbstractSet[object], |
| 47 |
highlighter: _HighlightFunc, |
| 48 |
verbose: int = 0, |
| 49 |
) -> Iterator[str]: |
| 50 |
yield from _set_one_sided_diff("left", left, right, highlighter) |
| 51 |
|
| 52 |
|
| 53 |
def _compare_gt_set( |
| 54 |
left: AbstractSet[object], |
| 55 |
right: AbstractSet[object], |
| 56 |
highlighter: _HighlightFunc, |
| 57 |
verbose: int = 0, |
| 58 |
) -> Iterator[str]: |
| 59 |
if left == right: |
| 60 |
yield "Both sets are equal" |
| 61 |
else: |
| 62 |
yield from _set_one_sided_diff("right", right, left, highlighter) |
| 63 |
|
| 64 |
|
| 65 |
def _compare_lt_set( |
| 66 |
left: AbstractSet[object], |
| 67 |
right: AbstractSet[object], |
| 68 |
highlighter: _HighlightFunc, |
| 69 |
verbose: int = 0, |
| 70 |
) -> Iterator[str]: |
| 71 |
if left == right: |
| 72 |
yield "Both sets are equal" |
| 73 |
else: |
| 74 |
yield from _set_one_sided_diff("left", left, right, highlighter) |
| 75 |
|
| 76 |
|
| 77 |
SetComparisonFunction: TypeAlias = Callable[ |
| 78 |
[AbstractSet[object], AbstractSet[object], _HighlightFunc, int], |
| 79 |
Iterator[str], |
| 80 |
] |
| 81 |
|
| 82 |
|
| 83 |
def _both_sets_are_equal( |
| 84 |
left: AbstractSet[object], |
| 85 |
right: AbstractSet[object], |
| 86 |
highlighter: _HighlightFunc, |
| 87 |
verbose: int = 0, |
| 88 |
) -> Iterator[str]: |
| 89 |
yield "Both sets are equal" |
| 90 |
|
| 91 |
|
| 92 |
SET_COMPARISON_FUNCTIONS: dict[str, SetComparisonFunction] = { |
| 93 |
# == can't be done here without a prior refactor because there's an additional |
| 94 |
# explanation for iterable in _compare_eq_any |
| 95 |
# "==": _compare_eq_set, |
| 96 |
"!=": _both_sets_are_equal, |
| 97 |
">=": _compare_gte_set, |
| 98 |
"<=": _compare_lte_set, |
| 99 |
">": _compare_gt_set, |
| 100 |
"<": _compare_lt_set, |
| 101 |
} |