toml
toml copied to clipboard
Not a homogeneous array
array with multiple types not allows. for example
test = [ [10,5], 4]
or
test = ["aaa", 5]
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.
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.
Good catch. Then heterogeneous array support will be needed.
Note that no exception is raised on write, only on read, resulting in data written by the library which cannot be read back.
PR: https://github.com/uiri/toml/pull/365
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, 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)
@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.
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.