|
| 1 | +import ast |
| 2 | +from collections.abc import Mapping |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +TYPE_AWARE_EQUAL_NAME = '__opencompass_type_aware_equal' |
| 6 | + |
| 7 | + |
| 8 | +def type_aware_equal(actual: Any, expected: Any) -> bool: |
| 9 | + """Compare values without accepting forged ``__eq__`` on return objects.""" |
| 10 | + if type(actual) is not type(expected): |
| 11 | + return False |
| 12 | + |
| 13 | + if isinstance(actual, (list, tuple)): |
| 14 | + return (len(actual) == len(expected) and all( |
| 15 | + type_aware_equal(a, e) for a, e in zip(actual, expected))) |
| 16 | + |
| 17 | + if isinstance(actual, Mapping): |
| 18 | + if len(actual) != len(expected): |
| 19 | + return False |
| 20 | + |
| 21 | + matched_expected_keys = set() |
| 22 | + for actual_key, actual_value in actual.items(): |
| 23 | + match_idx = None |
| 24 | + for idx, (expected_key, |
| 25 | + expected_value) in enumerate(expected.items()): |
| 26 | + if idx in matched_expected_keys: |
| 27 | + continue |
| 28 | + if (type_aware_equal(actual_key, expected_key) |
| 29 | + and type_aware_equal(actual_value, expected_value)): |
| 30 | + match_idx = idx |
| 31 | + break |
| 32 | + if match_idx is None: |
| 33 | + return False |
| 34 | + matched_expected_keys.add(match_idx) |
| 35 | + return True |
| 36 | + |
| 37 | + if isinstance(actual, (set, frozenset)): |
| 38 | + if len(actual) != len(expected): |
| 39 | + return False |
| 40 | + |
| 41 | + expected_items = list(expected) |
| 42 | + matched_expected_items = set() |
| 43 | + for actual_item in actual: |
| 44 | + match_idx = None |
| 45 | + for idx, expected_item in enumerate(expected_items): |
| 46 | + if idx in matched_expected_items: |
| 47 | + continue |
| 48 | + if type_aware_equal(actual_item, expected_item): |
| 49 | + match_idx = idx |
| 50 | + break |
| 51 | + if match_idx is None: |
| 52 | + return False |
| 53 | + matched_expected_items.add(match_idx) |
| 54 | + return True |
| 55 | + |
| 56 | + try: |
| 57 | + result = actual == expected |
| 58 | + except Exception: |
| 59 | + return False |
| 60 | + return type(result) is bool and result |
| 61 | + |
| 62 | + |
| 63 | +class _TypeAwareAssertTransformer(ast.NodeTransformer): |
| 64 | + """Replace ``assert actual == expected`` with a type-aware comparison.""" |
| 65 | + |
| 66 | + def visit_Assert(self, node: ast.Assert) -> ast.Assert: |
| 67 | + self.generic_visit(node) |
| 68 | + comparison = node.test |
| 69 | + if (isinstance(comparison, ast.Compare) and len(comparison.ops) == 1 |
| 70 | + and isinstance(comparison.ops[0], ast.Eq)): |
| 71 | + node.test = ast.Call( |
| 72 | + func=ast.Name(id=TYPE_AWARE_EQUAL_NAME, ctx=ast.Load()), |
| 73 | + args=[comparison.left, comparison.comparators[0]], |
| 74 | + keywords=[], |
| 75 | + ) |
| 76 | + return node |
| 77 | + |
| 78 | + |
| 79 | +def make_assertions_type_aware(source: str) -> str: |
| 80 | + """Harden direct equality assertions in a dataset-provided test program.""" |
| 81 | + try: |
| 82 | + tree = ast.parse(source) |
| 83 | + except SyntaxError: |
| 84 | + return source |
| 85 | + tree = _TypeAwareAssertTransformer().visit(tree) |
| 86 | + ast.fix_missing_locations(tree) |
| 87 | + return ast.unparse(tree) |
0 commit comments