Skip to content

Dumping a subelement of a parsed toml fails starting on 4rd level of nesting #411

Closed

Description

@ewjoachim

Hi :) Nice lib! <3

The shortest way to expose the issue:

from tomlkit import dumps, parse


def test_reproducer():
    a = parse("a.b.c.d='e'")["a"]
    assert a == {"b": {"c": {"d": "e"}}}  # ok
    assert dumps(a) == "b.c.d='e'"
$ pytest
____________ test_reproducer ____________

    def test_reproducer():
        a = parse("a.b.c.d='e'")["a"]
        assert a == {"b": {"c": {"d": "e"}}}  # ok
>       assert dumps(a) == "b.c.d='e'"
E       assert "c.d='e'" == "b.c.d='e'"
E
E         - b.c.d='e'
E         ? --
E         + c.d='e'

script.py:9: AssertionError
======== short test summary info ========
FAILED script.py::test_reproducer - assert "c.d='e'" == "b.c.d='e'"

  - b.c.d='e'
  ? --
  + c.d='e'
=========== 1 failed in 0.03s ===========

This doesn't happen if the dict is less nested than 4 levels, but it happens for higher levels of nesting too.

Activity

  1. ewjoachim commented on Apr 9, 2025

    @ewjoachim
    Author

    A workaround:

    tomlkit.dumps(json.loads(json.dumps(a)))

    We lose style preservation, but at least the output is valid.

  2. self-assigned this
    on Jun 5, 2025
  3. added a commit that references this issue on Jun 5, 2025
    432e02a
  4. added a commit that references this issue on Jun 5, 2025
    bd31dbd
  5. ewjoachim commented on Jun 9, 2025

    @ewjoachim
    Author

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions