Skip to content

fix(structured): reject a non-array 'enum' with a path-naming SchemaError - #1220

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-structured-enum-non-array
Oct 15, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-structured-enum-non-array

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

Fix: reject a non-array enum instead of leaking TypeError

What: _enum_field calls len(values) before checking that values is an array. A scalar enum (int / float / bool / None) raises a raw TypeError: object of type 'int' has no len() out of plan_from_json_schema, and a bare string enum like "abc" is iterable, so it silently splits into per-character options ['a','b','c'] instead of erroring. Added a type guard before len() that raises SchemaError("<path>: 'enum' must be an array of values, got <type>"). const is unaffected — it is already wrapped in a list at the call site.

Why: the module docstring promises anything that cannot be answered from a fixed option set "is rejected with an error that names the path" — a SchemaError. Four inputs violate that promise (leak a non-SchemaError), and a fifth mis-parses silently. This restores the documented error surface.

Verified:

  • Executed witness (fork branch, before fix): enum = 5/1.5/True/None/"abc" → [TypeError ... has no len()] for the four scalars and [BUILT] (char-split) for the string. After the fix all five raise SchemaError naming properties.a and the type; a valid ["x","y"] still builds.

  • Mutation check: removed the guard, ran tests/test_structured.py → exactly the 5 new arms fail (4 TypeError leaks + string "did not raise"), 163 pass / 5 fail; restored → 169 pass / 0 fail.

  • tests/test_structured.py 169/0, test_structured_api.py 38/0, test_structured_docs.py 117/0 (new rejection row documented in docs/structured.md, table count updated).

  • ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120 rc=0; python -m compileall -q laya/ tests/ rc=0; diff secret scan clean.

  • Deconfliction: no open PR touches laya/structured.py Python enum validation. PR perf(agent): tokenize only the prefix of state a sequence holds, and make state_tokens opt-in #687 also edits docs/structured.md/test_structured_docs.py but in disjoint hunks (their edits at docs line ~159 and test lines 517/551; mine at line ~87 and line 294).

  • tests pass before opening

  • secret scan clean

  • rebased onto main

  • one coherent change

…rror

_enum_field called len(values) before checking values was an array, so a
scalar enum (int/float/bool/None) leaked a raw TypeError, and a bare string
enum silently split into per-character options. Validate the type first,
matching the module's promise that malformed schemas raise a SchemaError
naming the path. const is unaffected (already wrapped in a list).
@NandhaKishorM
NandhaKishorM merged commit 9021d1d into NandhaKishorM:main Oct 15, 2026
44 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged in 0.4.8.

A bare string is the case that makes this more than a TypeError tidy-up. str is a Sequence, so {"enum": "abc"} passed len(), passed the emptiness check, and became a three-option choice named a, b, c. The module's contract is that a malformed schema raises a SchemaError naming the path, and that one quietly produced a question.

Checking before len() is the right position, since that is the first call that assumes a sequence. Covering 5, 1.5, True, None and "abc" in the test gets both halves: the three that raised TypeError from inside, and the one that did not raise at all.

The docs/structured.md row and the parsed-row count going 13 to 14 is what keeps the table honest, which is the pattern that page already uses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants