@@ -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+
4655def 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 :
0 commit comments