Skip to content

Keep the eol comment on an empty block sequence entry#123

Open
sarathfrancis90 wants to merge 2 commits into
pycontribs:mainfrom
sarathfrancis90:fix-empty-seq-entry-eol-comment
Open

Keep the eol comment on an empty block sequence entry#123
sarathfrancis90 wants to merge 2 commits into
pycontribs:mainfrom
sarathfrancis90:fix-empty-seq-entry-eol-comment

Conversation

@sarathfrancis90

Copy link
Copy Markdown

While round-tripping some config files I hit a case where dump(load(x)) isn't idempotent. A block sequence entry whose value is null/empty but carries an end-of-line comment moves the comment onto its own line on the second round-trip:

import ruyaml, sys
yaml = ruyaml.YAML()
d = yaml.load("- null  # comment\n")
yaml.dump(d, sys.stdout)      # -> "-       # comment"

Reload that output and dump again and you get:

- 
        # comment

It's worse with several empty entries: their comments all slide down and end up attached to the wrong items.

Root cause: an empty entry has no value token for the eol comment to attach to, so the scanner queues it as a comment preceding the next token. parse_block_sequence_entry (and the indentless variant) then let it fall through to the sequence end instead of keeping it on the entry. Empty block mapping values (a: # comment) already handle this correctly, so the sequence path was just missing the equivalent step.

The fix attaches a comment that sits on the same line as the - indicator to the empty entry itself, leaving genuine standalone comments (on their own line) alone.

Tested with two new cases in _test/test_comments.py covering the single-entry idempotency and the multi-entry attachment; the full _test/test_*.py suite still passes.

A null (empty) block sequence entry has no value token for an
end-of-line comment to attach to, so the scanner queues that comment as
one preceding the next token. The parser let it fall through to the
sequence end, so dump(load(x)) was not idempotent for e.g.

    - null  # comment

On the first dump the comment stays on the entry's line, but reloading
that output moves it onto the sequence end, so a second dump drifts it
onto its own line. With several empty entries the comments were even
reassigned to the wrong items.

Attach a comment that sits on the same line as the '-' indicator to the
(empty) entry itself, mirroring how empty block mapping values are
already handled. This covers both regular and indentless sequences.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant