Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

`Array.add_line()` doesn't serialize as documented (or expected) and can even produce syntactically invalid TOML

Đang mở
#580 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
52/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu với Array.add_line và theo dõi cách as_string tuần tự hóa các mảng một dòng, sau đó tái hiện ví dụ đã được chú thích và xác minh đầu ra thông qua tomlkit.loads. Hoàn thành khi việc thêm một dòng có chú thích tạo ra TOML hợp lệ và việc tuần tự hóa mảng kết quả phản ánh nhất quán hành vi nhiều dòng; thêm kiểm thử hồi quy cho cả việc tuần tự hóa trực tiếp và tuần tự hóa tài liệu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
850
Fork
163
Merge trung bình
13 phút
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

Chúng tôi chưa kiểm tra các tệp thiết lập môi trường của dự án này. Hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python-poetry/tomlkit

Tất cả issue của python-poetry/tomlkit

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.