python-poetry / python-poetry/tomlkit

`ParseError` exception when handling TOML AoT

Open
#437 1 comment 0 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

Hi!

I'm trying to parse a TOML document with tomlkit version 0.13.3 and getting this exception:
tomlkit.exceptions.ParseError: Key "fruits" already exists.

The document seems valid to me.

Code

import tomlkit


DOCUMENT = """
[[shops.aaa.fruits]]
id = "apple"

[[shops.bbb.fruits]]
id = "orange"

[[shops.aaa.fruits]]
id = "banana"
"""

parsed = tomlkit.parse(DOCUMENT)

print(tomlkit.dumps(parsed))

Expected

The document is parsed successfully.

Actual

Parsing fails with a ParseError.

Stacktrace

Traceback (most recent call last):
  File "/Users/wfzx/dev/py/toml/venv/lib/python3.12/site-packages/tomlkit/parser.py", line 161, in parse
    body.append(key, value)
  File "/Users/wfzx/dev/py/toml/venv/lib/python3.12/site-packages/tomlkit/container.py", line 252, in append
    current.append(k, v)
  File "/Users/wfzx/dev/py/toml/venv/lib/python3.12/site-packages/tomlkit/items.py", line 1632, in append
    _item = self._value[key]
            ~~~~~~~~~~~^^^^^
  File "/Users/wfzx/dev/py/toml/venv/lib/python3.12/site-packages/tomlkit/container.py", line 652, in __getitem__
    item = self.item(key)
           ^^^^^^^^^^^^^^
  File "/Users/wfzx/dev/py/toml/venv/lib/python3.12/site-packages/tomlkit/container.py", line 476, in item
    return OutOfOrderTableProxy(self, idx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/wfzx/dev/py/toml/venv/lib/python3.12/site-packages/tomlkit/container.py", line 846, in __init__
    self._internal_container._raw_append(k, v)
  File "/Users/wfzx/dev/py/toml/venv/lib/python3.12/site-packages/tomlkit/container.py", line 319, in _raw_append
    raise KeyAlreadyPresent(key)
tomlkit.exceptions.KeyAlreadyPresent: Key "fruits" already exists.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/wfzx/dev/py/toml/main.py", line 15, in <module>
    parsed = tomlkit.parse(DOCUMENT)
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/wfzx/dev/py/toml/venv/lib/python3.12/site-packages/tomlkit/api.py", line 93, in parse
    return Parser(string).parse()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/wfzx/dev/py/toml/venv/lib/python3.12/site-packages/tomlkit/parser.py", line 163, in parse
    raise self.parse_error(ParseError, str(e)) from e
tomlkit.exceptions.ParseError: Key "fruits" already exists. at line 9 col 0

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

Reproduce the failure with the TOML document from the issue, then trace the parse flow through parser.py, container.py, and items.py using the supplied stack trace. The fix is complete when the document parses successfully and tomlkit.dumps(parsed) produces output without the ParseError.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.