question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pyaml not compatible with ruamel.yaml

See original GitHub issue

Given the following test yaml:

# This is a commented YAML file
replica: 1

image:
  repository: docker/python
  pull: IfNotPresent

imagePullSecrets:
  - name: docker-secret

and the following test python:

#!/usr/bin/env python3
from pathlib import Path
import pyaml
from ruamel.yaml import YAML

yaml = YAML()
test_file = Path("./test.yaml")
test_load = yaml.load(test_file)
pyaml.p(test_load)

I get the following error:

Traceback (most recent call last):
  File "./test_yaml.py", line 9, in <module>
    pyaml.p(test_load)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyaml/__init__.py", line 208, in pprint
    dump(data, dst=dst, **dump_kws)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyaml/__init__.py", line 188, in dump
    yaml.dump_all( data, buff, Dumper=Dumper,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/yaml/__init__.py", line 278, in dump_all
    dumper.represent(data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/yaml/representer.py", line 27, in represent
    node = self.represent_data(data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/yaml/representer.py", line 58, in represent_data
    node = self.yaml_representers[None](self, data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyaml/__init__.py", line 37, in represent_undefined
    elif isinstance(data, OrderedDict): return dumper.represent_odict(data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyaml/__init__.py", line 29, in represent_odict
    node_value = dumper.represent_data(item_value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/yaml/representer.py", line 58, in represent_data
    node = self.yaml_representers[None](self, data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyaml/__init__.py", line 40, in represent_undefined
    return super(PrettyYAMLDumper, dumper).represent_undefined(data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/yaml/representer.py", line 231, in represent_undefined
    raise RepresenterError("cannot represent an object", data)
yaml.representer.RepresenterError: ('cannot represent an object', [ordereddict([('name', 'docker-secret')])])

Given that ruamel is mentioned in the docs for parsing, pyaml should be able to handle the results.

The problem is likely that ruamel creates and uses it’s own subclass of OrderedDict:

https://sourceforge.net/p/ruamel-yaml/code/ci/default/tree/constructor.py#l20

https://sourceforge.net/p/ruamel-yaml/code/ci/default/tree/compat.py#l23

pyaml could do something like:

try:
	from ruamel.yaml.compat import ordereddict
	PrettyYAMLDumper.add_representer(ordereddict, PrettyYAMLDumper.represent_odict)
except ImportError:
    pass

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
mk-fgcommented, Mar 10, 2020

Don’t think I can.

When I used it for setting values in exising config, didn’t bother with export and simply updated numbers/strings at a position where ruamel.yaml said they were in the source file, to easily avoid any re-formatting that way.

Only needed to update single and simple values that way though. Should be impractical to do that with more complex values or changes.

1reaction
mk-fgcommented, Mar 9, 2020

Dropped earlier special-case tweak now, and added suggestion to use YAML(typ='safe') to README instead.

As mentioned, don’t think “unsafe” load (if I understand what it does correctly) should work with this module, as it allows any kind of custom types via arbitrary code execution.

There are probably use-cases for it, but note that in many common cases it’s very much not what you want to do, and allows for easily-explotable security issues by design, unless yaml in question is some kind of static file comitted alongside code (e.g. definition of something) and can only be edited using same capabilities as editing code next to it.

And another two reasons for not bothering to support this use-case here:

  • Would be nice to avoid “import” for a random third-party module, like ruamel.yaml here.
  • I’m lazy to figure out how to do it too 😃

If you can make a PR that’d implement it somehow, and ideally won’t always be importing ruaml.yaml - let me know, can probably merge it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Differences with PyYAML - ruamel.yaml - Read the Docs
YAML 1.2 no longer supports sexagesimals, so the string scalar 12:34:56 doesn't need quoting. \/ escape for JSON compatibility; correct parsing of floating ......
Read more >
Import "ruamel.yaml" could not be resolved - Stack Overflow
I have a requirement of converting a JSON Object into a YAML file and trying to import ruamel. yaml . I did install...
Read more >
ruamel.yaml - PyPI
ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order.
Read more >
Allow dumpdata in YAML using ruamel.yaml rather than PyYaml.
I was unable to get it running because ruaml (even though based on pyyaml) is incompatible to the way Django handles serializers. However,...
Read more >
YAML: The Missing Battery in Python
Not only was JSON's minimalistic syntax appealing to developers, ... the ruamel.yaml library, which is derived from an older PyYAML version.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found