toml icon indicating copy to clipboard operation
toml copied to clipboard

Not a homogeneous array

Open barkorkos opened this issue 4 years ago • 9 comments

array with multiple types not allows. for example

test = [ [10,5], 4]
or test = ["aaa", 5]

barkorkos avatar Oct 14 '20 11:10 barkorkos

Here's some context to put this in perspective. Version 0.5.0 of the TOML specification forbids arrays with mixed types, that is true. But, that requirement was dropped beginning with v1.0.0-rc.1. Conformant TOML parsers will no longer need to reject arrays for mixing types.

That said, toml._spec_ is currently set to "0.5.0". So the error for multi-type arrays still needs to be raised.

eksortso avatar Oct 15 '20 02:10 eksortso

I don't think that it has to be raised. Quoting the toml-lang README:

All implementations are strongly encouraged to become compatible with TOML 1.0.0 release candidates and provide feedback, to ease the transition to 1.0.0, when it is released.

spapanik avatar Nov 02 '20 08:11 spapanik

Good catch. Then heterogeneous array support will be needed.

eksortso avatar Nov 02 '20 12:11 eksortso

Note that no exception is raised on write, only on read, resulting in data written by the library which cannot be read back.

ksunden avatar Jan 20 '21 01:01 ksunden

PR: https://github.com/uiri/toml/pull/365

sethcall avatar May 26 '21 14:05 sethcall

Is there a reason why this hasn't been closed yet? also, how can I install the v1.0.0rc referenced in one of the above comments?

zoj613 avatar Oct 26 '22 12:10 zoj613

@zoj613, consider using tomli instead, as it implements the latest toml spec. Note that tomli is read only itself, but tomli-w is a separately installable package to do writing.

Tomli also is the toml library that was incorporated into the python standard library (as tomllib, read only) in Python 3.11 (just released this week)

ksunden avatar Oct 26 '22 15:10 ksunden

@zoj613, consider using tomli instead, as it implements the latest toml spec. Note that tomli is read only itself, but tomli-w is a separately installable package to do writing.

Tomli also is the toml library that was incorporated into the python standard library (as tomllib, read only) in Python 3.11 (just released this week)

tomli does not support the _dict kwarg in its load function, which im making heavy use of to parse the data into a custom Dict object.

zoj613 avatar Oct 26 '22 15:10 zoj613

Since the data types that tomli and tomllib apply are simple enough, you could feed the resulting configuration dict into a function that swaps out the dicts with your custom Dicts.

Assuming you have a custom Dict class CustomDict which accepts a regular dict for initialization, define the following:

def _into_CustomDict(origval):
    if isinstance(origval, dict):
        return CustomDict({k: _into_CustomDict(origval[k]) for k in origval})
    elif isinstance(origval, list):
        return [_into_CustomDict(tv) for tv in origval]
    else:
        return origval

Then run config = _into_CustomDict(config), and you have your custom dicts in place. And in Python 3.7+, the keys will be defined in the exact same order. This also assumes that your custom Dict objects don't do anything all that exotic, so make sure to test this. YMMV.

eksortso avatar Oct 27 '22 18:10 eksortso