pyraml-parser icon indicating copy to clipboard operation
pyraml-parser copied to clipboard

side-effect on other modules using PyYAML

Open ntoofu opened this issue 7 years ago • 2 comments

Following example script fails because yaml.add_representer function which is called in initialization of pyraml module (pyraml/__init__.py) changes behavior of PyYAML module. Modified PyYAML module will parse dictionary structure in YAML to UniqOrderedDict, so re-assignment to the data parsed by PyYAML will raise exception.

import pyraml
import yaml


if __name__ == '__main__':
    with open("test.yml") as f:
        data = yaml.load(f)

    print(data)
    data['key'] = "new_value"    # <= raise ValidationError
    print(data)

Result:

UniqOrderedDict([('key', 'value')])
Traceback (most recent call last):
  File "example.py", line 10, in <module>
    data['key'] = "new_value"
  File "/usr/local/lib/python2.7/site-packages/pyraml/__init__.py", line 24, in __setitem__
    raise ValidationError("Property already used: {0}".format(key))
pyraml.ValidationError: 'Property already used: key'

The exception is raised from the line below. https://github.com/an2deg/pyraml-parser/blob/6d9f47409d316653eb428d858d0140fdea28dfa3/pyraml/init.py#L24

This problem is caused by design of yaml.add_representer in PyYAML module, but it may be better to modify pyraml-parser because PyYAML module is used widely and is hard to be changed.

ntoofu avatar Oct 07 '17 08:10 ntoofu

I'm about four years late to this party, but wanted to say thanks for raising this issue as it helped me understand a problem I was having with pyraml-parser breaking PyYAML used in another place in the same code base. I worked around it by using ruyaml instead (leaving pyraml to have its monkey-patched PyYAML for itself).

jimconner avatar Apr 30 '21 12:04 jimconner

Hi @jimconner ,

Unfortunately I don't have time to maintain this project. The project is unmaintained for 4 or 5 years already. Most probably I will put a banner that's the project is not maintaining anymore.

an2deg avatar Apr 30 '21 15:04 an2deg