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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ENH: one more example of the failure reporting
$ ARRAY_API_TESTS_MODULE=array_api_compat.torch pytest array_api_tests/test_array_object.py::test_getitem
  • Loading branch information
ev-br committed Jun 6, 2025
commit 4bb094920474fb70ca7fe8290dca259fd246a8a7
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