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

TypeError: Object of type <class 'NoneType'> is not TOML serializable #39

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

Comments

@amotl
Copy link

amotl commented Sep 23, 2022

Dear Taneli,

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).

Differently than #34, 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 tomlkit at python-poetry/tomlkit#240.

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", "wb") as config_file:
    tomli_w.dump(data, config_file)

tomli-w raises an exception, but still writes the file up to the point where the error happens, when data contains any NoneType value. The test at [3] demonstrates it for easy reproduction.

@hukkin
Copy link
Owner

hukkin commented Sep 24, 2022

Hey there, thanks for the issue!

I think it's better to be strict than graceful when non serializable objects are found. If a consumer of Tomli-W needs to ignore Nones, it is trivial for them to write a wrapper function something like the following (completely untested):

def remove_nones(d):
    if isinstance(d, dict):
        return {k: remove_nones(v) for k, v in d.items() if v is not None}
    return d

def write_toml_ignoring_none(d):
    return tomli_w.dumps(remove_nones(d))

The function write_toml_ignoring_none should do what you want.

@hukkin hukkin closed this as not planned Won't fix, can't repro, duplicate, stale Oct 20, 2022
@amotl
Copy link
Author

amotl commented Nov 14, 2022

Dear Taneli,

thank you very much for taking the time to respond to my request. I think it is fine to defer ignoring NULL values to userspace.

However, you may want to revisit my comment at #39 (comment). I think it indicates that it may happen that tomli-w writes invalid/incomplete TOML.

With kind regards,
Andreas.

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

2 participants