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

Construction of OutOfOrderTableProxy can cause newlines to be inserted #343

Closed
robbotorigami opened this issue May 3, 2024 · 0 comments · Fixed by #347
Closed

Construction of OutOfOrderTableProxy can cause newlines to be inserted #343

robbotorigami opened this issue May 3, 2024 · 0 comments · Fixed by #347
Assignees

Comments

@robbotorigami
Copy link

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.

@frostming frostming self-assigned this May 8, 2024
frostming added a commit that referenced this issue May 8, 2024
…serted

Fixes #343

Signed-off-by: Frost Ming <me@frostming.com>
frostming added a commit that referenced this issue May 8, 2024
…serted (#347)

Fixes #343

Signed-off-by: Frost Ming <me@frostming.com>
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

Successfully merging a pull request may close this issue.

2 participants