python-poetry / python-poetry/tomlkit

Comments from original TOML end up in the wrong spot

Open
#295 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
850
Forks
162
Avg merge
13m
Merged PRs (30d)
2

Description

Hello,

I am using the tomlkit Python library to modify an existing TOML file.

The original file looks like this:

## My POST route comment
[[routes]]
method = 'POST'
path = '/my/post/path'
topic = 'my-topic.0'

## My GET route comment
[[routes]]
method = 'GET'
path = '/my/get/path/{key}'
topic = 'my-topic.1'

I add two tables to that original array of tables "[[routes]]", with method = 'POST' and path = '/my/post/path'. When I do that, the resulting file becomes:

## My POST route comment
[[routes]]
method = 'POST'
path = '/my/post/path'
topic = 'my-topic.0'

## My GET route comment

[routes.rate_limit]
enabled = true

[routes.rate_limit.requests]
per_second = 25

[[routes]]
method = 'GET'
path = '/my/get/path/{key}'
topic = 'my-topic.1'

The comment ends up in the wrong spot. What can I do to make sure it stays on top of my second "[[routes]]" such as:

## My POST route comment
[[routes]]
method = 'POST'
path = '/my/post/path'
topic = 'my-topic.0'

[routes.rate_limit]
enabled = true

[routes.rate_limit.requests]
per_second = 25

## My GET route comment
[[routes]]
method = 'GET'
path = '/my/get/path/{key}'
topic = 'my-topic.1'

This is an extract of the code I use to add my 2 tables:

# result is <class 'tomlkit.items.Table'>
route_ratelimit = tomlkit.table()
route_ratelimit.add("enabled", True)
route_ratelimit_requests = tomlkit.table()
route_ratelimit_requests.add("per_second", 25)
result.append("rate_limit", route_ratelimit)
route_ratelimit.append("requests", route_ratelimit_requests)

Thank you for your assistance.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided TOML reproducer and trace the result.append("rate_limit", route_ratelimit) and route_ratelimit.append("requests", route_ratelimit_requests) operations on the parsed array of tables. Confirm why the second route comment moves, then ensure it remains immediately above the second [[routes]] entry and rerun the example to verify the output.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.