Skip to content

POC: start adding "repro snippets" for failed assertions #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions array_api_tests/pytest_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,7 @@ def assert_array_elements(
at_expected = expected[idx]
msg = msg_template.format(sh.fmt_idx(out_repr, idx), at_out, at_expected)
assert at_out == at_expected, msg


def format_snippet(s: str):
return f"\n{'='*10} FAILING CODE SNIPPET:\n{s}\n{'='*20}\n"
41 changes: 23 additions & 18 deletions array_api_tests/test_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,29 @@ def test_getitem(shape, dtype, data):
note(f"{x=}")
key = data.draw(xps.indices(shape=shape, allow_newaxis=True), label="key")

out = x[key]

ph.assert_dtype("__getitem__", in_dtype=x.dtype, out_dtype=out.dtype)
_key = normalize_key(key, shape)
axes_indices, expected_shape = get_indexed_axes_and_out_shape(_key, shape)
ph.assert_shape("__getitem__", out_shape=out.shape, expected=expected_shape)
out_zero_sided = any(side == 0 for side in expected_shape)
if not zero_sided and not out_zero_sided:
out_obj = []
for idx in product(*axes_indices):
val = obj
for i in idx:
val = val[i]
out_obj.append(val)
out_obj = sh.reshape(out_obj, expected_shape)
expected = xp.asarray(out_obj, dtype=dtype)
ph.assert_array_elements("__getitem__", out=out, expected=expected)

repro_snippet = ph.format_snippet(f"{x!r}[{key!r}]")

try:
out = x[key]

ph.assert_dtype("__getitem__", in_dtype=x.dtype, out_dtype=out.dtype)
_key = normalize_key(key, shape)
axes_indices, expected_shape = get_indexed_axes_and_out_shape(_key, shape)
ph.assert_shape("__getitem__", out_shape=out.shape, expected=expected_shape)
out_zero_sided = any(side == 0 for side in expected_shape)
if not zero_sided and not out_zero_sided:
out_obj = []
for idx in product(*axes_indices):
val = obj
for i in idx:
val = val[i]
out_obj.append(val)
out_obj = sh.reshape(out_obj, expected_shape)
expected = xp.asarray(out_obj, dtype=dtype)
ph.assert_array_elements("__getitem__", out=out, expected=expected)
except Exception as exc:
exc.add_note(repro_snippet)
raise

@pytest.mark.unvectorized
@given(
Expand Down
18 changes: 11 additions & 7 deletions array_api_tests/test_data_type_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,17 @@ def test_finfo(dtype):
# np.float64 and np.asarray(1, dtype=np.float64).dtype are different
xp.asarray(1, dtype=dtype).dtype,
):
out = xp.finfo(arg)
assert isinstance(out.bits, int)
assert isinstance(out.eps, float)
assert isinstance(out.max, float)
assert isinstance(out.min, float)
assert isinstance(out.smallest_normal, float)

repro_snippet = ph.format_snippet(f"xp.finfo({arg})")
try:
out = xp.finfo(arg)
assert isinstance(out.bits, int)
assert isinstance(out.eps, float)
assert isinstance(out.max, float)
assert isinstance(out.min, float)
assert isinstance(out.smallest_normal, float)
except Exception as exc:
exc.add_note(repro_snippet)
raise

@pytest.mark.min_version("2022.12")
@pytest.mark.parametrize("dtype", dh.real_float_dtypes + dh.complex_dtypes)
Expand Down
Loading