Menu

#535 Encountered "NotImplementedError: overlap in comment"

resolved
nobody
None
minor
bug
2025-01-17
2025-01-16
No
# Bug report:
# This input (which I *think* is valid):
yaml_str = ("""\
-   name: John
    family:
        spouse: Mary
        children: []

-   # name: Robert
    name: Bob
    age: 47
""")
import ruamel.yaml
yaml = ruamel.yaml.YAML()
yaml.load(yaml_str)
# causes this error on load:
#   NotImplementedError: overlap in comment [None, [CommentToken('\n', line: 4, col: 0)]] [None, [CommentToken('# name: Robert\n', line: 5, col: 4)]]

output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ktessarzik/.pyenv/versions/3.11.3/lib/python3.11/site-packages/ruamel/yaml/main.py", line 451, in load
    return constructor.get_single_data()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ktessarzik/.pyenv/versions/3.11.3/lib/python3.11/site-packages/ruamel/yaml/constructor.py", line 114, in get_single_data
    node = self.composer.get_single_node()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ktessarzik/.pyenv/versions/3.11.3/lib/python3.11/site-packages/ruamel/yaml/composer.py", line 72, in get_single_node
    document = self.compose_document()
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ktessarzik/.pyenv/versions/3.11.3/lib/python3.11/site-packages/ruamel/yaml/composer.py", line 94, in compose_document
    node = self.compose_node(None, None)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ktessarzik/.pyenv/versions/3.11.3/lib/python3.11/site-packages/ruamel/yaml/composer.py", line 128, in compose_node
    node = self.compose_sequence_node(anchor)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ktessarzik/.pyenv/versions/3.11.3/lib/python3.11/site-packages/ruamel/yaml/composer.py", line 172, in compose_sequence_node
    while not self.parser.check_event(SequenceEndEvent):
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ktessarzik/.pyenv/versions/3.11.3/lib/python3.11/site-packages/ruamel/yaml/parser.py", line 141, in check_event
    self.current_event = self.state()
                         ^^^^^^^^^^^^
  File "/Users/ktessarzik/.pyenv/versions/3.11.3/lib/python3.11/site-packages/ruamel/yaml/parser.py", line 537, in parse_block_sequence_entry
    self.move_token_comment(token)
  File "/Users/ktessarzik/.pyenv/versions/3.11.3/lib/python3.11/site-packages/ruamel/yaml/parser.py", line 815, in move_token_comment
    token.move_old_comment(self.scanner.peek_token() if nt is None else nt, empty=empty)
  File "/Users/ktessarzik/.pyenv/versions/3.11.3/lib/python3.11/site-packages/ruamel/yaml/tokens.py", line 139, in move_old_comment
    raise NotImplementedError(f'overlap in comment {c!r} {tc!r}')
NotImplementedError: overlap in comment [None, [CommentToken('\n', line: 4, col: 0)]] [None, [CommentToken('# full_name: Robert\n', line: 5, col: 4)]]

expected:


Mac OS 14.5
Python 3.11.3
ruamel.yaml 0.18.6

Discussion

  • Anthon van der Neut

    The input is valid, it goes wrong because the comment is between the sequence indicator (-) and the element ( the mapping ). This is a bug, thanks for reporting.
    IIRC it used to be that those comments would wander to a different position. I didn't change/test on this and I made changes a long time ago. But it should not crash.
    Put the comment above the second sequence indicator on a line of its own if you want to keep the comment in the file. That should help as a workaround

     

    Last edit: Anthon van der Neut 2025-01-16
  • Anthon van der Neut

    It is the combination of having a comment before the sequence indicator (i.e. the empty line) and one after it (before the element). I updated the code that moves the comment, to combine the comments (so # name: Robert will move up a line, at least it doesn't throw an exception).
    This will be in the next release.
    Thanks for reporting

     
  • Anthon van der Neut

    • status: open --> resolved
     

Log in to post a comment.