Skip to content

Commit ccf4cba

Browse files
committed
fix: trim a floatstring range element and unsign a zero
Two review findings, both cases where the forwarded text did not match what openjd-rs renders for the same element. float() ignores surrounding whitespace but the regex does not, so ' 1.5 ' was forwarded with its spaces and reached a command line as --frame ' 1.5 '. openjd-rs trims the resolved element before keeping it; do the same here. Zero has no sign. '-0.0' rendered -0.0 here and 0.0 on mainline and in openjd-rs, which is the reading the deleted test pinned by name. Drop the sign without dropping the decimal places, so '-0.00' renders 0.00. Text that reaches zero only by underflow, like '1e-400', does not render the value and is not kept -- which also covers the huge negative exponents, since '1e-999999999' underflows to zero. Both now agree with openjd-rs#354 on every case measured: ' 1.5 '->1.5, '-0.0'->0.0, '-0.00'->0.00, '1e-400'->0.0, '5.'->5., '.5'->.5, '+2.50'->+2.50, '1E+2'->1E+2. Also soften a test comment that claimed an element can no longer exceed TaskParameterStringValueAsJob's cap and fall through to a numeric member of the union. That holds for the huge-exponent inputs the test parametrizes, but long literal text still can, on this branch and on mainline and 0.11.6 alike -- only the expansion is gone. Verification. Model suite 5533 passed, 24 skipped, 3 xfailed; ruff, black and mypy clean. Full 2023-09 conformance 1162 passed, 0 failed. Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
1 parent 51990e9 commit ccf4cba

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

‎src/openjd/model/v2023_09/_model.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,12 +1262,21 @@ def _normalized_range_element(elem: str, to_int: bool) -> Any:
12621262
return int(elem) # int() already drops leading zeros
12631263
# Parsed only to check it is a number; the text is what renders. Not a
12641264
# Decimal -- re-rendering one is context-sensitive and unbounded (§7.5).
1265-
float(elem)
1265+
value = float(elem)
12661266
except ValueError:
12671267
# A resolved format string can be non-numeric. Literals are checked at
12681268
# template parse time, so carry it through rather than rejecting here.
12691269
return elem
1270-
return _REDUNDANT_LEADING_ZEROS.sub(r"\1", elem)
1270+
# Trimmed because the text reaches a command line and float() ignores
1271+
# surrounding whitespace; openjd-rs trims here too.
1272+
text = _REDUNDANT_LEADING_ZEROS.sub(r"\1", elem.strip())
1273+
if value == 0.0:
1274+
# Zero has no sign, so drop one without dropping the decimal places:
1275+
# '-0.00' renders `0.00`. Text that reaches zero only by underflow, like
1276+
# '1e-400', does not render the value at all and is not kept.
1277+
unsigned = text.lstrip("+-")
1278+
return unsigned if set(unsigned) <= {"0", "."} else "0.0"
1279+
return text
12711280

12721281

12731282
# Target model for task parameters when instantiating a job.

‎test/openjd/model_v0/v2023_09/test_parameter_space.py‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,16 @@ def test_intstring_elements_carry_their_value(self, param_type: str) -> None:
485485
pytest.param("0", "0", id="bare zero"),
486486
pytest.param("0.0", "0.0", id="zero"),
487487
pytest.param("0.00", "0.00", id="zero keeps its trailing zeros too"),
488+
# Zero has no sign, which openjd-rs and mainline both render away.
489+
# Dropping it must not drop the decimal places with it.
490+
pytest.param("-0.0", "0.0", id="negative zero has no sign"),
491+
pytest.param("-0.00", "0.00", id="unsigned, but still two places"),
492+
# Reaches zero only by underflow, so the text does not render the
493+
# value and is not kept.
494+
pytest.param("1e-400", "0.0", id="underflow to zero"),
495+
# float() ignores surrounding whitespace and this text reaches a
496+
# command line, so it is trimmed rather than forwarded.
497+
pytest.param(" 1.5 ", "1.5", id="surrounding whitespace"),
488498
pytest.param("1.0", "1.0", id="integral float keeps its point"),
489499
# Exponent notation is the author's own text and is forwarded. The
490500
# FLOAT parameter default path does the same for `default: "1E+2"`.
@@ -508,25 +518,33 @@ def test_floatstring_elements_keep_their_scale(self, element: str, expected: str
508518
assert [str(v) for v in model.range] == [expected]
509519

510520
@pytest.mark.parametrize(
511-
"element",
521+
"element,expected",
512522
(
513-
pytest.param("1e999999999", id="huge positive exponent"),
514-
pytest.param("1e-999999999", id="huge negative exponent"),
515-
pytest.param("1E+1022", id="positive exponent past the field cap"),
516-
pytest.param("1E-1023", id="negative exponent past the field cap"),
523+
# An exponent too large for a float still costs only its own
524+
# characters, which is the whole point: nothing expands it.
525+
pytest.param("1e999999999", "1e999999999", id="huge positive exponent"),
526+
pytest.param("1E+1022", "1E+1022", id="positive exponent past the field cap"),
527+
# Too small, and the value underflows to zero, so the text no longer
528+
# renders it. openjd-rs resolves these to zero and renders 0.0 too.
529+
pytest.param("1e-999999999", "0.0", id="huge negative exponent"),
530+
pytest.param("1E-1023", "0.0", id="negative exponent past the field cap"),
517531
),
518532
)
519-
def test_a_huge_exponent_costs_only_its_own_characters(self, element: str) -> None:
533+
def test_a_huge_exponent_costs_only_its_own_characters(
534+
self, element: str, expected: str
535+
) -> None:
520536
# WHEN 11 characters of template text denote a number needing ~10**9
521537
# digits in plain notation
522538
model = _parse_model(
523539
model=RangeListTaskParameterDefinition, obj={"type": "FLOAT", "range": [element]}
524540
)
525541

526-
# THEN it renders as its own text. Nothing expands it, so no exponent
527-
# bound is needed and the value cannot exceed the field's 1024-character
528-
# cap and fall through to a numeric member of the union.
529-
assert [str(v) for v in model.range] == [element]
542+
# THEN nothing expands it, so no bound on the exponent is needed to stop a
543+
# template allocating ~10**9 characters. Long *literal* text can still
544+
# exceed TaskParameterStringValueAsJob's cap and fall through to a numeric
545+
# member of the union, as it does on mainline and in 0.11.6; only the
546+
# expansion is gone.
547+
assert [str(v) for v in model.range] == [expected]
530548

531549
def test_floatstring_rendering_ignores_the_decimal_context(self) -> None:
532550
# GIVEN an embedding application that has narrowed the process-wide

0 commit comments

Comments
 (0)