Repository navigation
fix(703): carry decision specs through to_stickler_config() and model_from_json() - #724
Conversation
…_from_json() to_stickler_config() wrote Choice, Score and Noul fields as plain exact-match fields, so model_from_json() rebuilt them without their questions: an unasked Noul scored 0.0 instead of being left out, and from_json() rejected answer objects. The config now records each question under 'decision', with the same content as the schema's x-aws-stickler-decision, and model_from_json() rebuilds the decision field from it with the schema importer's checks: a comparator or threshold other than exact match at 1.0, a malformed question, or a type that is not the answer type is refused. A 'decision' key stands in for a comparator in a hand-written config. Fixes #703.
Security Scan ResultsASH Security Scan Report
Scan Metadata
SummaryScanner ResultsThe table below shows findings by scanner, with status based on severity thresholds and dependencies:
Report generated by Automated Security Helper (ASH) at 2026-12-03T21:57:47+00:00 |
digalaviz
left a comment
There was a problem hiding this comment.
The fix is right for the #703 case, but one loss is still silent: a decision field's default does not survive the round trip, and that changes the score.
_convert_decision_field always builds the field with {"default": None}, while to_stickler_config() writes the real default. With queue: Optional[str] = Choice(criteria=["billing", "sales"], default="billing") and a plain note field:
- original:
T(note="x").compare_with(T(queue="sales", note="x"))["field_scores"]is{'queue': 0.0, 'note': 1.0} - rebuilt from the config:
{'note': 1.0}, soqueueis left out of the score - a second
to_stickler_config()changesdefaultfrom'billing'toNone
Also on the config path: {"type": "str", "decision": {...}, "alias": "Q"} builds a field with no alias and no warning, but Choice(alias=...) raises TypeError.
This PR copies the gap from JsonSchemaFieldImporter._decision_field, which also hard-codes {"default": None}, so from_json_schema(T.to_json_schema()) loses the default too. That path came in with #540. I can open a separate issue for it.
I measured this on 2f2be3d. Merged on top of dev plus #688, #690 and #710 it has no conflicts: 7430 passed, 13 skipped, 1 xfailed.
Do you want to pass default through and raise on alias, or raise on any non-None default?
From review: both importers built a decision field with default None, so a Choice(default='billing') lost its default through to_stickler_config() and model_from_json(), and the question dropped out of the score. They now pass the default through, checked against the question's answers, and the config path refuses 'alias' as Choice(alias=...) does.
|
Thanks @digalaviz, good catch. Fixed in
5 new tests fail on |
digalaviz
left a comment
There was a problem hiding this comment.
The default and alias fixes work: I get {'queue': 0.0, 'note': 1.0} from both classes, a second export keeps 'billing', and an invalid default or an alias raises.
One more field setting is lost on the config path: required. With c: Optional[str] = Choice(criteria=["x", "y"], default=...), to_stickler_config() writes 'required': True, but _convert_decision_field never reads it, so the rebuilt class accepts an instance without c. On dev the same config builds a required field, so this is new in this PR. The schema path loses it too, but it already did on dev.
Two small notes, not blocking:
- In Python,
Choice(criteria=["a", "b"], default="zzz")builds without error, andmodel_from_json()now refuses its export. Raising is right; validating inChoice()too would make both sides agree. That can be a follow-up. - The PR body still describes the first commit: it says 14 tests (the file has 12 test functions) and does not mention the default, alias and schema importer changes.
I measured this on da7d6d0. Merged with dev it has no conflicts: 7446 passed, 13 skipped, 1 xfailed.
Can you read required in _convert_decision_field as the plain field path does?
model_from_json() now reads 'required' and 'default' for a decision field the way the plain field path does: 'required' wins over a default, and a field with neither defaults to None. A required Choice exported by to_stickler_config() rebuilt as optional before.
|
Thanks @digalaviz. Fixed in
3 new tests fail on On your notes:
|
digalaviz
left a comment
There was a problem hiding this comment.
required now survives the config round trip: with c: Optional[str] = Choice(criteria=["x", "y"], default=...), the rebuilt class rejects an instance without c, as the original does. Default and alias handling still hold. Merged with dev at 545da9f it has no conflicts: 7451 passed, 13 skipped, 1 xfailed.
What changes
to_stickler_config()wroteChoice,ScoreandNoulfields as plain exact-match fields, somodel_from_json()rebuilt them without their questions. The rebuilt model scored an unasked Noul (goldNone) as 0.0 instead of leaving it out, and itsfrom_json()rejected answer objects. Nothing warned.to_json_schema()already kept the specs.The config now records each question under
decision, with the same content as the schema'sx-aws-stickler-decision:model_from_json()rebuilds the decision field from it, with the schema importer's checks, and keeps the field's settings:defaultis passed through and checked against the question's answers. A default the question doesn't have raisesInvalid default for decision field .... The schema importer (JsonSchemaFieldImporter._decision_field) now reads a schemadefaultthe same way. It used to hard-codeNone.requiredreads as on the plain field path:requiredwins over a default, and a field with neither defaults toNone.aliasis refused, asChoice(alias=...)is.It also refuses:
decisionthat isn't an object with atype;on_invalid;typethat isn't the answer type (strfor Choice,intfor Score,boolfor Noul;Optional[...]is fine).In a hand-written config,
decisionstands in for a comparator.Not changed here:
to_json_schema()writes no literal default for any scalar field, decision or not, sofrom_json_schema(T.to_json_schema())still has no default to read. That's the general export gap.requiredfor a decision field, as ondev.Choice()doesn't validate its own default.The last two are a follow-up.
The decision API is unreleased, so the CHANGELOG change is a clause in the #469 entry.
Tests
tests/decisions/test_config_round_trip.py: 14 test functions, 24 cases. They cover:on_invalid="missing"and a non-defaultweightsurviving the round trip;decision_metricsfor the original and the rebuilt model;defaultandrequiredsurviving the round trip, and the schema importer reading a default;Full suite: 7254 passed, 13 skipped, 1 xfailed.
ruff check .anddocs/check_links.pypass.Fixes #703. @vawsgit, this was assigned to you with no branch yet, and it comes from #540, so I picked it up to make 1.1.0. Shout if you'd started.