Skip to content

Commit c476cf8

Browse files
committed
fix: Name the over-long binding in the let identifier error
Review finding on #348. The validator is a `field_validator` on the whole `let` list, so pydantic anchors the error at `steps[0] -> let` with no list index. Measured on a four-binding template where the third name is 513 characters: the error was steps[0] -> let: A 'let' binding name must be at most 512 characters long which identifies neither the binding nor its position. With 50 bindings allowed that is not diagnosable. Truncate rather than omit, as the reviewer suggested. The message now carries a 32-character prefix and the true length: A 'let' binding name must be at most 512 characters long: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'... (513 characters) Total error text goes from 110 to 166 characters, so it stays bounded while becoming locatable. The original concern about a 513-character name dwarfing the diagnostic still holds against interpolating the name in full, which is why this truncates. Two assertions pin it, both mutation-checked: reverting to the bare message fails `test_name_513_chars` and the new `test_name_513_chars_names_the_offending_binding`, which uses a multi-binding `let` to cover the case the finding was about. No matching change in openjd-rs: its path is `steps[0] -> let[0]`, so the index already identifies the binding there. Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
1 parent ab33552 commit c476cf8

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,10 +946,13 @@ def parse_let_bindings(value: Any) -> list[tuple[str, str]]:
946946
expr = expr.strip()
947947
if not _LET_NAME_RE.match(name):
948948
raise ValueError(f"A 'let' binding name must be a valid identifier: {name!r}")
949-
# Name omitted from the message; at 513 characters it would dwarf it.
949+
# Truncated rather than omitted: the caller is a field_validator on the
950+
# whole list, so the error path is `let` with no index to identify which
951+
# binding is over.
950952
if len(name) > LET_MAX_IDENTIFIER_LEN:
951953
raise ValueError(
952-
f"A 'let' binding name must be at most {LET_MAX_IDENTIFIER_LEN} characters long"
954+
f"A 'let' binding name must be at most {LET_MAX_IDENTIFIER_LEN} "
955+
f"characters long: {name[:32]!r}... ({len(name)} characters)"
953956
)
954957
if not expr:
955958
raise ValueError(f"A 'let' binding must define an expression: {binding!r}")

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,19 @@ def test_self_reference(self):
121121
# alone, so these pin the cap independently of FEATURE_BUNDLE_1.
122122
def test_name_513_chars(self):
123123
name = "a" * 513
124-
with pytest.raises(DecodeValidationError, match="at most 512 characters"):
124+
with pytest.raises(
125+
DecodeValidationError,
126+
match=r"at most 512 characters long: 'a{32}'\.\.\. \(513 characters\)",
127+
):
125128
_decode(_job([{"name": "S", "let": [f"{name} = 1"], "script": _onrun("hi")}]))
126129

130+
def test_name_513_chars_names_the_offending_binding(self):
131+
# The validator is a field_validator on the whole list, so the error path
132+
# is `let` with no index; the message has to identify the binding itself.
133+
name = "b" * 513
134+
with pytest.raises(DecodeValidationError, match=r"'b{32}'\.\.\. \(513 characters\)"):
135+
_decode(_job([{"name": "S", "let": ["ok = 1", f"{name} = 2"], "script": _onrun("hi")}]))
136+
127137
def test_name_513_chars_with_fb1(self):
128138
name = "a" * 513
129139
with pytest.raises(DecodeValidationError, match="at most 512 characters"):

0 commit comments

Comments
 (0)