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

Dumping should fail for lists with mixed data types #228

Closed
metatoaster opened this issue Feb 7, 2019 · 4 comments
Closed

Dumping should fail for lists with mixed data types #228

metatoaster opened this issue Feb 7, 2019 · 4 comments
Labels
component: encoder Related to serialising in `toml.dump` type: bug A confirmed bug or unintended behavior

Comments

@metatoaster
Copy link

Currently, the behavior of dumps on data that contain a list of mixed types will produce invalid toml serialization successfully instead of raising an exception. For example:

>>> import toml
>>> toml.dumps({'a': {'b': ["a", [1, 2]]}})  # b mixes string with array
'[a]\nb = [ "a", [ 1, 2,],]\n'

Which naturally result in an invalid toml document that can't be loaded back:

>>> toml.loads(toml.dumps({'a': {'b': ["a", [1, 2]]}}))
Traceback (most recent call last):
 ...
IndexError: list index out of range

Compared to the node implementation @iarna/toml which has a more compliant behavior on the same input:

> const TOML = require('@iarna/toml')
> TOML.stringify({'a': {'b': ["a", [1, 2]]}})
Error: Array values can't have mixed types
@eksortso
Copy link

eksortso commented Feb 7, 2019

The TOML standard currently does not permit mixed-type arrays. To be compliant with TOML v0 5.0, yes, heterogenous arrays must throw an exception.

It may be worth noting there's talk of allowing mixed types in arrays in future versions of the standard. It appears to be gaining traction, because of kludgey ways of working around the restriction and TOML's drift away from just being a configuration format.

So far, allowing mixed types has not been approved. But for this project and others, I would recommend maintaining logic allowing for mixed types, even if it isn't always used. Depending on how the standard evolves, the current non-compliant behavior may still be useful.

@metatoaster
Copy link
Author

The problem I clearly demonstrated here is that the loader cannot round-trip (de)serialization of a particular input; regardless of current/future specification conformity this is a problem that can manifest as bugs for downstream packages that accept user-input that somehow successfully serializes but could not then reloaded. As it is now this also forces packages depending on toml to apply additional logic that should have been provided by toml to ensure input passed to toml.dumps will produce an output that can be successfully loaded again by toml.loads. If in the future mixed types is allowed it would simply disable whatever additional checks that should be implemented inside toml.dumps.

@pradyunsg pradyunsg added component: encoder Related to serialising in `toml.dump` type: bug A confirmed bug or unintended behavior labels Apr 20, 2022
@ghost
Copy link

ghost commented May 3, 2022

It's not really an actual problem anymore, because with TOML version 1.0.0 values of different types may be mixed.
Doc (https://toml.io/en/v1.0.0#array).

Now different types can't be mixed, this problem is already illustrated here: #270

@pradyunsg
Copy link
Collaborator

Indeed!

I'll close this out in that case. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: encoder Related to serialising in `toml.dump` type: bug A confirmed bug or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants