Skip to content

Raise ParseError for truncated stoch plane arrays - #159

Merged
lispandfound merged 1 commit into
mainfrom
ai/issue-145
Oct 21, 2026
Merged

lispandfound merged 1 commit into
mainfrom
ai/issue-145

Conversation

@claude

@claude claude Bot commented Oct 21, 2026

Copy link
Copy Markdown

Closes #145

What was wrong

_read_stoch_plane read the slip, rise and trup arrays with np.fromfile(..., sep=" ", count=nx*ny).reshape((ny, nx)). If the file ended, or held a token numpy could not parse, partway through an array, np.fromfile returned a shorter array. The reshape then failed with a bare ValueError: cannot reshape array of size K into shape (ny,nx). That error named neither the array nor the problem, and it was not the parse_utils.ParseError that the rest of the parser raises for malformed input.

The StochFile.from_file docstring also said it raises ValueError for a non-positive plane count. The code raises parse_utils.ParseError, which is not a ValueError subclass.

What changed

  • New helper _read_array(handle, header, label) in source_modelling/stoch.py. It reads nx*ny values and raises parse_utils.ParseError(f"Expected {n} values for {label}, got {size}") if it gets fewer. _read_stoch_plane now uses it for slip, rise and trup, and its docstring gains a Raises section.
  • The Raises section of the StochFile.from_file docstring now says parse_utils.ParseError, and covers truncated or malformed data too.

Parsing of well-formed files is unchanged: same dtype, same shape, same cast.

Tests

test_stoch_file_truncated_array_raises_parse_error uses the reproducer from the issue: nx=ny=2, with only 3 trup values. Before the fix it failed with ValueError: cannot reshape array of size 3 into shape (2,2). After the fix it passes, and the error is ParseError("Expected 4 values for trup, got 3"). The full suite passes (801 tests), and ruff check and ruff format --check are clean on the changed files.

For reviewers

🤖 Generated with Claude Code

Closes #145

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@claude claude Bot added the ai-generated Opened by the scheduled Claude jobs label Oct 21, 2026
@claude
claude Bot requested a review from lispandfound October 21, 2026 09:46
@lispandfound
lispandfound merged commit 97e5be0 into main Oct 21, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-generated Opened by the scheduled Claude jobs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stoch.py: StochFile.from_file raises an unhelpful reshape ValueError on truncated or malformed slip/rise/trup arrays, an

1 participant