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

fozen_box with lists cannot converted to YAML

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

@apoorvdarshan đang làm issue này rồi.

Từ ngày 4/7/2026.

  • #315 của @apoorvdarshan — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu với Box.to_yaml trong box/box.py và quá trình chuyển đổi đệ quy trong box/converters.py, sau đó tái hiện ví dụ frozen_box=True từ issue. Kiểm tra rằng to_dict loại bỏ các đối tượng Box lồng nhau bên trong các giá trị tuple, và xác nhận rằng cấu trúc thu được có thể được tuần tự hóa thành YAML mà không gặp lỗi representer.

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

Mô tả

Bug

Simple example

from box import Box

test_box = Box({'a': [{'b': 123}, {'b': 222}]}, frozen_box=True)
test_box.to_yaml()

This yields the following error:

ruamel.yaml.representer.RepresenterError: cannot represent an object: {'b': 123}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "box/box.py", line 1013, in box.box.Box.to_yaml
  File "box/converters.py", line 207, in box.converters._to_yaml
  File "box/converters.py", line 208, in box.converters._to_yaml
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/main.py", line 594, in dump
    return self.dump_all([data], stream, transform=transform)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/main.py", line 604, in dump_all
    self._context_manager.dump(data)
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/main.py", line 953, in dump
    self._yaml.representer.represent(data)
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 83, in represent
    node = self.represent_data(data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 105, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 318, in represent_dict
    return self.represent_mapping('tag:yaml.org,2002:map', data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 841, in represent_mapping
    node_value = self.represent_data(item_value)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 105, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 309, in represent_list
    return self.represent_sequence('tag:yaml.org,2002:seq', data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 750, in represent_sequence
    node_item = self.represent_data(item)
                ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 115, in represent_data
    node = self.yaml_representers[None](self, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/daniel/.local/share/virtualenvs/bedrock-JuU-uWSt/lib/python3.11/site-packages/ruamel/yaml/representer.py", line 347, in represent_undefined
    raise RepresenterError(f'cannot represent an object: {data!s}')
ruamel.yaml.representer.RepresenterError: cannot represent an object: {'b': 123}

Investigation

The problem is that - when using frozen_box=True - that lists get converted to tuples. But then test_box.to_dict() does not remove the Boxes from tuples so we have this:

test_box.to_dict()
# {'a': (Box({'b': 123}), Box({'b': 222}))}

which should actually be this:

test_box.to_dict()
# {'a': ({'b': 123}, {'b': 222})}
Ngôn ngữ chính
Python
Star
2.8k
Fork
145
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

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

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 cdgriffith/Box

Tất cả issue của cdgriffith/Box

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.