cdgriffith / cdgriffith/Box

fozen_box with lists cannot converted to YAML

Open
#272 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2.8k
Forks
135
PR merge metrics
No merged PRs in 30d

Description

## Bug

Simple example

```python
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 "", line 1, in
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:

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

which **should** actually be this:
```python
test_box.to_dict()
# {'a': ({'b': 123}, {'b': 222})}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.