`Array.add_line()` doesn't serialize as documented (or expected) and can even produce syntactically invalid TOML
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 52/100
Research direction
Start with Array.add_line and follow how as_string serializes single-line arrays, then reproduce the commented example and verify the output through tomlkit.loads. Done means adding a line with a comment produces valid TOML and the resulting array serialization consistently reflects multiline behavior; add regression coverage for both direct and document serialization.
Written by the indexing model from the issue text.
Description
Update: This seems to be part of a larger problem; see my comment below for additional context.
If an Array isn't multiline, then calling its .add_line method doesn't add a trailing line separator. When the added line includes a comment, this causes .as_string and similar to produce invalid TOML:
>>> import tomlkit
>>> array = tomlkit.array()
>>> array.add_line("foo", comment="bar")
>>> print(array.as_string())
[
"foo", # bar]
>>> doc = tomlkit.document()
>>> doc.add("array", array)
{'array': ['foo']}
>>> print(doc.as_string())
array = [
"foo", # bar]
>>> tomlkit.loads(doc.as_string())
Traceback (most recent call last):
File "<python-input-7>", line 1, in <module>
tomlkit.loads(doc.as_string())
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/home/five35/.local/share/hatch/env/virtual/python-scripts/S8f9twao/python-scripts/lib/python3.14/site-packages/tomlkit/api.py", line 52, in loads
return parse(string)
File "/home/five35/.local/share/hatch/env/virtual/python-scripts/S8f9twao/python-scripts/lib/python3.14/site-packages/tomlkit/api.py", line 99, in parse
return Parser(string).parse()
~~~~~~~~~~~~~~~~~~~~^^
File "/home/five35/.local/share/hatch/env/virtual/python-scripts/S8f9twao/python-scripts/lib/python3.14/site-packages/tomlkit/parser.py", line 139, in parse
item = self._parse_item()
File "/home/five35/.local/share/hatch/env/virtual/python-scripts/S8f9twao/python-scripts/lib/python3.14/site-packages/tomlkit/parser.py", line 238, in _parse_item
return self._parse_key_value(True)
~~~~~~~~~~~~~~~~~~~~~^^^^^^
File "/home/five35/.local/share/hatch/env/virtual/python-scripts/S8f9twao/python-scripts/lib/python3.14/site-packages/tomlkit/parser.py", line 331, in _parse_key_value
val = self._parse_value()
File "/home/five35/.local/share/hatch/env/virtual/python-scripts/S8f9twao/python-scripts/lib/python3.14/site-packages/tomlkit/parser.py", line 435, in _parse_value
return self._parse_array()
~~~~~~~~~~~~~~~~~^^
File "/home/five35/.local/share/hatch/env/virtual/python-scripts/S8f9twao/python-scripts/lib/python3.14/site-packages/tomlkit/parser.py", line 612, in _parse_array
raise self.parse_error(UnexpectedCharError, self._current)
tomlkit.exceptions.UnexpectedCharError: Unexpected character: '\x00' at line 2 col 0
I think the simplest fix might be to make calling .add_line implicitly change single-line arrays to multiline. If nothing else, it doesn't make semantic sense to "add a line" to something which can't contain multiple lines, so hopefully that behavior wouldn't be surprising to anyone.
- Dominant language
- Python
- Stars
- 850
- Forks
- 163
- Avg merge
- 13m
- Merged PRs (30d)
- 2
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-poetry/tomlkit
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-poetry/tomlkit#546 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
python-poetry/tomlkit#603 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
python-poetry/tomlkit#577 ·
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
python-poetry/tomlkit#574 · 5 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
python-poetry/tomlkit#571 ·
All issues in python-poetry/tomlkit
Similar issues
-
agent-ready documentation needs-triage
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
-
documentation
Difficulty 1/5 Under an hour Newbie friendliness 91/100
-
workflow-status page template still says reusable workflows are "triggered only by workflow_call:" Open
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
instance instance add
Difficulty 1/5 Under an hour Newbie friendliness 72/100
searxng/searx-instances#939 · 1 comment ·
-
area-deployment area-integrations triage:bot-seen
Difficulty 2/5 Half a day Newbie friendliness 86/100