|
24 | 24 | from _pytest.assertion._compare_set import _compare_gte_set
|
25 | 25 | from _pytest.assertion._compare_set import _compare_lt_set
|
26 | 26 | from _pytest.assertion._compare_set import _compare_lte_set
|
| 27 | +from _pytest.assertion._compare_set import SetComparisonFunction |
27 | 28 | from _pytest.assertion._typing import _HighlightFunc
|
28 | 29 | from _pytest.config import Config
|
29 | 30 |
|
@@ -203,30 +204,28 @@ def assertrepr_compare(
|
203 | 204 |
|
204 | 205 | summary = f"{left_repr} {op} {right_repr}"
|
205 | 206 | highlighter = config.get_terminal_writer()._highlight
|
206 |
| - |
207 |
| - explanation = None |
| 207 | + explanation: list[str] | None |
208 | 208 | 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()): |
213 | 213 | 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 |
230 | 229 | except outcomes.Exit:
|
231 | 230 | raise
|
232 | 231 | except Exception:
|
|
0 commit comments