Repository navigation
Validate assignments to Selection so values stays a tuple - #1283
Open
frostyhand wants to merge 1 commit into
Open
frostyhand wants to merge 1 commit into
frostyhand wants to merge 1 commit into
Conversation
Selection.values is declared as tuple[MinimalDecisionPointValue, ...] with min_length=1, but pydantic only checks that at init. Code that assigns afterwards, such as `selection.values = [...]` in examples.py and in the API example tests, left a plain list on the model. Serializing it then emitted PydanticSerializationUnexpectedValue warnings (12 in the test suite), and an empty list was accepted. Turn on validate_assignment for Selection, so an assigned list is coerced to a tuple of MinimalDecisionPointValue and an empty list is rejected, the same as at init. Fixes CERTCC#1135
This branch has not been deployed
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.
Selection.valuesis declared astuple[MinimalDecisionPointValue, ...]withmin_length=1, but pydantic only enforces that when the object is created. Assigning afterwards skips validation, soselection.values = [...]leaves a plain list on the model.That is where the warnings in #1135 come from.
examples.pydoes this forEXAMPLE_SELECTION_1, which the/examples/selectionsand/examples/selection-listsendpoints return, and the API example tests do it after sampling values. Serializing those objects then emitsPydanticSerializationUnexpectedValue(12 times in the test suite). An empty list was also accepted on assignment, although it is rejected at init.The change turns on
validate_assignmentforSelectiononly. An assigned list is now coerced to a tuple ofMinimalDecisionPointValue, plain dicts are validated into that type, and an empty list raises a validation error, the same as at init. The other fields ofSelectionget the same treatment, so assigning a badnamespaceorversionis now caught too.Selectionhas no model-level validators, so nothing else runs on assignment.I kept the declared type as a tuple rather than switching to a list (the other option in the issue), since decision point values are ordered sets (ADR 0008) and keeping the type means the JSON schema does not change.
Tests:
test_selection_values_assignment_is_validated: assigning a list stores a tuple, dicts becomeMinimalDecisionPointValue, andmodel_dump,model_dump_jsonand the parentSelectionList.model_dump_jsonraise no warnings when warnings are turned into errors.test_selection_values_assignment_requires_one_value: assigning[]raises and leaves the old values in place.main. Full suite: 204 passed (202 onmainplus these 2), andpytest -W defaultnow shows 0 Pydantic serializer warnings (12 before).main.