Repository navigation
fix(structured): reject a non-array 'enum' with a path-naming SchemaError - #1220
Merged
NandhaKishorM merged 1 commit intoOct 15, 2026
Merged
Conversation
…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).
Owner
|
Merged in 0.4.8. A bare string is the case that makes this more than a Checking before The |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: reject a non-array
enuminstead of leakingTypeErrorWhat:
_enum_fieldcallslen(values)before checking thatvaluesis an array. A scalarenum(int / float / bool / None) raises a rawTypeError: object of type 'int' has no len()out ofplan_from_json_schema, and a bare stringenumlike"abc"is iterable, so it silently splits into per-character options['a','b','c']instead of erroring. Added a type guard beforelen()that raisesSchemaError("<path>: 'enum' must be an array of values, got <type>").constis 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 raiseSchemaErrornamingproperties.aand 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.py169/0,test_structured_api.py38/0,test_structured_docs.py117/0 (new rejection row documented indocs/structured.md, table count updated).ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120rc=0;python -m compileall -q laya/ tests/rc=0; diff secret scan clean.Deconfliction: no open PR touches
laya/structured.pyPython enum validation. PR perf(agent): tokenize only the prefix of state a sequence holds, and make state_tokens opt-in #687 also editsdocs/structured.md/test_structured_docs.pybut 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