Skip to content

Commit 2ff1881

Browse files
[match case] Use match case in assertrepr_compare
1 parent 12eb748 commit 2ff1881

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

src/_pytest/assertion/_compare_set.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,12 @@ def _compare_lte_set(
7575
verbose: int = 0,
7676
) -> list[str]:
7777
return _set_one_sided_diff("left", left, right, highlighter)
78+
79+
80+
SetComparisonFunction = dict[
81+
str,
82+
Callable[
83+
[AbstractSet[Any], AbstractSet[Any], _HighlightFunc, int],
84+
list[str],
85+
],
86+
]

src/_pytest/assertion/util.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from _pytest.assertion._compare_set import _compare_gte_set
2525
from _pytest.assertion._compare_set import _compare_lt_set
2626
from _pytest.assertion._compare_set import _compare_lte_set
27+
from _pytest.assertion._compare_set import SetComparisonFunction
2728
from _pytest.assertion._typing import _HighlightFunc
2829
from _pytest.config import Config
2930

@@ -203,30 +204,28 @@ def assertrepr_compare(
203204

204205
summary = f"{left_repr} {op} {right_repr}"
205206
highlighter = config.get_terminal_writer()._highlight
206-
207-
explanation = None
207+
explanation: list[str] | None
208208
try:
209-
if op == "==":
210-
explanation = _compare_eq_any(left, right, highlighter, verbose)
211-
elif op == "not in":
212-
if istext(left) and istext(right):
209+
match (left, op, right):
210+
case (_, "==", _):
211+
explanation = _compare_eq_any(left, right, highlighter, verbose)
212+
case (str(), "not in", str()):
213213
explanation = _notin_text(left, right, verbose)
214-
elif op == "!=":
215-
if isset(left) and isset(right):
216-
explanation = ["Both sets are equal"]
217-
elif op == ">=":
218-
if isset(left) and isset(right):
219-
explanation = _compare_gte_set(left, right, highlighter, verbose)
220-
elif op == "<=":
221-
if isset(left) and isset(right):
222-
explanation = _compare_lte_set(left, right, highlighter, verbose)
223-
elif op == ">":
224-
if isset(left) and isset(right):
225-
explanation = _compare_gt_set(left, right, highlighter, verbose)
226-
elif op == "<":
227-
if isset(left) and isset(right):
228-
explanation = _compare_lt_set(left, right, highlighter, verbose)
229-
214+
case (
215+
set() | frozenset(),
216+
"!=" | ">=" | "<=" | ">" | "<",
217+
set() | frozenset(),
218+
):
219+
set_compare_func: SetComparisonFunction = {
220+
"!=": lambda *a, **kw: ["Both sets are equal"],
221+
">=": _compare_gte_set,
222+
"<=": _compare_lte_set,
223+
">": _compare_gt_set,
224+
"<": _compare_lt_set,
225+
}
226+
explanation = set_compare_func[op](left, right, highlighter, verbose)
227+
case _:
228+
explanation = None
230229
except outcomes.Exit:
231230
raise
232231
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)