Skip to content

Construction of OutOfOrderTableProxy can cause newlines to be inserted #343

@robbotorigami

Description

@robbotorigami

Minimal example for reproduction:

def test_no_whitespace():
    content = dedent("""\
    [a.b.c.d]
    [unrelated]
    [a.b.e]
    """)

    doc = loads(content)
    doc['a']
    assert dumps(doc) == content

The value of dumps(doc) is:

[a.b.c.d]
[unrelated]

[a.b.e]

The newline is added in tomlkit/container.py:187, when the [a.b.e] block is merged in with the [a.b.c.d] block. Setting the parsed flag in the container prior to the items being merged in seems to be a minimally invasive way to fix it, but I don't understand the nuance here to know if it's a good solution. I.e.

# Create a new element to replace the old one
current = copy.deepcopy(current)
for k, v in item.value.body:
current.append(k, v)
self._body[
(
current_idx[-1]
if isinstance(current_idx, tuple)
else current_idx
)
] = (current_body_element[0], current)
becomes

current = copy.deepcopy(current)
current.value.parsing(True)
for k, v in item.value.body: 
    current.append(k, v) 
...

If this is an acceptable fix I'm happy to make a PR with the test and change, otherwise I'm happy to fix it in a different way or defer to someone with more knowledge of the library's inner workings haha.

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