Skip to content

KeyAlreadyPresent exception for a seemingly valid document. #148

@canburak

Description

@canburak

Following document looks like legit, but when I try to parse it I get tomlkit.exceptions.KeyAlreadyPresent: Key "scripts" already exists. exception with tomlkit 0.7.2.

document

[tool.poetry]

[tool.isort]

[tool.poetry.dev-dependencies]

[build-system]

[tool.poetry.scripts]

stacktrace

Traceback (most recent call last):
  File "/Users/ccilingi/prepos/tomlbug/tomltest.py", line 21, in <module>
    regular_dict = dict(parsed)
  File "/Users/ccilingi/Library/Caches/pypoetry/virtualenvs/tomlbug-EdMsyBII-py3.9/lib/python3.9/site-packages/tomlkit/container.py", line 559, in __getitem__
    return OutOfOrderTableProxy(self, idx)
  File "/Users/ccilingi/Library/Caches/pypoetry/virtualenvs/tomlbug-EdMsyBII-py3.9/lib/python3.9/site-packages/tomlkit/container.py", line 699, in __init__
    self._internal_container.append(k, v)
  File "/Users/ccilingi/Library/Caches/pypoetry/virtualenvs/tomlbug-EdMsyBII-py3.9/lib/python3.9/site-packages/tomlkit/container.py", line 169, in append
    current.append(k, v)
  File "/Users/ccilingi/Library/Caches/pypoetry/virtualenvs/tomlbug-EdMsyBII-py3.9/lib/python3.9/site-packages/tomlkit/items.py", line 922, in append
    self._value.append(key, _item)
  File "/Users/ccilingi/Library/Caches/pypoetry/virtualenvs/tomlbug-EdMsyBII-py3.9/lib/python3.9/site-packages/tomlkit/container.py", line 175, in append
    raise KeyAlreadyPresent(key)
tomlkit.exceptions.KeyAlreadyPresent: Key "scripts" already exists.

code

You can run the following python script to reproduce:

from tomlkit import parse
import ast
from pprint import pprint

doc = """
[tool.poetry]

[tool.isort]

[tool.poetry.dev-dependencies]

[build-system]

[tool.poetry.scripts]
"""

parsed = parse(doc)
as_str = str(parsed)
eval_dict = ast.literal_eval(as_str)
pprint(eval_dict)
regular_dict = dict(parsed)

This is the contents of eval_dict:

{'build-system': {},
 'tool': {'isort': {}, 'poetry': {'dev-dependencies': {}, 'scripts': {}}}}

Metadata

Metadata

Assignees

No one assigned

    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