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

ValueError: Invalid type <class 'NoneType'> #240

Closed
amotl opened this issue Sep 23, 2022 · 5 comments
Closed

ValueError: Invalid type <class 'NoneType'> #240

amotl opened this issue Sep 23, 2022 · 5 comments

Comments

@amotl
Copy link

amotl commented Sep 23, 2022

Dear @sdispater and @frostming,

thanks a stack for conceiving and maintaining this excellent library. We just wanted to let you know about an issue we just discovered when serializing NoneType values [1]. We are aware that TOML does not know about any kinds of NULL values (toml-lang/toml#921).

This issue is merely a proposal to handle NoneType values gracefully by just ignoring them on serialization. The toml package does just that [2]. We have exercised the situation with all of toml, tomlkit, and tomli-w on behalf of a unit test at [3] and also reported the same issue to the author of tomli-w at hukkin/tomli-w#39.

With kind regards,
Andreas.

[1] isarengineering/SecPi#25
[2] isarengineering/SecPi#27
[3] https://gist.github.com/amotl/4d8bc928d781a5d0fbc7f404396f8417

@amotl
Copy link
Author

amotl commented Sep 23, 2022

We also found that, when using the access pattern to directly serialize to a file,

with open("testdrive.toml", "w") as config_file:
    tomlkit.dump(data, config_file)

tomlkit raises an exception, but still writes an empty file, when data contains any NoneType value. The test at [3] demonstrates it for easy reproduction.

@jcdrubay
Copy link

jcdrubay commented Nov 14, 2022

@amotl You are suggesting to ignore the NoneType values, and I suggest to keep them as a comment.

From toml specifications "Unspecified values are invalid.", but comments are.

For instance, this dictionary: {"a": 1, "b": None} would generate this string :

a = 1
# b = None

As a workaround, I strip the dictionary from empty values:

def stripper(data):
    """
    Workaround for:
    https://github.com/sdispater/tomlkit/issues/240
    """
    new_data = {}
    for k, v in data.items():
        if isinstance(v, dict):
            v = stripper(v)
        if v not in (u"", None, {}):
            new_data[k] = v
    return new_data

@amotl

This comment was marked as resolved.

@jcdrubay
Copy link

Dear @amotl I have updated my previous reply to make it clearer. Sorry for this initial version that wasn't clear at all.

@frostming
Copy link
Contributor

None type isn't supported by TOML spec and we are not going to do things beyond that. So no special handling, just as other parsers do. Closing it now

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

No branches or pull requests

3 participants