Skip to content

Commit 65da216

Browse files
committed
fix: Keep a FLOAT range element's spelling built from Python
`StepParameterSpace(taskParameterDefinitions={"F": {"type": "FLOAT", "range": ["1.50"]}})` rendered the task parameter value `1.5`, while the same element decoded from a template rendered `1.50`. Review on #349 found it; measured, 5 of 6 elements disagreed between the two routes: '1.50'->1.5, '1e3'->1000.0, '5.'->5.0, '.5'->0.5, '02.50'->2.5. `task_param_def_from_dict` read every element through `v.str()` and re-emitted the float variant as a JSON *number*. `Float64` reads a JSON number as a bare value and a JSON string as a preserved spelling, so the number form threw the decimal places away -- the thing openjd-rs#354 added `Float64` to carry. The template makes this distinction with the `<float> | <floatstring>` union, so the binding now makes it the same way: an element that arrives as a Python `str` is forwarded as a JSON string, anything else keeps the numeric path. `v.str()` collapsed the two, which is why the fix is a branch rather than a cast. Deliberately not widened: - A Python `float` must not acquire a spelling from `str()`. Measured, both routes already agree on 1.5, 1000.0 and 0.5, and forwarding '1000.0' as text would have been a new divergence in the other direction. - An unparseable element still fails, in `Float64`'s deserializer rather than here, exactly as it did when this re-emitted a number. - `FloatTaskParameter.range` stays `list[float]`. It is the numeric introspection view, so '2.50' still reports 2.5 there while rendering 2.50. Two of them can compare equal by `.range` and render differently; the spec now says so rather than the API changing shape. - A redundant leading zero is still not stripped here. That is a `create_job` normalization (openjd-model `create_job/ranges.rs`), not part of reading a resolved value, and constructing the Rust `TaskParameter` directly behaves the same way -- `Float64`'s deserializer trims whitespace and unsigns zero but does not strip. So '02.50' given straight to the resolved type keeps its zero. Pinned by name. `TestFloatRangeSpellingIsPreserved` covers it: 10 string spellings, 3 float negative controls, agreement with the template path, the leading-zero divergence, and the rejection path. Mutation-checked by flipping the new branch back to the numeric path, which fails 10 of the 16. The two survivors are labelled in the test as not pinning the fix: '2.5' renders the same either way, and ' 2.50 ' already took the string path because Rust's f64 parse rejects the spaces. Separately measured against openjd-rs#354 itself: all 26 input/expected pairs from that PR's own assertions render identically through both this repo's implementations, v0 and v1 -- including '-0.0'->0.0, '0e5', '-0.0E+2'->0.0E+2, '1e-400', a 403-character 0.000...1, '5.' and '.5'. Verified: 5567 passed, 24 skipped, 3 xfailed with the 94% coverage gate enforced; cargo clippy --all-targets -D warnings, cargo fmt --check, cargo test, ruff, black and mypy all clean. Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
1 parent bfa28bc commit 65da216

3 files changed

Lines changed: 126 additions & 3 deletions

