Skip to content

Dump multi-line strings as YAML literal blocks - #972

Merged
mauvilsa merged 2 commits into
mainfrom
yaml-dump-str-line-breaks
Sep 10, 2026
Merged

Dump multi-line strings as YAML literal blocks#972
mauvilsa merged 2 commits into
mainfrom
yaml-dump-str-line-breaks

Conversation

@mauvilsa

Copy link
Copy Markdown
Owner

What does this PR do?

The yaml dump 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:

# before                          # after
prompt: 'You are helpful.         prompt: |
                                    You are helpful.
  Be concise.                       Be concise.

  '

yaml_dump now uses a lazily built DefaultDumper, a subclass of yaml.SafeDumper with a str representer that requests the | style for values containing a newline. This mirrors the existing DefaultLoader pattern. Subclassing instead of registering the representer on yaml.SafeDumper keeps the change from leaking into the yaml.safe_dump calls of users, consistent with the v5 removal of the global Namespace representer.

Both YAML backends are covered. ruamel.yaml is only used for the yaml_comments format, which round-trips the PyYAML output, so it parses the block into a LiteralScalarString and 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

  • Did you read the contributing guideline?
  • If you used a coding agent, did you fully understand and validate all generated code and ensure it follows the contributing guidelines?
  • Did you update the documentation? (readme and public docstrings)
  • Did you write unit tests such that there is 100% coverage on related code? (required for bug fixes and new features)
  • Did you verify that new and existing tests pass locally?
  • [n/a] If this is a bug fix, did you verify that the tests fail without the code fix?
  • Did you make sure that all changes preserve backward compatibility?
  • Did you update the CHANGELOG including a pull request link? (not for typos, docs, test updates, or minor internal changes/refactors)

@mauvilsa mauvilsa added the enhancement New feature or request label Sep 10, 2026
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Sep 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5fc71c7) to head (fd0a0d5).
✅ All tests successful. No failed tests found.

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.
📢 Have feedback on the report? Share it here.

@mauvilsa
mauvilsa merged commit 41fccd0 into main Sep 10, 2026
32 checks passed
@mauvilsa
mauvilsa deleted the yaml-dump-str-line-breaks branch September 10, 2026 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant