Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(common): a choice label with no description came back as a non-string

PerryLink7695e5f3f7ed
diff --git a/laya/common.py b/laya/common.py
index 6c4c426e..683b8a45 100644
--- a/laya/common.py
+++ b/laya/common.py
@@ -52,8 +52,16 @@ def render_options(q: Dict) -> List[str]:
if t != "noul" and "labels" in q:
raise ValueError("labels is only supported for noul questions")
if t == "choice":
- # only None/"" mean "no description"; 0 and False are legitimate criterion values
- return [k if v is None or v == "" else "%s: %s" % (k, render_criterion(v)) for k, v in crit.items()]
+ # only None/"" mean "no description"; 0 and False are legitimate criterion values.
+ # `str(k)` unconditionally: a label with no description is rendered as itself, so an int
+ # label used to come back as an int from a function annotated `-> List[str]` and then
+ # reached `build_sequence`, which calls `.replace` on it and raised an AttributeError
+ # naming neither the question nor the label. With a description the same label already
+ # went through `"%s: %s" %` and was a str, which is why only the undescribed form broke.
+ # `structured._enum_field` stringifies labels the same way; the returned answer still
+ # carries the caller's original label, which is unchanged.
+ return [str(k) if v is None or v == "" else "%s: %s" % (k, render_criterion(v))
+ for k, v in crit.items()]
if t == "score":
return ["level %d: %s" % (i, render_criterion(c)) for i, c in enumerate(crit)]
crit = crit or {}
diff --git a/tests/test_criteria.py b/tests/test_criteria.py
index 738ea87f..32705b4a 100644
--- a/tests/test_criteria.py
+++ b/tests/test_criteria.py
@@ -142,12 +142,30 @@ check("score/string criteria still work",
render_options({"t": "score", "ins": "x", "crit": ["low", "high"]}),
["level 0: low", "level 1: high"])
-# every rendered option must be a str, whatever went in
-for qq in [{"t": "choice", "ins": "x", "crit": {"a": {"n": 1}, "b": [1, 2], "c": 3.5}},
- {"t": "score", "ins": "x", "crit": [{"a": 1}, [2], None]},
- {"t": "noul", "ins": "x", "crit": {"true": [1], "false": {"z": 0}}}]:
- check_true("all options are str (%s)" % qq["t"],
- all(isinstance(o, str) for o in render_options(qq)))
+# every rendered option must be a str, whatever went in.
+# The original set varied the criterion *values* only, and left every label a string, which is
+# why it passed while `render_options({1: None})` returned `[1]` from a function annotated
+# `-> List[str]`. `build_sequence` then called `.replace` on that int, and the error named
+# neither the question nor the label. Labels are varied here for the same reason the values were.
+for label, qq in [
+ ("choice/objects", {"t": "choice", "ins": "x", "crit": {"a": {"n": 1}, "b": [1, 2], "c": 3.5}}),
+ ("score/objects", {"t": "score", "ins": "x", "crit": [{"a": 1}, [2], None]}),
+ ("noul/objects", {"t": "noul", "ins": "x", "crit": {"true": [1], "false": {"z": 0}}}),
+ ("choice/int labels, no description", {"t": "choice", "ins": "x", "crit": {1: None, 2: None, 3: None}}),
+ ("choice/float labels, no description", {"t": "choice", "ins": "x", "crit": {1.5: None}}),
+ ("choice/None label, no description", {"t": "choice", "ins": "x", "crit": {None: None, "billing": None}}),
+ ("choice/bool labels, no description", {"t": "choice", "ins": "x", "crit": {True: None, False: None}}),
+ ("choice/int labels with descriptions", {"t": "choice", "ins": "x", "crit": {1: "one", 2: "two"}}),
+]:
+ check_true("all options are str (%s)" % label,
+ all(isinstance(o, str) for o in render_options(qq)), render_options(qq))
+
+# the rendered text of a non-string label is its string form, not its repr
+check("choice/int label renders as its str", render_options({"t": "choice", "ins": "x", "crit": {1: None}}), ["1"])
+check("choice/None label renders as the word none",
+ render_options({"t": "choice", "ins": "x", "crit": {None: None}}), ["None"])
+check("choice/int label keeps its description form",
+ render_options({"t": "choice", "ins": "x", "crit": {1: "one"}}), ["1: one"])
# the JSON we emit is parseable back
parsed = json.loads(render_options(