File tree

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -899,15 +899,25 @@ fn task_param_def_from_dict(
899899
// List of values — coerce each to the variant's element type.
900900
let mut items: Vec<serde_json::Value> = Vec::with_capacity(list.len());
901901
for v in list.iter() {
902-
let s: String = match v.extract::<String>() {
903-
Ok(s) => s,
904-
Err(_) => v.str()?.extract()?,
902+
// Whether the element arrived as a Python `str` decides which member of
903+
// the template's own `<float> | <floatstring>` union it is, and a
904+
// `<floatstring>` has to keep the spelling it was written with (§7.5).
905+
// `Float64` reads a JSON string as that spelling and a JSON number as a
906+
// bare value, so the distinction has to survive to here -- `v.str()`
907+
// collapses it.
908+
let (s, is_text): (String, bool) = match v.extract::<String>() {
909+
Ok(s) => (s, true),
910+
Err(_) => (v.str()?.extract()?, false),
905911
};
906912
items.push(match variant {
907913
"int" | "chunkInt" => match s.parse::<i64>() {
908914
Ok(n) => serde_json::Value::Number(n.into()),
909915
Err(_) => serde_json::Value::String(s),
910916
},
917+
// The text carries as-is. `Float64`'s deserializer parses it, and
918+
// rejects an unparseable one there rather than here -- which is also
919+
// what happened when this re-emitted a number.
920+
"float" if is_text => serde_json::Value::String(s),
911921
"float" => match s.parse::<f64>() {
912922
Ok(n) => serde_json::Number::from_f64(n)
913923
.map(serde_json::Value::Number)

‎specs/python-model-interface.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,24 @@ does. (The underlying Rust struct has `chunks: Option<ResolvedChunks>`
718718
on the `Int` variant for shape reasons, but no resolver path ever
719719
populates it; the binding mirrors the runtime *behaviour*.)
720720

721+
`FloatTaskParameter.range` is numeric, so it does not show the decimal
722+
places a `<floatstring>` range element was written with. A range element
723+
`'2.50'` reports `2.5` here and renders `2.50` as the task parameter
724+
value, which is the form that reaches a command line (Template Schemas
725+
§7.5). Two `FloatTaskParameter`s that compare equal by `range` can
726+
therefore render different task values. Read
727+
`StepParameterSpaceIterator` for the rendered form.
728+
729+
Constructing a space directly follows the same rule as the template: a
730+
range element given as a `str` is a `<floatstring>` and keeps its
731+
spelling, and one given as a `float` is a `<float>` and renders as the
732+
number. `StepParameterSpace(taskParameterDefinitions={"F": {"type":
733+
"FLOAT", "range": ["1.50"]}})` renders `1.50`, and `range=[1.5]` renders
734+
`1.5`. Stripping a redundant leading zero is a `create_job`
735+
normalization rather than part of reading a resolved value, so `'02.50'`
736+
given directly to the constructor keeps its zero where the same element
737+
in a template does not.
738+
721739
### `ChunkIntTaskParameter`
722740

723741
Available only when the `TASK_CHUNKING` extension is enabled.

‎test/openjd/model_v1/test_step_param_space_iter.py‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,101 @@ def test_len_raises_on_adaptive_chunking(self) -> None:
563563
len(it)
564564

565565

566+
class TestFloatRangeSpellingIsPreserved:
567+
"""A FLOAT range element given as a ``str`` keeps the decimal places it was
568+
written with, so the value reaching a command line is the one the caller asked
569+
for (Template Schemas §7.5, openjd-rs#354).
570+
571+
``StepParameterSpace`` builds the *resolved* space directly, without the
572+
``decode_job_template`` + ``create_job`` path, so it is its own route to a
573+
``Float64`` and has to make the same choice: the template's range element is a
574+
``<float> | <floatstring>`` union, and only the string member carries a
575+
spelling. Before openjd-model 0.6.0 there was no spelling to carry -- the
576+
resolved range was ``Vec<f64>`` -- so this could not be observed.
577+
"""
578+
579+
@staticmethod
580+
def _rendered(elements: list[Any]) -> list[str]:
581+
space = StepParameterSpace(
582+
taskParameterDefinitions={"F": {"type": "FLOAT", "range": elements}}
583+
)
584+
return [params["F"].value for params in StepParameterSpaceIterator(space=space)]
585+
586+
@pytest.mark.parametrize(
587+
"element,expected",
588+
[
589+
("1.50", "1.50"), # the trailing zero is the requested scale
590+
("3.500", "3.500"),
591+
("1e3", "1e3"), # exponent notation is not expanded
592+
("1E+2", "1E+2"),
593+
("5.", "5."), # no digit is invented after the point
594+
(".5", ".5"), # nor before it
595+
("0.50", "0.50"),
596+
("-0.00", "0.00"), # zero has no sign, but keeps its places
597+
# The last two do not pin the fix -- both render the same without it, and
598+
# both survive the mutant that reverts it. Kept as controls: canonical text
599+
# must not change, and padded text must still be trimmed (it took the
600+
# string path already, because Rust's `f64` parse rejects the spaces).
601+
("2.5", "2.5"),
602+
(" 2.50 ", "2.50"),
603+
],
604+
)
605+
def test_a_string_element_keeps_its_spelling(self, element: str, expected: str) -> None:
606+
assert self._rendered([element]) == [expected]
607+
608+
@pytest.mark.parametrize("element,expected", [(1.5, "1.5"), (1000.0, "1000.0"), (0.5, "0.5")])
609+
def test_a_float_element_renders_as_the_number(self, element: float, expected: str) -> None:
610+
"""The negative control. A Python ``float`` is the union's ``<float>`` member and
611+
has no spelling to keep, so it must not acquire one from ``str()`` -- these are
612+
the values where preserving the text would have been indistinguishable from
613+
rendering the number, and they must stay that way."""
614+
assert self._rendered([element]) == [expected]
615+
616+
def test_it_matches_what_the_template_path_renders(self) -> None:
617+
"""The two routes to a resolved space agree, which is the point of the fix:
618+
before it, this space rendered ``1.5`` where the template rendered ``1.50``."""
619+
elements = ["1.50", "1e3", "5.", ".5", "2.5"]
620+
template = {
621+
"specificationVersion": "jobtemplate-2023-09",
622+
"name": "T",
623+
"steps": [
624+
{
625+
"name": "S",
626+
"parameterSpace": {
627+
"taskParameterDefinitions": [
628+
{"name": "F", "type": "FLOAT", "range": elements}
629+
]
630+
},
631+
"script": {
632+
"actions": {"onRun": {"command": "echo", "args": ["{{Task.Param.F}}"]}}
633+
},
634+
}
635+
],
636+
}
637+
step = create_job(
638+
job_template=decode_job_template(template=template), job_parameter_values={}
639+
).steps[0]
640+
from_template = [params["F"].value for params in StepParameterSpaceIterator(step=step)]
641+
assert self._rendered(elements) == from_template == elements
642+
643+
def test_a_redundant_leading_zero_is_not_stripped_here(self) -> None:
644+
"""The one place the two routes still differ, and deliberately. Stripping a
645+
redundant leading zero is a ``create_job`` normalization (openjd-model
646+
``create_job/ranges.rs``), not part of reading a resolved value, so the
647+
template path renders ``2.50`` while a value handed straight to the resolved
648+
type keeps the ``0``. Constructing the Rust ``TaskParameter`` directly behaves
649+
the same way, because ``Float64``'s deserializer trims whitespace and unsigns
650+
zero but does not strip.
651+
"""
652+
assert self._rendered(["02.50"]) == ["02.50"]
653+
654+
def test_an_unparseable_element_is_still_rejected(self) -> None:
655+
"""Forwarding the text rather than a number must not turn a bad element into an
656+
accepted one; it fails in ``Float64``'s deserializer instead of here."""
657+
with pytest.raises(ValueError, match="not-a-float"):
658+
self._rendered(["not-a-float"])
659+
660+
566661
class TestChunkIntContains:
567662
"""``__contains__`` must round-trip values yielded by a chunked
568663
iterator. The iterator yields ``TaskParameterValue`` instances

0 commit comments

Comments
 (0)