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

dumps() doesn't support tuple #171

Closed
pklapperich opened this issue Feb 2, 2022 · 0 comments · Fixed by #174
Closed

dumps() doesn't support tuple #171

pklapperich opened this issue Feb 2, 2022 · 0 comments · Fixed by #174

Comments

@pklapperich
Copy link

This is almost a drop-in replacement for the toml library. But an issue I ran into is lack of support for tuples, which toml converts to lists.

In[50]: import toml
In[51]: import tomlkit
In[52]: x={'section': {'tuple_value': (1,2)}}
In[53]: tomlkit.dumps(x)
Out[53]: Traceback (most recent call last):
  File "C:\projects\foo\.venv\lib\site-packages\IPython\core\interactiveshell.py", line 3457, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-53-add147bf2e5e>", line 1, in <module>
    tomlkit.dumps(x)
  File "C:\projects\foo\.venv\lib\site-packages\tomlkit\api.py", line 49, in dumps
    data = item(dict(data), _sort_keys=sort_keys)
  File "C:\projects\foo\.venv\lib\site-packages\tomlkit\items.py", line 92, in item
    val[k] = item(v, _parent=val, _sort_keys=_sort_keys)
  File "C:\projects\foo\.venv\lib\site-packages\tomlkit\items.py", line 92, in item
    val[k] = item(v, _parent=val, _sort_keys=_sort_keys)
  File "C:\projects\foo\.venv\lib\site-packages\tomlkit\items.py", line 152, in item
    raise ValueError(f"Invalid type {type(value)}")
ValueError: Invalid type <class 'tuple'>
In[54]: toml.dumps(x)
Out[54]: '[section]\ntuple_value = [ 1, 2,]\n'

I think the way the toml library handles this is there's a fall back method that converts unrecognized iterables to lists, and so that catches tuples.

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

Successfully merging a pull request may close this issue.

1 participant