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

fozen_box with lists cannot converted to YAML #272

Open
Eusebius1920 opened this issue Jun 13, 2024 · 0 comments
Open

fozen_box with lists cannot converted to YAML #272

Eusebius1920 opened this issue Jun 13, 2024 · 0 comments

Comments

@Eusebius1920
Copy link

Bug

Simple example

from box import Box

test_box = Box({'a': [{'b': 123}, {'b': 222}]}, frozen_box=True)
test_box.to_yaml()

This yields the following error:

ruamel.yaml.representer.RepresenterError: cannot represent an object: {'b': 123}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "box/box.py", line 1013, in box.box.Box.to_yaml
  File "box/converters.py", line 207, in box.converters._to_yaml
  File "box/converters.py", line 208, in box.converters._to_yaml
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/main.py", line 594, in dump
    return self.dump_all([data], stream, transform=transform)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/main.py", line 604, in dump_all
    self._context_manager.dump(data)
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/main.py", line 953, in dump
    self._yaml.representer.represent(data)
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 83, in represent
    node = self.represent_data(data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 105, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 318, in represent_dict
    return self.represent_mapping('tag:yaml.org,2002:map', data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 841, in represent_mapping
    node_value = self.represent_data(item_value)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 105, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 309, in represent_list
    return self.represent_sequence('tag:yaml.org,2002:seq', data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 750, in represent_sequence
    node_item = self.represent_data(item)
                ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 115, in represent_data
    node = self.yaml_representers[None](self, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 347, in represent_undefined
    raise RepresenterError(f'cannot represent an object: {data!s}')
ruamel.yaml.representer.RepresenterError: cannot represent an object: {'b': 123}

Investigation

The problem is that - when using frozen_box=True - that lists get converted to tuples. But then test_box.to_dict() does not remove the Boxes from tuples so we have this:

test_box.to_dict()
# {'a': (Box({'b': 123}), Box({'b': 222}))}

which should actually be this:

test_box.to_dict()
# {'a': ({'b': 123}, {'b': 222})}
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

1 participant