Menu

#567 Support for preserving flow style

invalid
nobody
None
minor
enhancement
2026-03-20
2026-03-18
No

If my YAML has some spacing and indentation, currently both the spacing and indentation in a flow style are reformatted:

before:

deployment: { enabled: false }
daemonset:  { enabled: false }

after ruamel:

deployment: {enabled: false}
daemonset: {enabled: false}

I notice the same on arrays:

command: [ "sleep", "60" ]

becomes:

command: ["sleep", "60"]

Can support be added to respect these style via a preserve_flow_spacing = True configuration or is there another way to prevent this?

Settings:

#ruamel.yaml==0.19.0
#ruamel.yaml.clib==0.2.15

yaml = YAML(typ="rt")
yaml.preserve_quotes = True
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.allow_duplicate_keys = True
yaml.width = 4096
yaml_contents = yaml.load(f)

Discussion

  • Anthon van der Neut

    • status: open --> invalid
     
  • Anthon van der Neut

    This is by design as the extra spaces are suprefluous.
    However the start, end and separator for both collections types are specified as class variables on Emitter as flow_map_start = '{' etc. So you can have any of those "spaced" so the flow styles come out differently (but still all the same way).
    This request for supprot belongs on StackOverflow where the question has already been answered https://stackoverflow.com/a/76547814/1307905, and it is also mentioned in the ChangeLog

     
    👍
    1
  • Colin O'Keeffe

    Colin O'Keeffe - 2026-03-20

    thanks for the response.

     

Log in to post a comment.

MongoDB Logo MongoDB