Repository navigation
fix(ts): reject a null choice label, as Python does (#508 parity) - #710
NandhaKishorM merged 1 commit into
Conversation
…parity) A choice question given its criteria as a list uses each label as an object key and as the answer key. Python rejects a null label (NandhaKishorM#508): its answer key comes back as the string "null", which a client cannot tell apart from a real "null" label, and `criteria[answer.choice]` never finds it. The TypeScript checkQuestion accepted null and undefined (and a hole in a sparse list, which forEach skipped), so the same undecodable answer key reached the caller. Reject a null, undefined or missing label with a message naming the question and the index, and say "string, number or bool" in the nested-label message, as Python now does. Dict criteria and the empty string are unchanged. This is a parity fix; it makes no prediction-accuracy claim. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Bruce-Yii
left a comment
There was a problem hiding this comment.
I checked the Python side on main (laya/agent.py:653-668) against this diff. The layer and the moment line up — checkQuestion runs immediately before toInternal, the same relationship _check_question has to _to_internal — and the hole handling is worth having on its own: on main, ["a", , "b"] passes checkQuestion because forEach skips the hole, and then toInternal dies with TypeError: Iterator value undefined is not an entry object, naming neither the question nor the label. Two things before merge.
The message drops the clause that explains the rejection. Python raises:
question 'q': choice label 1 is null; a label is rendered as option text and used as the answer key, so it must be a string, number or bool -- a null label renders as the text "None" while its answer key is "null"
This stops at bool. That -- clause is the only part telling a caller why a label which used to be accepted is now an error, and the test matches just the prefix, so nothing catches the omission.
The mechanism in the description does not occur in this runtime. Python's render_options renders with str(k), so a null label's option text is "None" while the JSON key is "null" — the two disagree, which is what makes the answer undecodable. Here toInternal uses Object.fromEntries, which applies ToPropertyKey, so the key is already "null"; renderOptions reads Object.entries, so the text is "null"; and choice comes from Object.keys(q.crit), also "null". I measured ["billing", null] on main: crit = {"billing":null,"null":null}, options = ["billing","null"]. Nothing disagrees internally.
The argument that does hold is the next sentence in your own description: the string "null" and JSON null produce the same key, so a client cannot tell them apart, and after this change only the string can — which makes label-to-key injective. I'd lead with that, and keep the behaviour change explicit, because a null label that round-trips fine today stops being accepted.
Two notes, not blocking. This is stacked on #711, and git merge-tree of the two heads produces exactly #711's tree, so whichever order they are reviewed or merged in, one of them is a no-op — worth deciding which single PR carries this. And moving the nested-label wording from "string, number or null" to "string, number or bool" moves the TS toward main, which already says bool; that part is right.
|
Passed checks and merged. Thank you @Divit-aggarwal! It ships in 0.3.22. |
What
checkQuestioninlaya-tsnow rejects anullorundefinedlabel in a choice question'scriterialist (including a hole in a sparse list), with an error that names the question and the index, as Python does since #508.Why
A list-form label becomes the answer key. A null label comes back as the key
"null", which a client cannot tell apart from a real"null"label, andcriteria[answer.choice]never finds it:The nested-label message now says "string, number or bool" rather than "… or null", matching Python. This reverses one assertion from #551, which accepted
nullbefore #508 landed.How it was verified
The two Python commands are the CI gates; no Python was changed. The new test fails on
mainand passes with this change.Scope
checkQuestionand its test""are unchangedRelated: #508 (the Python change). #496's TS port is stacked on this branch.
Written with Claude Code; I've reviewed the change and re-run the verification myself.
🤖 Generated with Claude Code