|
| 1 | +from array_api_tests.dtype_helpers import dtype_to_name |
| 2 | +from array_api_tests import _array_module as xp |
| 3 | +from array_api_tests import __version__ |
| 4 | + |
| 5 | +from collections import Counter |
| 6 | +from types import BuiltinFunctionType, FunctionType |
| 7 | +import dataclasses |
| 8 | +import json |
| 9 | +import warnings |
| 10 | + |
| 11 | +from hypothesis.strategies import SearchStrategy |
| 12 | + |
| 13 | +from pytest import mark, fixture |
| 14 | +try: |
| 15 | + import pytest_jsonreport # noqa |
| 16 | +except ImportError: |
| 17 | + raise ImportError("pytest-json-report is required to run the array API tests") |
| 18 | + |
| 19 | +def to_json_serializable(o): |
| 20 | + if o in dtype_to_name: |
| 21 | + return dtype_to_name[o] |
| 22 | + if isinstance(o, (BuiltinFunctionType, FunctionType, type)): |
| 23 | + return o.__name__ |
| 24 | + if dataclasses.is_dataclass(o): |
| 25 | + return to_json_serializable(dataclasses.asdict(o)) |
| 26 | + if isinstance(o, SearchStrategy): |
| 27 | + return repr(o) |
| 28 | + if isinstance(o, dict): |
| 29 | + return {to_json_serializable(k): to_json_serializable(v) for k, v in o.items()} |
| 30 | + if isinstance(o, tuple): |
| 31 | + if hasattr(o, '_asdict'): # namedtuple |
| 32 | + return to_json_serializable(o._asdict()) |
| 33 | + return tuple(to_json_serializable(i) for i in o) |
| 34 | + if isinstance(o, list): |
| 35 | + return [to_json_serializable(i) for i in o] |
| 36 | + |
| 37 | + # Ensure everything is JSON serializable. If this warning is issued, it |
| 38 | + # means the given type needs to be added above if possible. |
| 39 | + try: |
| 40 | + json.dumps(o) |
| 41 | + except TypeError: |
| 42 | + warnings.warn(f"{o!r} (of type {type(o)}) is not JSON-serializable. Using the repr instead.") |
| 43 | + return repr(o) |
| 44 | + |
| 45 | + return o |
| 46 | + |
| 47 | +@mark.optionalhook |
| 48 | +def pytest_metadata(metadata): |
| 49 | + """ |
| 50 | + Additional global metadata for --json-report. |
| 51 | + """ |
| 52 | + metadata['array_api_tests_module'] = xp.mod_name |
| 53 | + metadata['array_api_tests_version'] = __version__ |
| 54 | + |
| 55 | +@fixture(autouse=True) |
| 56 | +def add_extra_json_metadata(request, json_metadata): |
| 57 | + """ |
| 58 | + Additional per-test metadata for --json-report |
| 59 | + """ |
| 60 | + def add_metadata(name, obj): |
| 61 | + obj = to_json_serializable(obj) |
| 62 | + json_metadata[name] = obj |
| 63 | + |
| 64 | + test_module = request.module.__name__ |
| 65 | + if test_module.startswith('array_api_tests.meta'): |
| 66 | + return |
| 67 | + |
| 68 | + test_function = request.function.__name__ |
| 69 | + assert test_function.startswith('test_'), 'unexpected test function name' |
| 70 | + |
| 71 | + if test_module == 'array_api_tests.test_has_names': |
| 72 | + array_api_function_name = None |
| 73 | + else: |
| 74 | + array_api_function_name = test_function[len('test_'):] |
| 75 | + |
| 76 | + add_metadata('test_module', test_module) |
| 77 | + add_metadata('test_function', test_function) |
| 78 | + add_metadata('array_api_function_name', array_api_function_name) |
| 79 | + |
| 80 | + if hasattr(request.node, 'callspec'): |
| 81 | + params = request.node.callspec.params |
| 82 | + add_metadata('params', params) |
| 83 | + |
| 84 | + def finalizer(): |
| 85 | + # TODO: This metadata is all in the form of error strings. It might be |
| 86 | + # nice to extract the hypothesis failing inputs directly somehow. |
| 87 | + if hasattr(request.node, 'hypothesis_report_information'): |
| 88 | + add_metadata('hypothesis_report_information', request.node.hypothesis_report_information) |
| 89 | + if hasattr(request.node, 'hypothesis_statistics'): |
| 90 | + add_metadata('hypothesis_statistics', request.node.hypothesis_statistics) |
| 91 | + |
| 92 | + request.addfinalizer(finalizer) |
| 93 | + |
| 94 | +def pytest_json_modifyreport(json_report): |
| 95 | + # Deduplicate warnings. These duplicate warnings can cause the file size |
| 96 | + # to become huge. For instance, a warning from np.bool which is emitted |
| 97 | + # every time hypothesis runs (over a million times) causes the warnings |
| 98 | + # JSON for a plain numpy namespace run to be over 500MB. |
| 99 | + |
| 100 | + # This will lose information about what order the warnings were issued in, |
| 101 | + # but that isn't particularly helpful anyway since the warning metadata |
| 102 | + # doesn't store a full stack of where it was issued from. The resulting |
| 103 | + # warnings will be in order of the first time each warning is issued since |
| 104 | + # collections.Counter is ordered just like dict(). |
| 105 | + counted_warnings = Counter([frozenset(i.items()) for i in json_report['warnings']]) |
| 106 | + deduped_warnings = [{**dict(i), 'count': counted_warnings[i]} for i in counted_warnings] |
| 107 | + |
| 108 | + json_report['warnings'] = deduped_warnings |
0 commit comments