diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index 3898a6f79a8..4d0157e9e38 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -6,6 +6,7 @@ from collections.abc import Callable from collections.abc import Iterator from collections.abc import Sequence +from collections.abc import Set as AbstractSet from typing import Literal from unicodedata import normalize @@ -15,8 +16,6 @@ from _pytest._io.saferepr import saferepr_unlimited from _pytest.assertion._compare_any import _compare_eq_any from _pytest.assertion._compare_set import SET_COMPARISON_FUNCTIONS -from _pytest.assertion._guards import isset -from _pytest.assertion._guards import istext from _pytest.assertion._typing import _AssertionTextDiffStyle from _pytest.assertion._typing import _HighlightFunc from _pytest.assertion._typing import NO_TRUNCATION_BUDGET @@ -179,21 +178,26 @@ def assertrepr_compare( summary = f"{left_repr} {op} {right_repr}" try: - if op == "==": - source = _compare_eq_any( - left, - right, - highlighter, - verbose, - assertion_text_diff_style, - truncation_budget, - ) - elif op == "not in" and istext(left) and istext(right): - source = _notin_text(left, right, verbose, truncation_budget) - elif op in {"!=", ">=", "<=", ">", "<"} and isset(left) and isset(right): - source = SET_COMPARISON_FUNCTIONS[op](left, right, highlighter, verbose) - else: - source = iter(()) + match (left, op, right): + case (_, "==", _): + source = _compare_eq_any( + left, + right, + highlighter, + verbose, + assertion_text_diff_style, + truncation_budget, + ) + case (str(), "not in", str()): + source = _notin_text(left, right, verbose, truncation_budget) + case ( + AbstractSet(), + "!=" | ">=" | "<=" | ">" | "<", + AbstractSet(), + ): + source = SET_COMPARISON_FUNCTIONS[op](left, right, highlighter, verbose) + case _: + source = iter(()) # Only yield the summary if there is a detailed explanation. # Make sure there's a separating empty line after the summary.