Skip to content

Commit e152030

Browse files
committed
fix(model): Report every non-positive chunk override as ValueError
Signed-off-by: David Leong <leongdl@amazon.com>
1 parent 6b3d9be commit e152030

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

‎rust-bindings/src/model/step_param_space.rs‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,24 @@ impl PyStepParameterSpaceIterator {
149149
fn new(
150150
step: Option<&PyStep>,
151151
space: Option<&PyStepParameterSpace>,
152-
chunks_task_count_override: Option<usize>,
152+
chunks_task_count_override: Option<i64>,
153153
) -> PyResult<Self> {
154-
// Rejected rather than clamped: openjd-model applies `.max(1)` to the
155-
// override, so 0 would silently mean 1. The `chunks_default_task_count`
156-
// setter already refuses 0, and the two should not disagree.
157-
if chunks_task_count_override == Some(0) {
158-
return Err(pyo3::exceptions::PyValueError::new_err(
159-
"chunks_task_count_override must be a positive integer.",
160-
));
161-
}
154+
// Taken as i64, not usize, so a negative value reaches this check instead of
155+
// failing pyo3's unsigned extraction with OverflowError. Every non-positive
156+
// value should report the same ValueError the message and the spec promise.
157+
//
158+
// Rejected rather than clamped: openjd-model applies `.max(1)` to the override,
159+
// so 0 would silently mean 1. The `chunks_default_task_count` setter already
160+
// refuses 0, and the two should not disagree.
161+
let chunks_task_count_override: Option<usize> = match chunks_task_count_override {
162+
Some(n) if n <= 0 => {
163+
return Err(pyo3::exceptions::PyValueError::new_err(
164+
"chunks_task_count_override must be a positive integer.",
165+
));
166+
}
167+
Some(n) => Some(n as usize),
168+
None => None,
169+
};
162170
let ps = if let Some(s) = space {
163171
s.inner.clone()
164172
} else if let Some(st) = step {

‎specs/python-model-interface.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,8 +1158,12 @@ space can be walked at a granularity the caller picks. Pass `1` to
11581158
iterate individual tasks — a `1-20` range chunked five at a time then
11591159
yields `1-1`, `2-2`, … instead of `1-5`, `6-10`, …. It is ignored when
11601160
the space has no chunked parameter, matching the pure-Python reference.
1161-
Both iteration and indexing observe it, and `len()` counts the
1162-
overridden granularity.
1161+
Iteration observes it, and `len()` counts the overridden granularity.
1162+
1163+
Indexing observes it too, but only for a space that supports random
1164+
access. A `CONTIGUOUS` chunked space requires sequential iteration, so
1165+
`it[i]` raises `IndexError` for any index — with or without the
1166+
override — even though `len(it)` reports a count.
11631167

11641168
This is the only way to change the chunk size of a *static* chunked
11651169
space: the `chunks_default_task_count` setter accepts adaptive spaces

‎test/openjd/model_v1/test_step_param_space_iter.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,19 @@ def test_override_is_ignored_when_the_space_has_no_chunked_parameter(self) -> No
788788
assert it.chunks_default_task_count is None
789789
assert it.chunks_parameter_name is None
790790

791-
def test_override_of_zero_is_rejected(self) -> None:
792-
"""openjd-model clamps the override to at least 1, so 0 would silently mean
793-
1. The ``chunks_default_task_count`` setter already refuses 0."""
791+
@pytest.mark.parametrize("override", [0, -1, -5])
792+
def test_a_non_positive_override_is_rejected_as_a_value_error(self, override: int) -> None:
793+
"""openjd-model clamps the override to at least 1, so 0 would silently mean 1, and
794+
the ``chunks_default_task_count`` setter already refuses 0.
795+
796+
Negatives report the same ``ValueError`` rather than the ``OverflowError`` an
797+
unsigned extraction would raise, so one ``except ValueError`` covers every
798+
non-positive input.
799+
"""
794800
with pytest.raises(ValueError) as excinfo:
795-
StepParameterSpaceIterator(step=self._step(self._STATIC), chunks_task_count_override=0)
801+
StepParameterSpaceIterator(
802+
step=self._step(self._STATIC), chunks_task_count_override=override
803+
)
796804
assert str(excinfo.value) == "chunks_task_count_override must be a positive integer."
797805

798806
def test_the_setter_still_refuses_a_static_space(self) -> None:

0 commit comments

Comments
 (0)