fix: fix t_array setter ValueError when passing numpy array#732
Conversation
The setter compared t_array == 'auto' which raises ValueError when t_array is a numpy array. Use isinstance() check instead.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #732 +/- ##
=======================================
Coverage 74.82% 74.82%
=======================================
Files 56 56
Lines 8095 8095
Branches 1577 1577
=======================================
Hits 6057 6057
Misses 1422 1422
Partials 616 616
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Hi @wangwangbobo, Thanks for the PR. This looks good. Can you add a short test case for the fix please? Best, |
waltsims
left a comment
There was a problem hiding this comment.
LGTM. The isinstance guard correctly short-circuits the numpy ambiguous-truth-value crash, the existing 'auto' string path is preserved, and the rest of the setter is untouched. Greptile 5/5, 0 unresolved.
|
Thanks for the quick merge, Walter! 🙏 The test case is a good idea — I'll add one in a follow-up. The fix is straightforward (swapping for ), so a simple test passing a numpy array to the setter should cover it. Will submit a PR with the test shortly. 👍 |
Fixes the t_array setter which raises
ValueError: The truth value of an array with more than one element is ambiguouswhen trying to set a numpy array.The issue was that
t_array == 'auto'returns a boolean array whent_arrayis a numpy array, causing theifstatement to fail.Fixed by using
isinstance(t_array, str) and t_array == 'auto'instead.Greptile Summary
This PR fixes a
ValueError: The truth value of an array with more than one element is ambiguouscrash in thet_arraysetter by guarding the== "auto"comparison with anisinstance(t_array, str)check, which short-circuits before NumPy can produce a boolean array.kwave/kgrid.pyreplacesif t_array == "auto":withif isinstance(t_array, str) and t_array == "auto":, correctly handling both string ("auto") and numpy array inputs.self.Nt == "auto"/self.dt == "auto"comparisons in the getter (line 100) remain unaffected because those attributes are always plain strings or scalars, not numpy arrays.Confidence Score: 5/5
Safe to merge — the change is a minimal, well-targeted guard that fixes a crash without altering any other behavior.
The fix is a single-line change that correctly handles both the existing 'auto' string path and the previously broken numpy array path. The isinstance short-circuit is the idiomatic Python solution, the rest of the setter is untouched, and the getter's own 'auto' comparisons operate on plain scalar attributes so they are unaffected.
No files require special attention.
Important Files Changed
Sequence Diagram
sequenceDiagram participant Caller participant t_array setter participant kWaveGrid Caller->>t_array setter: set t_array = numpy_array Note over t_array setter: isinstance(t_array, str) → False<br/>short-circuit: skip "auto" branch t_array setter->>t_array setter: extract Nt_temp, dt_temp t_array setter->>t_array setter: validate (zero start, even spacing, increasing) t_array setter->>kWaveGrid: self.Nt = Nt_temp, self.dt = dt_temp Caller->>t_array setter: set t_array = "auto" Note over t_array setter: isinstance(t_array, str) → True<br/>t_array == "auto" → True t_array setter->>kWaveGrid: self.Nt = "auto", self.dt = "auto"Reviews (1): Last reviewed commit: "fix: fix t_array setter ValueError when ..." | Re-trigger Greptile