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 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
Next Next commit
ENH: start adding "repro snippets" for failed assertions
It is not always easy to tell what exactly failed with hypothesis (gh-379).
Thus add a failing snippet to the exceptions' output.
  • Loading branch information
ev-br committed Jun 6, 2025
commit 0744535bb580d24ec7e26d3119ff7084cdf673e0
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"
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
66 changes: 37 additions & 29 deletions array_api_tests/test_manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,37 +313,45 @@ def test_repeat(x, kw, data):

assume(n_repititions <= hh.SQRT_MAX_ARRAY_SIZE)

out = xp.repeat(x, repeats, **kw)
ph.assert_dtype("repeat", in_dtype=x.dtype, out_dtype=out.dtype)
if axis is None:
expected_shape = (n_repititions,)
else:
expected_shape = list(shape)
expected_shape[axis] = n_repititions
expected_shape = tuple(expected_shape)
ph.assert_shape("repeat", out_shape=out.shape, expected=expected_shape)
repro_snippet = ph.format_snippet(f"xp.repeat({x!r},{repeats!r}, **kw) with {kw=}")
try:
out = xp.repeat(x, repeats, **kw)

# Test values
ph.assert_dtype("repeat", in_dtype=x.dtype, out_dtype=out.dtype)
if axis is None:
expected_shape = (n_repititions,)
else:
expected_shape = list(shape)
expected_shape[axis] = n_repititions
expected_shape = tuple(expected_shape)
ph.assert_shape("repeat", out_shape=out.shape, expected=expected_shape)

# Test values

if isinstance(repeats, int):
repeats_array = xp.full(size, repeats, dtype=xp.int32)
else:
repeats_array = repeats

if kw.get("axis") is None:
x = xp.reshape(x, (-1,))
axis = 0

for idx, in sh.iter_indices(x.shape, skip_axes=axis):
x_slice = x[idx]
out_slice = out[idx]
start = 0
for i, count in enumerate(repeats_array):
end = start + count
ph.assert_array_elements("repeat", out=out_slice[start:end],
expected=xp.full((count,), x_slice[i], dtype=x.dtype),
kw=kw)
start = end

except Exception as exc:
exc.add_note(repro_snippet)
raise

if isinstance(repeats, int):
repeats_array = xp.full(size, repeats, dtype=xp.int32)
else:
repeats_array = repeats

if kw.get("axis") is None:
x = xp.reshape(x, (-1,))
axis = 0

for idx, in sh.iter_indices(x.shape, skip_axes=axis):
x_slice = x[idx]
out_slice = out[idx]
start = 0
for i, count in enumerate(repeats_array):
end = start + count
ph.assert_array_elements("repeat", out=out_slice[start:end],
expected=xp.full((count,), x_slice[i], dtype=x.dtype),
kw=kw)
start = end

reshape_shape = st.shared(hh.shapes(), key="reshape_shape")

Expand Down