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.
Update: This seems to be part of a larger problem; see my comment below for additional context.
If an
Arrayisn't multiline, then calling its.add_linemethod doesn't add a trailing line separator. When the added line includes a comment, this causes.as_stringand similar to produce invalid TOML:I think the simplest fix might be to make calling
.add_lineimplicitly 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.