Skip to content

Commit 105dff2

Browse files
authored
fix: coerce LIST[BOOL] items per RFC 0007 §2.15 (#352)
fix: coerce LIST[BOOL] items per RFC 0007 §2.15 Each LIST[BOOL] item accepts the same values as scalar BOOL (bool literals, int/float 0/1, true/yes/on/1, false/no/off/0). Python stored values verbatim, so heterogeneous lists failed expression evaluation with 'List contains incompatible types' and homogeneous non-bool spellings silently produced wrongly-typed lists. Coerce each item to a canonical bool at the job-creation boundary, reusing the scalar BOOL coercion (moved to a version-agnostic _bool_coercion module): template defaults and submitted values (native list and JSON string), gated exactly on LIST_BOOL. This also covers environment-template merged defaults, which bypass model validators via model_copy. Per-item failures carry 'Parameter <name>:' context; JSON parse errors stay verbatim like other list types. Template objects keep the author's original spellings. Behavior change: created-Job LIST[BOOL] values are now canonical booleans (e.g. ["yes","no"] stores and interpolates as true/false), matching openjd-rs. Signed-off-by: Yongzhi Wei <276409147+wyongzhi@users.noreply.github.com>
1 parent b136693 commit 105dff2

5 files changed

Lines changed: 483 additions & 46 deletions

File tree

‎src/openjd/model/_bool_coercion.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
from typing import Any
4+
5+
# Accepted string spellings for boolean defaults/values (case-insensitive),
6+
# per RFC 0007 (BOOL parameter type).
7+
_BOOL_TRUE_STRINGS = frozenset({"true", "yes", "on", "1"})
8+
_BOOL_FALSE_STRINGS = frozenset({"false", "no", "off", "0"})
9+
10+
11+
def _coerce_bool_value(value: Any) -> bool:
12+
"""Coerce an RFC 0007 BOOL value to a Python bool, raising ValueError for
13+
anything outside the accepted set (bool, int 0/1, float 0.0/1.0, or a
14+
case-insensitive true/false/yes/no/on/off/1/0 string).
15+
"""
16+
if isinstance(value, bool):
17+
return value
18+
if isinstance(value, int): # bool already handled above
19+
if value in (0, 1):
20+
return bool(value)
21+
raise ValueError("BOOL value as an integer must be 0 or 1.")
22+
if isinstance(value, float):
23+
if value in (0.0, 1.0):
24+
return bool(value)
25+
raise ValueError("BOOL value as a float must be 0.0 or 1.0.")
26+
if isinstance(value, str):
27+
low = value.lower()
28+
if low in _BOOL_TRUE_STRINGS:
29+
return True
30+
if low in _BOOL_FALSE_STRINGS:
31+
return False
32+
raise ValueError(
33+
"BOOL value as a string must be one of (case-insensitive): "
34+
"true, false, yes, no, on, off, 1, 0."
35+
)
36+
raise ValueError("BOOL value must be a boolean, 0/1, 0.0/1.0, or a boolean string.")

‎src/openjd/model/_create_job.py‎

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from pydantic import ValidationError
1010

11+
from ._bool_coercion import _coerce_bool_value
1112
from ._errors import CompatibilityError, DecodeValidationError
1213
from ._format_strings import FormatStringError
1314
from ._symbol_table import SymbolTable
@@ -63,17 +64,29 @@ class JobWithSymbolTables:
6364
_LEGACY_SCALAR_TYPE_NAMES = frozenset({"STRING", "INT", "FLOAT", "PATH"})
6465

6566

67+
class _ListBoolItemError(ValueError):
68+
"""A LIST[BOOL] per-item coercion failure, distinct from the JSON-level
69+
parse errors shared by all LIST[*] types. The value-collection call site
70+
prefixes only these with the parameter name; JSON/scalar errors stay
71+
verbatim, matching the other list types.
72+
"""
73+
74+
6675
def _coerce_expr_param_value(param_type_name: str, value: Any) -> Any:
6776
"""Coerce a SUBMITTED string value for an EXPR-typed job parameter to its
6877
native form, mirroring openjd-rs's ``coerce_from_str``
6978
(job/create_job/parameters.rs): BOOL accepts the spec's boolean strings,
7079
and LIST[*] values may be supplied as JSON — the public input type is
71-
``dict[str, str]``, so string forms must be accepted. Native values
72-
(bool, list) pass through unchanged.
80+
``dict[str, str]``, so string forms must be accepted. LIST[BOOL] values
81+
are additionally normalized per item (RFC 0007 §2.15): each item accepts
82+
the same values as a scalar BOOL parameter, whether the value arrives as
83+
a native list or as a JSON string. Other native values (bool, list) pass
84+
through unchanged.
7385
7486
Raises:
75-
ValueError: If a string value cannot be coerced (message shapes match
76-
the Rust implementation).
87+
ValueError: If a string value cannot be coerced, or a LIST[BOOL] item
88+
is not a valid boolean (message shapes match the Rust
89+
implementation).
7790
"""
7891
if param_type_name == "BOOL" and isinstance(value, str):
7992
lowered = value.lower()
@@ -91,7 +104,20 @@ def _coerce_expr_param_value(param_type_name: str, value: Any) -> Any:
91104
raise ValueError(f"Value '{value}' is not valid JSON for a list parameter.")
92105
if not isinstance(parsed, list):
93106
raise ValueError(f"Value '{value}' is not valid JSON for a list parameter.")
107+
if param_type_name == "LIST_BOOL":
108+
# §2.15: LIST[BOOL] items accept the same spellings as scalar BOOL; reuse the scalar's coercion so the two can't drift.
109+
try:
110+
return [_coerce_bool_value(item) for item in parsed]
111+
except ValueError as exc:
112+
raise _ListBoolItemError(str(exc)) from exc
94113
return parsed
114+
if param_type_name == "LIST_BOOL" and isinstance(value, list):
115+
# Same §2.15 normalization as the JSON branch above; build a new list,
116+
# never mutate the caller's input.
117+
try:
118+
return [_coerce_bool_value(item) for item in value]
119+
except ValueError as exc:
120+
raise _ListBoolItemError(str(exc)) from exc
95121
return value
96122

97123

@@ -208,8 +234,18 @@ def _collect_defaults_2023_09(
208234
# default through so the typed symbol-table builder can
209235
# coerce it. The PATH-relative-default handling below only
210236
# applies to the scalar PATH type.
237+
default_value: Any = param.default
238+
if param.type.name == "LIST_BOOL" and isinstance(param.default, list):
239+
# Same §2.15 normalization for template defaults. Decode-time
240+
# validation normally guarantees success, but validator-bypassing
241+
# definitions (e.g. model_copy) can still reach here, hence the
242+
# Parameter-name context on failure.
243+
try:
244+
default_value = [_coerce_bool_value(item) for item in param.default]
245+
except ValueError as exc:
246+
raise ValueError(f"Parameter {param.name}: {exc}") from exc
211247
return_value[param.name] = ParameterValue(
212-
type=ParameterValueType(param.type), value=param.default
248+
type=ParameterValueType(param.type), value=default_value
213249
)
214250
continue
215251
default = str(param.default)
@@ -233,7 +269,16 @@ def _collect_defaults_2023_09(
233269
# their native values, then carry through; mirrors
234270
# openjd-rs's coerce_from_str.
235271
# Raises ValueError (collected by the caller) on bad input.
236-
value = _coerce_expr_param_value(param.type.name, value)
272+
try:
273+
value = _coerce_expr_param_value(param.type.name, value)
274+
except _ListBoolItemError as exc:
275+
# RFC 0007 §2.15: per-item coercion runs here during value
276+
# collection, before _check_2023_09/_check_constraints, so
277+
# a per-item failure would surface name-free unless named
278+
# at this call site. JSON-level and scalar errors are plain
279+
# ValueErrors and keep their verbatim (name-free) message,
280+
# matching the other list types.
281+
raise ValueError(f"Parameter {param.name}: {exc}") from exc
237282
return_value[param.name] = ParameterValue(
238283
type=ParameterValueType(param.type), value=value
239284
)
@@ -263,11 +308,11 @@ def _check_2023_09(
263308
for param in job_parameter_definitions:
264309
if param.name in job_parameter_values:
265310
param_value = job_parameter_values[param.name]
266-
# The EXPR-extension LIST[*]/RANGE_EXPR definitions don't implement
267-
# _check_constraints (BOOL and the original scalars do). Their
268-
# template defaults are validated at decode time, and their values
269-
# are type-checked when coerced into the typed EXPR symbol table, so
270-
# skip the create-time constraint check when it isn't available.
311+
# Every 2023_09 job-parameter definition now implements
312+
# _check_constraints: the scalars (STRING/PATH/INT/FLOAT/BOOL), the
313+
# LIST[*] types via _JobListParameterDefinitionBase, and RANGE_EXPR.
314+
# The getattr fallback is retained as defense in case a definition
315+
# type without one is ever added; it currently matches none.
271316
check_constraints = getattr(param, "_check_constraints", None)
272317
if check_constraints is None:
273318
continue

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

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
from .._format_strings import FormatString
3333
from .._errors import ExpressionError, TokenError
34+
from .._bool_coercion import _coerce_bool_value
3435
from .._capabilities import (
3536
validate_amount_capability_name,
3637
validate_attribute_capability_name,
@@ -3755,40 +3756,6 @@ class JobBoolParameterDefinitionUserInterface(OpenJDModel_v2023_09):
37553756
groupLabel: Optional[UserInterfaceLabelStringValue] = None # noqa: N815
37563757

37573758

3758-
# Accepted string spellings for boolean defaults/values (case-insensitive),
3759-
# per RFC 0007 (BOOL parameter type).
3760-
_BOOL_TRUE_STRINGS = frozenset({"true", "yes", "on", "1"})
3761-
_BOOL_FALSE_STRINGS = frozenset({"false", "no", "off", "0"})
3762-
3763-
3764-
def _coerce_bool_value(value: Any) -> bool:
3765-
"""Coerce an RFC 0007 BOOL value to a Python bool, raising ValueError for
3766-
anything outside the accepted set (bool, int 0/1, float 0.0/1.0, or a
3767-
case-insensitive true/false/yes/no/on/off/1/0 string).
3768-
"""
3769-
if isinstance(value, bool):
3770-
return value
3771-
if isinstance(value, int): # bool already handled above
3772-
if value in (0, 1):
3773-
return bool(value)
3774-
raise ValueError("BOOL value as an integer must be 0 or 1.")
3775-
if isinstance(value, float):
3776-
if value in (0.0, 1.0):
3777-
return bool(value)
3778-
raise ValueError("BOOL value as a float must be 0.0 or 1.0.")
3779-
if isinstance(value, str):
3780-
low = value.lower()
3781-
if low in _BOOL_TRUE_STRINGS:
3782-
return True
3783-
if low in _BOOL_FALSE_STRINGS:
3784-
return False
3785-
raise ValueError(
3786-
"BOOL value as a string must be one of (case-insensitive): "
3787-
"true, false, yes, no, on, off, 1, 0."
3788-
)
3789-
raise ValueError("BOOL value must be a boolean, 0/1, 0.0/1.0, or a boolean string.")
3790-
3791-
37923759
class JobBoolParameterDefinition(NameIdentifierLengthMixin, OpenJDModel_v2023_09):
37933760
"""A Job Parameter of type bool (EXPR extension, RFC 0007).
37943761

0 commit comments

Comments
 (0)