Dump multi-line strings as YAML literal blocks - #972
Merged
Conversation
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #972 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 27
Lines 8721 8734 +13
=========================================
+ Hits 8721 8734 +13 ☔ View full report in Codecov by Harness. |
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 does this PR do?
The
yamldump format now writes multi-line strings as literal blocks, i.e.|, instead of escaping the line breaks. This makes dumps of configs that hold things like prompts, descriptions or scripts readable and editable:yaml_dumpnow uses a lazily builtDefaultDumper, a subclass ofyaml.SafeDumperwith astrrepresenter that requests the|style for values containing a newline. This mirrors the existingDefaultLoaderpattern. Subclassing instead of registering the representer onyaml.SafeDumperkeeps the change from leaking into theyaml.safe_dumpcalls of users, consistent with the v5 removal of the globalNamespacerepresenter.Both YAML backends are covered. ruamel.yaml is only used for the
yaml_commentsformat, which round-trips the PyYAML output, so it parses the block into aLiteralScalarStringand re-emits it as|. The comments dump gets the same formatting, with comments still placed above the block.Round-trips are lossless. PyYAML falls back to double-quoted style by itself when a block scalar cannot represent the value, e.g. a line with trailing spaces, non-printable characters, or a flow context. Chomping is preserved:
|with a trailing newline,|-without one and|+for several.Before submitting