Add optional strict mode for duplicate YAML keys#2256
Open
salignatmoandal wants to merge 1 commit intobasetenlabs:mainfrom
Open
Add optional strict mode for duplicate YAML keys#2256salignatmoandal wants to merge 1 commit intobasetenlabs:mainfrom
strict mode for duplicate YAML keys#2256salignatmoandal wants to merge 1 commit intobasetenlabs:mainfrom
Conversation
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.
🚀 What
truss/util/yaml_utils.pyto support an optional strict mode when loading configs.strict=Trueis passed tosafe_load_yaml_with_no_duplicates, aValueErroris raised as soon as a duplicate key is detected.This prepares the future breaking change mentioned in the TODO comment (around ~7/2026) where duplicate keys should become an error instead of a warning.
💻 How
strict: bool = Falseflag on_SafeLoaderWarnDuplicateKeys, the customyaml.SafeLoadersubclass used for Truss configs.construct_mappingto:set.strictisTrue, raiseValueError("Duplicate keyin YAML mapping").warnings.warn(...)while still using the last value.safe_load_yaml_with_no_duplicatesto:strict: bool = Falseparameter._SafeLoaderWarnDuplicateKeys.strict = strictfor the current call.yaml.load(stream, Loader=_SafeLoaderWarnDuplicateKeys).This keeps the current default behaviour fully backwards compatible while allowing callers (e.g. CLI validation) to opt into strict duplicate-key checking when desired.
🔬 Testing
strict=False:safe_load_yaml_with_no_duplicates:test_duplicate_keys_warn_in_non_strict_mode– ensures a warning is emitted and loading succeeds whenstrict=False.test_duplicate_keys_raise_in_strict_mode– ensures aValueErroris raised whenstrict=True.