Safe dumping and loading of defaultdict with ruamel.yaml

Viewed 1521

I'm trying to (de-)serialize classes that have collections.defaultdict properties with ruamel.yaml in Python (3.6+ in my case).

This would be a minimal example that I would like to get to work:

from collections import defaultdict
import ruamel.yaml
from pathlib import Path

class Foo:
    def __init__(self):
        self.x = defaultdict()


YAML = ruamel.yaml.YAML(typ="safe")
YAML.register_class(Foo)
YAML.register_class(defaultdict)

fp =  Path("./test.yaml")
YAML.dump(Foo(), fp)
YAML.load(fp)

But this fails with:

AttributeError: 'collections.defaultdict' object has no attribute '__dict__'

Any ideas that would not require writing custom code for every "Foo-like" class? I was hoping I could add a different representer for defaultdict objects, but my attempts have been in vain so far.

Full traceback:

Traceback (most recent call last):
File "./tests/test_yaml.py", line 18, in <module>
    YAML.dump(Foo(), fp)
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\main.py", line 439, in dump
    return self.dump_all([data], stream, _kw, transform=transform)
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\main.py", line 453, in dump_all
    self._context_manager.dump(data)
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\main.py", line 801, in dump
    self._yaml.representer.represent(data)
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\representer.py", line 81, in represent
    node = self.represent_data(data)
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\representer.py", line 108, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\main.py", line 638, in t_y
    tag, data, cls, flow_style=representer.default_flow_style
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\representer.py", line 384, in represent_yaml_object
    return self.represent_mapping(tag, state, flow_style=flow_style)
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\representer.py", line 218, in represent_mapping
    node_value = self.represent_data(item_value)
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\representer.py", line 108, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\main.py", line 638, in t_y
    tag, data, cls, flow_style=representer.default_flow_style
File "C:\miniconda-windows\envs\ratio\lib\site-packages\ruamel\yaml\representer.py", line 383, in represent_yaml_object
    state = data.__dict__.copy()
AttributeError: 'collections.defaultdict' object has no attribute '__dict__'
2 Answers

This is because defaultdict is a subclass of the built-in class dict, which does not have a __dict__ attribute for the YAML encoder to generate class attribute names. In this case defaultdict should be treated as a dict, but the problem is that the represent_data method of the ruamel.yaml.representer.BaseRepresenter class only looks at the class of the object itself to determine if there is a representer for the object:

data_types = type(data).__mro__
# ...skipped
if data_types[0] in self.yaml_representers:
    node = self.yaml_representers[data_types[0]](self, data)

What should be done instead is for it to check if any of the data types in __mro__ has a representer, and use it if one is found:

if any(data_type in self.yaml_representers for data_type in data_types):
    node = self.yaml_representers[next(data_type for data_type in data_types if data_type in self.yaml_representers)](self, data)

So we can monkey-patch this method ourselves:

def represent_data(self, data):
    # type: (Any) -> Any
    if self.ignore_aliases(data):
        self.alias_key = None
    else:
        self.alias_key = id(data)
    if self.alias_key is not None:
        if self.alias_key in self.represented_objects:
            node = self.represented_objects[self.alias_key]
            # if node is None:
            #     raise RepresenterError(
            #          "recursive objects are not allowed: %r" % data)
            return node
        # self.represented_objects[alias_key] = None
        self.object_keeper.append(data)
    data_types = type(data).__mro__
    if representer.PY2:
        # if type(data) is types.InstanceType:
        if isinstance(data, representer.types.InstanceType):
            data_types = representer.get_classobj_bases(data.__class__) + list(data_types)
    if any(data_type in self.yaml_representers for data_type in data_types):
        node = self.yaml_representers[next(data_type for data_type in data_types if data_type in self.yaml_representers)](self, data)
    else:
        for data_type in data_types:
            if data_type in self.yaml_multi_representers:
                node = self.yaml_multi_representers[data_type](self, data)
                break
        else:
            if None in self.yaml_multi_representers:
                node = self.yaml_multi_representers[None](self, data)
            elif None in self.yaml_representers:
                node = self.yaml_representers[None](self, data)
            else:
                node = representer.ScalarNode(None, representer.text_type(data))
    # if alias_key is not None:
    #     self.represented_objects[alias_key] = node
    return node
representer.BaseRepresenter.represent_data = represent_data

so that your code would work without registering defaultdict:

class Foo:
    def __init__(self):
        self.x = defaultdict()

YAML = ruamel.yaml.YAML(typ="safe")
YAML.register_class(Foo)
# YAML.register_class(defaultdict)
fp =  Path("/temp/test.yaml")
YAML.dump(Foo(), fp)
YAML.load(fp)

EDIT: A much more elegant solution is to simply add the SafeRepresenter.represent_dict method as a representer for defaultdict:

from ruamel.yaml import representer
representer.SafeRepresenter.add_representer(defaultdict, representer.SafeRepresenter.represent_dict)

There is now a package ruamel.yaml.pytypes that supports dumping defaultdict instances. Please note that if you provide a function as parameter (for the default_factory) you will need to specify typ='unsafe' as otherwise your factory function cannot be represented.

After installing ruamel.yaml.pytypes and ruamel.yaml in your virtualenv, you can do:

yaml = ruamel.yaml.YAML(typ=['unsafe', 'pytypes'])
yaml.default_flow_style = False
buf = ruamel.yaml.compat.StringIO()

def factory():
    import datetime
    return datetime.datetime.now()

data = defaultdict(factory)

x = data[4]
data[2] = 42
yaml.dump(data, buf)
print(buf.getvalue(), end='')
d = yaml.load(buf.getvalue())
assert data == d
assert data.default_factory == d.default_factory

the above will print (your datetime will be different).

!defaultdict
- !!python/name:__main__.factory 
- 2: 42
  4: 2019-08-19 13:06:05.129019

(and the assert will not throw an exception)


See the edit history for "manual" ways to achieve similar results.

Related