Skip to content

Commit 91cc956

Browse files
cdgriffithjesperschlegelJesper Schlegel
authored
Version 7.3.1 (#287)
* Fixing #275 default_box_create_on_get is ignored with from_yaml (thanks to Ward Loos) * Fixing #285 Infinite Recursion when accessing non existent list index in a DefaultBox with box_dots (thanks to Jesper Schlegel) --------- Co-authored-by: jesperschlegel <jesperhschlegel@gmail.com> Co-authored-by: Jesper Schlegel <jesper.schlegel@link2.ai>
1 parent 37b9181 commit 91cc956

File tree

8 files changed

+55
-13
lines changed

8 files changed

+55
-13
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Code contributions:
3434
- Muspi Merol (CNSeniorious000)
3535
- YISH (mokeyish)
3636
- Bit0r
37+
- Jesper Schlegel (jesperschlegel)
3738

3839

3940
Suggestions and bug reporting:

CHANGES.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
Version 7.3.1
5+
-------------
6+
7+
* Fixing #275 default_box_create_on_get is ignored with from_yaml (thanks to Ward Loos)
8+
* Fixing #285 Infinite Recursion when accessing non existent list index in a DefaultBox with box_dots (thanks to Jesper Schlegel)
9+
10+
411
Version 7.3.0
512
-------------
613

box/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
__author__ = "Chris Griffith"
5-
__version__ = "7.3.0"
5+
__version__ = "7.3.1"
66

77
from box.box import Box
88
from box.box_list import BoxList

box/box.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ def _parse_box_dots(bx, item, setting=False):
101101
if char == "[":
102102
return item[:idx], item[idx:]
103103
elif char == ".":
104-
if item[:idx] in bx:
105-
return item[:idx], item[idx + 1 :]
104+
return item[:idx], item[idx + 1 :]
106105
if setting and "." in item:
107106
return item.split(".", 1)
108107
raise BoxError("Could not split box dots properly")
@@ -657,9 +656,14 @@ def __setitem__(self, key, value):
657656
if hasattr(self[first_item], "__setitem__"):
658657
return self[first_item].__setitem__(children, value)
659658
elif self._box_config["default_box"]:
660-
super().__setitem__(
661-
first_item, self._box_config["box_class"](**self.__box_config(extra_namespace=first_item))
662-
)
659+
if children[0] == "[":
660+
super().__setitem__(
661+
first_item, box.BoxList(**self.__box_config(extra_namespace=first_item))
662+
)
663+
else:
664+
super().__setitem__(
665+
first_item, self._box_config["box_class"](**self.__box_config(extra_namespace=first_item))
666+
)
663667
return self[first_item].__setitem__(children, value)
664668
else:
665669
raise BoxKeyError(f"'{self.__class__}' object has no attribute {first_item}")

box/box_list.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,17 @@ def __setitem__(self, key, value):
9494
if self.box_options.get("box_dots") and isinstance(key, str) and key.startswith("["):
9595
list_pos = _list_pos_re.search(key)
9696
pos = int(list_pos.groups()[0])
97+
if pos >= len(self) and self.box_options.get("default_box"):
98+
self.extend([None] * (pos - len(self) + 1))
9799
if len(list_pos.group()) == len(key):
98100
return super().__setitem__(pos, value)
99-
return super().__getitem__(pos).__setitem__(key[len(list_pos.group()) :].lstrip("."), value)
101+
children = key[len(list_pos.group()):].lstrip(".")
102+
if self.box_options.get("default_box"):
103+
if children[0] == "[":
104+
super().__setitem__(pos, box.BoxList(**self.box_options))
105+
else:
106+
super().__setitem__(pos, self.box_options.get("box_class")(**self.box_options))
107+
return super().__getitem__(pos).__setitem__(children, value)
100108
super().__setitem__(key, value)
101109

102110
def _is_intact_type(self, obj):

box/converters.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,19 @@ class BoxTomlDecodeError(BoxError, tomli.TOMLDecodeError): # type: ignore
109109
BOX_PARAMETERS = (
110110
"default_box",
111111
"default_box_attr",
112-
"conversion_box",
112+
"default_box_none_transform",
113+
"default_box_create_on_get",
113114
"frozen_box",
114115
"camel_killer_box",
116+
"conversion_box",
117+
"modify_tuples_box",
115118
"box_safe_prefix",
116119
"box_duplicates",
117-
"default_box_none_transform",
118-
"box_dots",
119-
"modify_tuples_box",
120120
"box_intact_types",
121+
"box_dots",
121122
"box_recast",
123+
"box_class",
124+
"box_namespace",
122125
)
123126

124127

test/test_box.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,8 @@ def test_box_kwargs_should_not_be_included(self):
13401340
bx = Box(**params)
13411341
assert bx == Box()
13421342

1343-
for param in BOX_PARAMETERS:
1344-
assert param in params
1343+
for param in params:
1344+
assert param in BOX_PARAMETERS
13451345

13461346
def test_box_greek(self):
13471347
# WARNING μ is ord 956 whereas µ is ord 181 and will not work due to python NFKC normalization

test/test_box_list.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ def test_box_list_dots(self):
204204
for key in keys:
205205
db[key]
206206

207+
def test_box_list_default_dots(self):
208+
box_1 = Box(default_box=True, box_dots=True)
209+
box_1["a[0]"] = 42
210+
assert box_1.a[0] == 42
211+
212+
box_1["b[0].c[0].d"] = 42
213+
assert box_1.b[0].c[0].d == 42
214+
215+
box_1["c[0][0][0]"] = 42
216+
assert box_1.c[0][0][0] == 42
217+
218+
box_2 = Box(default_box=True, box_dots=True)
219+
box_2["a[4]"] = 42
220+
assert box_2.a.to_list() == [None, None, None, None, 42]
221+
222+
box_3 = Box(default_box=True, box_dots=True)
223+
box_3["a.b[0]"] = 42
224+
assert box_3.a.b[0] == 42
225+
207226
def test_box_config_propagate(self):
208227
structure = Box(a=[Box(default_box=False)], default_box=True, box_inherent_settings=True)
209228
assert structure._box_config["default_box"] is True

0 commit comments

Comments
 (0)