Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError trying to modify comment with yaml_add_eol_comment #32

Closed
tompaton opened this issue Dec 16, 2020 · 3 comments
Closed

TypeError trying to modify comment with yaml_add_eol_comment #32

tompaton opened this issue Dec 16, 2020 · 3 comments

Comments

@tompaton
Copy link
Contributor

tompaton commented Dec 16, 2020

Using version ruyaml==0.19.2 (issue also occurred with master).

Generate a test file:

Python 3.7.2 (default, Mar  5 2019, 06:22:51)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ruamel.yaml import YAML
>>> data = {'items': [{'one': 1, 'uno': '1'}, {'two': 2, 'duo': '2'}, {'three': 3}]}
>>> yaml = YAML(typ='rt')
>>> yaml.dump(data, stream=open('input.yaml', 'w'))
>>> print(open('input.yaml', 'r').read())
items:
- one: 1
  uno: '1'
- two: 2
  duo: '2'
- three: 3

Read the file and add a comment, this works as expected:

>>> yaml = YAML(typ='rt')
>>> data = yaml.load(open('input.yaml', 'r'))
>>> data['items'].yaml_add_eol_comment('item 2', key=1)
>>> yaml = YAML(typ='rt')
>>> yaml.dump(data, stream=open('output.yaml', 'w'))
>>> print(open('output.yaml', 'r').read())
items:
- one: 1
  uno: '1'
-  # item 2
  two: 2
  duo: '2'
- three: 3

But if I read that file back in and try to change the comment, there is an error:

>>> yaml = YAML(typ='rt')
>>> data = yaml.load(open('output.yaml', 'r'))
>>> data['items'].yaml_add_eol_comment('second pass', key=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/ruamel/yaml/comments.py", line 303, in yaml_add_eol_comment
    self._yaml_add_eol_comment(ct, key=key)
  File "/usr/local/lib/python3.7/site-packages/ruamel/yaml/comments.py", line 441, in _yaml_add_eol_comment
    self._yaml_add_comment(comment, key=key)
  File "/usr/local/lib/python3.7/site-packages/ruamel/yaml/comments.py", line 435, in _yaml_add_comment
    self.yaml_key_comment_extend(key, comment)
  File "/usr/local/lib/python3.7/site-packages/ruamel/yaml/comments.py", line 206, in yaml_key_comment_extend
    r[1].extend(comment[0])
TypeError: 'CommentToken' object is not iterable

Note, this works ok with a simpler input file, e.g.:

items:
- one
- two
- three
@ssbarnea
Copy link
Member

PRs are welcomed!

@tompaton
Copy link
Contributor Author

I looked into it when trying to reproduce it, but I'm no where near familiar enough with the code to understand what's going on, let along know what needs to be done to fix this I'm afraid...

@tompaton
Copy link
Contributor Author

tompaton commented Jan 2, 2021

I think I managed to fix this issue in PR #35

@smurfix smurfix closed this as completed in 33e86bb Feb 8, 2021
smurfix added a commit that referenced this issue Feb 8, 2021
Add test+fix for issue #32 and xfail-marked test for #33
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

No branches or pull requests

2 participants