Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/_pytest/assertion/_compare_set.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from collections.abc import Callable
from collections.abc import Iterable
from collections.abc import Iterator
from collections.abc import Set as AbstractSet
from typing import TypeAlias
Expand Down Expand Up @@ -77,14 +76,24 @@ def _compare_lt_set(

SetComparisonFunction: TypeAlias = Callable[
[AbstractSet[object], AbstractSet[object], _HighlightFunc, int],
Iterable[str],
Iterator[str],
]


def _both_sets_are_equal(
left: AbstractSet[object],
right: AbstractSet[object],
highlighter: _HighlightFunc,
verbose: int = 0,
) -> Iterator[str]:
yield "Both sets are equal"


SET_COMPARISON_FUNCTIONS: dict[str, SetComparisonFunction] = {
# == can't be done here without a prior refactor because there's an additional
# explanation for iterable in _compare_eq_any
# "==": _compare_eq_set,
"!=": lambda *a, **kw: ["Both sets are equal"],
"!=": _both_sets_are_equal,
">=": _compare_gte_set,
"<=": _compare_lte_set,
">": _compare_gt_set,
Expand Down
6 changes: 2 additions & 4 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def assertrepr_compare(

try:
if op == "==":
source: Iterator[str] = _compare_eq_any(
source = _compare_eq_any(
left,
right,
highlighter,
Expand All @@ -187,9 +187,7 @@ def assertrepr_compare(
elif op == "not in" and istext(left) and istext(right):
source = _notin_text(left, right, verbose)
elif op in {"!=", ">=", "<=", ">", "<"} and isset(left) and isset(right):
source = iter(
SET_COMPARISON_FUNCTIONS[op](left, right, highlighter, verbose)
)
source = SET_COMPARISON_FUNCTIONS[op](left, right, highlighter, verbose)
else:
source = iter(())

Expand Down
Loading