Menu

#404 Type error when trying to add_eol_comment to an item in a list

open
nobody
None
minor
bug
2021-10-23
2021-10-23
No

Hello,

I am coming across a unique error where if I create a 'before' comment, then try and create an eol comment on the same object, I get the following error:

Traceback

Traceback (most recent call last):
  File "/tmp/tmp.SdU5nOzhkq/run-ruamel.py", line 39, in <module>
    shopping_list.get("Shopping List").get("milk").get("brands").\
  File "/home/alexiswl/anaconda3/envs/ruamel/lib/python3.9/site-packages/ruamel/yaml/comments.py", line 347, in yaml_add_eol_comment
    self._yaml_add_eol_comment(ct, key=key)
  File "/home/alexiswl/anaconda3/envs/ruamel/lib/python3.9/site-packages/ruamel/yaml/comments.py", line 485, in _yaml_add_eol_comment
    self._yaml_add_comment(comment, key=key)
  File "/home/alexiswl/anaconda3/envs/ruamel/lib/python3.9/site-packages/ruamel/yaml/comments.py", line 479, in _yaml_add_comment
    self.yaml_key_comment_extend(key, comment)
  File "/home/alexiswl/anaconda3/envs/ruamel/lib/python3.9/site-packages/ruamel/yaml/comments.py", line 247, in yaml_key_comment_extend
    r[1].extend(comment[0])
TypeError: 'CommentToken' object is not iterable

Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python
import ruamel.yaml
from ruamel.yaml.main import round_trip_dump
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedSeq
from ruamel.yaml.comments import CommentedMap
import sys

print(ruamel.yaml.version_info)

INDENTATION_LEVEL = 2

shopping_list = CommentedMap({
    "Shopping List": CommentedMap({
        "eggs": CommentedMap({
            "type": "free range",
            "brand": "Mr Tweedy",
            "amount": 12
        }),
        "milk": CommentedMap({
            "type": "pasteurised",
            "litres": 1.5,
            "brands": CommentedSeq([
                "FarmFresh",
                "FarmHouse gold",
                "Daisy The Cow"
            ])
        })
    }),
    "Another List": CommentedMap({
        "foo": "bee"
    })
})

object_depth = 3 # Shopping List -> Milk -> Brands
shopping_list.get("Shopping List").get("milk").get("brands").\
    yaml_set_comment_before_after_key(key=1, before="Last Resorts", indent=object_depth*INDENTATION_LEVEL)

shopping_list.get("Shopping List").get("milk").get("brands").\
    yaml_add_eol_comment(key=1, comment="Too creamy")

round_trip_dump(shopping_list, sys.stdout, indent=2, block_seq_indent=2) 

Although the error happens in the yaml_add_eol_comment, if I take out the previous command that sets the 'Last Resorts' method, the script completes.

Discussion

  • Anthon van der Neut

    It could well be that the yaml_set_comment_before_after_key initiates the .ca attribute incorrectly, I would have to look into that, but these (undocumented) routines are going to change/be replaced, so it is not a high priority for me.
    It might be that when you order the assignments differently, you won't get the error.

    Make sure that the end result YAML document (with comments), round trips correctly. That is what the library should do. If it doesn't building it from scratch with the undocumented methods is going to be difficult if not impossible.

    On a side note: stop using the function round_trip_dump(), it has a deprecation warning and you are using it with incorrect parameters. Instead use:

    yaml = ruamel.yaml.YAML()
    yaml.indent(sequence=2, sequence=4, offset=2)
    
     
  • Anthon van der Neut

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -19,7 +19,7 @@
     ~~~
    
     ### Script
    -~~~
    +```
     #!/usr/bin/python
     import ruamel.yaml
     from ruamel.yaml.main import round_trip_dump
    @@ -62,6 +62,6 @@
         yaml_add_eol_comment(key=1, comment=&#34;Too creamy&#34;)
    
     round_trip_dump(shopping_list, sys.stdout, indent=2, block_seq_indent=2) 
    -~~~
    +```
    
     Although the error happens in the `yaml_add_eol_comment`, if I take out the previous command that sets the &#39;Last Resorts&#39; method, the script completes.  
    
     
  • Alexis Lucattini

    On a side note: stop using the function round_trip_dump(), it has a deprecation warning and you are using it with incorrect parameters.

    Thanks, can you point me to the documentation for this? The documentation here has an example of round_trip_dump, so I've used that. I'm using version 0.17.4 (see https://sourceforge.net/p/ruamel-yaml/tickets/400) where it was not deprecated so I haven't seen these deprecation warnings. What parameters are incorrect in my current implementation?

     
  • Anthon van der Neut

    The documentation is not fully updated, and there are legio examples on StackOverflow not updated as well. The next section ( https://yaml.readthedocs.io/en/latest/api.html ) explains that and the basic example use the YAML() instance.

    Your block_seq_indent has to be 2 less than the indent otherwise there is no space for the blockelement indicator (-) and the space that needs to follow it.

     

Log in to post a comment.