Skip to content

Commit 0611968

Browse files
[match case] Use match case in assertrepr_compare
1 parent 9355aad commit 0611968

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

src/_pytest/assertion/util.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ def __call__(self, source: str, lexer: Literal["diff", "python"] = "python") ->
4343
"""Apply highlighting to the given source."""
4444

4545

46+
CompareSetFunction = dict[
47+
str,
48+
Callable[
49+
[AbstractSet[Any], AbstractSet[Any], _HighlightFunc, int],
50+
list[str],
51+
],
52+
]
53+
54+
4655
def dummy_highlighter(source: str, lexer: Literal["diff", "python"] = "python") -> str:
4756
"""Dummy highlighter that returns the text unprocessed.
4857
@@ -204,30 +213,28 @@ def assertrepr_compare(
204213

205214
summary = f"{left_repr} {op} {right_repr}"
206215
highlighter = config.get_terminal_writer()._highlight
207-
208-
explanation = None
216+
explanation: list[str] | None
209217
try:
210-
if op == "==":
211-
explanation = _compare_eq_any(left, right, highlighter, verbose)
212-
elif op == "not in":
213-
if istext(left) and istext(right):
218+
match (left, op, right):
219+
case (_, "==", _):
220+
explanation = _compare_eq_any(left, right, highlighter, verbose)
221+
case (str(), "not in", str()):
214222
explanation = _notin_text(left, right, verbose)
215-
elif op == "!=":
216-
if isset(left) and isset(right):
217-
explanation = ["Both sets are equal"]
218-
elif op == ">=":
219-
if isset(left) and isset(right):
220-
explanation = _compare_gte_set(left, right, highlighter, verbose)
221-
elif op == "<=":
222-
if isset(left) and isset(right):
223-
explanation = _compare_lte_set(left, right, highlighter, verbose)
224-
elif op == ">":
225-
if isset(left) and isset(right):
226-
explanation = _compare_gt_set(left, right, highlighter, verbose)
227-
elif op == "<":
228-
if isset(left) and isset(right):
229-
explanation = _compare_lt_set(left, right, highlighter, verbose)
230-
223+
case (
224+
set() | frozenset(),
225+
"!=" | ">=" | "<=" | ">" | "<",
226+
set() | frozenset(),
227+
):
228+
set_compare_func: CompareSetFunction = {
229+
"!=": lambda *a, **kw: ["Both sets are equal"],
230+
">=": _compare_gte_set,
231+
"<=": _compare_lte_set,
232+
">": _compare_gt_set,
233+
"<": _compare_lt_set,
234+
}
235+
explanation = set_compare_func[op](left, right, highlighter, verbose)
236+
case _:
237+
explanation = None
231238
except outcomes.Exit:
232239
raise
233240
except Exception:

testing/test_assertion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,10 +1976,10 @@ def f():
19761976

19771977

19781978
def test_exit_from_assertrepr_compare(monkeypatch) -> None:
1979-
def raise_exit(obj):
1979+
def raise_exit(*args, **kwargs):
19801980
outcomes.exit("Quitting debugger")
19811981

1982-
monkeypatch.setattr(util, "istext", raise_exit)
1982+
monkeypatch.setattr(util, "_compare_eq_any", raise_exit)
19831983

19841984
with pytest.raises(outcomes.Exit, match="Quitting debugger"):
19851985
callequal(1, 1)

0 commit comments

Comments
 (0)