|
8 | 8 | from collections.abc import Iterable |
9 | 9 | from collections.abc import Mapping |
10 | 10 | from collections.abc import Sequence |
11 | | -from collections.abc import Set as AbstractSet |
12 | 11 | import os |
13 | 12 | import pprint |
14 | 13 | from typing import Any |
15 | 14 | from typing import Literal |
16 | | -from typing import Protocol |
17 | 15 | from unicodedata import normalize |
18 | 16 |
|
19 | 17 | from _pytest import outcomes |
20 | 18 | import _pytest._code |
21 | 19 | from _pytest._io.pprint import PrettyPrinter |
22 | 20 | from _pytest._io.saferepr import saferepr |
23 | 21 | from _pytest._io.saferepr import saferepr_unlimited |
| 22 | +from _pytest.assertion._compare_set import _compare_eq_set |
| 23 | +from _pytest.assertion._compare_set import SET_COMPARISON_FUNCTIONS |
| 24 | +from _pytest.assertion._typing import _HighlightFunc |
24 | 25 | from _pytest.config import Config |
25 | 26 |
|
26 | 27 |
|
|
38 | 39 | _config: Config | None = None |
39 | 40 |
|
40 | 41 |
|
41 | | -class _HighlightFunc(Protocol): |
42 | | - def __call__(self, source: str, lexer: Literal["diff", "python"] = "python") -> str: |
43 | | - """Apply highlighting to the given source.""" |
44 | | - |
45 | | - |
46 | | -CompareSetFunction = dict[ |
47 | | - str, |
48 | | - Callable[ |
49 | | - [AbstractSet[Any], AbstractSet[Any], _HighlightFunc, int], |
50 | | - list[str], |
51 | | - ], |
52 | | -] |
53 | | - |
54 | | - |
55 | 42 | def dummy_highlighter(source: str, lexer: Literal["diff", "python"] = "python") -> str: |
56 | 43 | """Dummy highlighter that returns the text unprocessed. |
57 | 44 |
|
@@ -225,14 +212,9 @@ def assertrepr_compare( |
225 | 212 | "!=" | ">=" | "<=" | ">" | "<", |
226 | 213 | set() | frozenset(), |
227 | 214 | ): |
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) |
| 215 | + explanation = SET_COMPARISON_FUNCTIONS[op]( |
| 216 | + left, right, highlighter, verbose |
| 217 | + ) |
236 | 218 | case _: |
237 | 219 | explanation = None |
238 | 220 | except outcomes.Exit: |
@@ -433,75 +415,6 @@ def _compare_eq_sequence( |
433 | 415 | return explanation |
434 | 416 |
|
435 | 417 |
|
436 | | -def _compare_eq_set( |
437 | | - left: AbstractSet[Any], |
438 | | - right: AbstractSet[Any], |
439 | | - highlighter: _HighlightFunc, |
440 | | - verbose: int = 0, |
441 | | -) -> list[str]: |
442 | | - explanation = [] |
443 | | - explanation.extend(_set_one_sided_diff("left", left, right, highlighter)) |
444 | | - explanation.extend(_set_one_sided_diff("right", right, left, highlighter)) |
445 | | - return explanation |
446 | | - |
447 | | - |
448 | | -def _compare_gt_set( |
449 | | - left: AbstractSet[Any], |
450 | | - right: AbstractSet[Any], |
451 | | - highlighter: _HighlightFunc, |
452 | | - verbose: int = 0, |
453 | | -) -> list[str]: |
454 | | - explanation = _compare_gte_set(left, right, highlighter) |
455 | | - if not explanation: |
456 | | - return ["Both sets are equal"] |
457 | | - return explanation |
458 | | - |
459 | | - |
460 | | -def _compare_lt_set( |
461 | | - left: AbstractSet[Any], |
462 | | - right: AbstractSet[Any], |
463 | | - highlighter: _HighlightFunc, |
464 | | - verbose: int = 0, |
465 | | -) -> list[str]: |
466 | | - explanation = _compare_lte_set(left, right, highlighter) |
467 | | - if not explanation: |
468 | | - return ["Both sets are equal"] |
469 | | - return explanation |
470 | | - |
471 | | - |
472 | | -def _compare_gte_set( |
473 | | - left: AbstractSet[Any], |
474 | | - right: AbstractSet[Any], |
475 | | - highlighter: _HighlightFunc, |
476 | | - verbose: int = 0, |
477 | | -) -> list[str]: |
478 | | - return _set_one_sided_diff("right", right, left, highlighter) |
479 | | - |
480 | | - |
481 | | -def _compare_lte_set( |
482 | | - left: AbstractSet[Any], |
483 | | - right: AbstractSet[Any], |
484 | | - highlighter: _HighlightFunc, |
485 | | - verbose: int = 0, |
486 | | -) -> list[str]: |
487 | | - return _set_one_sided_diff("left", left, right, highlighter) |
488 | | - |
489 | | - |
490 | | -def _set_one_sided_diff( |
491 | | - posn: str, |
492 | | - set1: AbstractSet[Any], |
493 | | - set2: AbstractSet[Any], |
494 | | - highlighter: _HighlightFunc, |
495 | | -) -> list[str]: |
496 | | - explanation = [] |
497 | | - diff = set1 - set2 |
498 | | - if diff: |
499 | | - explanation.append(f"Extra items in the {posn} set:") |
500 | | - for item in diff: |
501 | | - explanation.append(highlighter(saferepr(item))) |
502 | | - return explanation |
503 | | - |
504 | | - |
505 | 418 | def _compare_eq_dict( |
506 | 419 | left: Mapping[Any, Any], |
507 | 420 | right: Mapping[Any, Any], |
|
0 commit comments