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

Default Box fails to handle singleton instances of itself

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

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
38/100
Loại issue
Lỗi
Độ rõ ràng
Khá 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 trong box/box.py tại getitem, getattr và get_default, những vị trí được hiển thị trong traceback. Tái hiện ví dụ singleton TestBox và theo dõi quá trình xây dựng default-box; hoàn thành khi một thuộc tính bị thiếu như env hoạt động đúng mà không xuất hiện KeyError đệ quy như trên.

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

Mô tả

documentation

It appears that trying to use Default Box in a singleton pattern breaks it. See below for code and traceback

from box import Box

_test_box = None

class TestBox(Box):
    def __new__(cls, *args, **kwargs):
        global _test_box
        if _test_box is None:
            _test_box = super().__new__(cls, *args, **kwargs)
        return _test_box

    def __init__(self, *args, **kwargs):
        super().__init__(default_box=True, *args, **kwargs)
        self.__test_method()

    def __test_method(self):
        _this_should_not_fail = self.env

TestBox()

Traceback is as follows

Traceback (most recent call last):
  File "/home/miversen/git-local/transcoder/venv/lib/python3.8/site-packages/box/box.py", line 488, in __getitem__
    return super().__getitem__(item)
KeyError: 'env'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/miversen/git-local/transcoder/venv/lib/python3.8/site-packages/box/box.py", line 514, in __getattr__
    value = self.__getitem__(item, _ignore_default=True)
  File "/home/miversen/git-local/transcoder/venv/lib/python3.8/site-packages/box/box.py", line 509, in __getitem__
    raise BoxKeyError(str(err)) from _exception_cause(err)
box.exceptions.BoxKeyError: "'env'"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/miversen/git-local/transcoder/venv/lib/python3.8/site-packages/box/box.py", line 516, in __getattr__
    value = object.__getattribute__(self, item)
AttributeError: 'TestBox' object has no attribute 'env'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    c1 = TestBox()
  File "test.py", line 14, in __init__
    self.__test_method()
  File "test.py", line 17, in __test_method
    _this_should_not_fail = self.env
  File "/home/miversen/git-local/transcoder/venv/lib/python3.8/site-packages/box/box.py", line 529, in __getattr__
    return self.__get_default(item, attr=True)
  File "/home/miversen/git-local/transcoder/venv/lib/python3.8/site-packages/box/box.py", line 426, in __get_default
    value = self._box_config["box_class"](**self.__box_config())
  File "test.py", line 13, in __init__
    super().__init__(default_box=True, *args, **kwargs)
KeyError: 'default_box'

Environment Details

  • Python3: Python 3.8.10
  • OS: Ubuntu 20.04.3 LTS
  • Kernel: 5.11.0-27-generic
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.