Skip to content

fix(numpy): do not mutate input edges; validate histogramdd dimensions#1144

Draft
henryiii wants to merge 1 commit into
developfrom
fix/numpy-input-mutation
Draft

fix(numpy): do not mutate input edges; validate histogramdd dimensions#1144
henryiii wants to merge 1 commit into
developfrom
fix/numpy-input-mutation

Conversation

@henryiii

Copy link
Copy Markdown
Member

🤖 AI text below 🤖

Part of #1143.

Fixes three findings in src/boost_histogram/numpy.py:

  • B3 (caller data mutation): histogramdd (and therefore histogram/histogram2d) used np.asarray(b, dtype=np.double) on user-supplied bin edges, which aliases an already-float64 input. The subsequent in-place np.nextafter adjustment of the last edge then mutated the caller's array (e.g. edges = np.array([0., 1., 2.]); bh.numpy.histogram([0.5, 1.5], bins=edges) left edges[-1] == 2.0000000000000004). np.histogram never mutates its input. The edges are now always copied before adjustment.

  • B13 (silent truncation): histogramdd zipped bins and range with strict=False, silently truncating on length mismatch: bh.numpy.histogramdd((x, y), bins=(2, 2, 2)) returned a 2-D result where np.histogramdd raises, and a too-short range failed later with an unhelpful "Wrong number of args". Lengths are now validated up front, raising ValueError with the same messages as np.histogramdd ("The dimension of bins must be equal to the dimension of the sample x." / "range argument must have one entry per dimension").

  • Dead version check (D2, numpy part): removed the unreachable numpy < 1.13 check ("Upgrade numpy to 1.13+") in the string-bins branch of histogrampyproject.toml requires numpy>=1.21.3 — along with the obsolete "Bug in NumPy 1.20 typing" comment; np.histogram_bin_edges is now called directly (mypy strict remains clean).

Regression tests added in tests/test_numpy_interface.py: input-edges non-mutation for histogram and histogramdd, and mismatched bins/range raising ValueError in lockstep with np.histogramdd.

🤖 Generated with Claude Code

- histogram/histogramdd no longer mutate a caller-provided float64 edges
  array in place when applying the nextafter upper-edge adjustment;
  the edges are now always copied (B3 in #1143).
- histogramdd now validates that the lengths of bins and range match the
  sample rank up front, raising the same ValueError messages as
  np.histogramdd instead of silently truncating via zip (B13 in #1143).
- Remove a dead numpy<1.13 version check and the obsolete NumPy 1.20
  typing workaround comment in the string-bins branch of histogram.

Part of #1143

Assisted-by: ClaudeCode:claude-fable-5
@github-actions github-actions Bot added the needs changelog Might need a changelog entry label Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Might need a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant