Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7854b34
Preserve BoxList comments during dotted deletion and sorting
HocVoNgThai Nov 5, 2026
f142725
Merge PR #349: Preserve BoxList comments during dotted deletion and s…
cdgriffith Nov 6, 2026
fa7ca67
Version 8.0.1
cdgriffith Nov 6, 2026
c66ae27
Clear removed BoxList item comments after in-place repetition
Mrun25 Nov 7, 2026
8183d43
Preserve one-shot pairs in ConfigBox.merge_update
alexbertho Nov 8, 2026
40edc1f
Preserve BoxList comments with index protocol objects
parijat1222q Nov 9, 2026
5c19bca
Fix update() storing dotted keys literally in box_dots mode
rahul05ranjan Nov 9, 2026
d3f0d31
Make BoxList + and * return a BoxList
rahul05ranjan Nov 9, 2026
0cab758
Merge PR #351
cdgriffith Nov 11, 2026
447c641
Merge PR #353
cdgriffith Nov 11, 2026
0c93d9f
Merge PR #355
cdgriffith Nov 11, 2026
456388f
Merge PR #357: Fix update() storing dotted keys literally in box_dots…
cdgriffith Nov 13, 2026
9da6747
Merge develop into PR #359
cdgriffith Nov 13, 2026
01efaba
Merge PR #359: Make BoxList + and * return a BoxList
cdgriffith Nov 13, 2026
a12d27f
Normalize in-place BoxList repetition counts before mutation
anishpentyala Nov 12, 2026
b6743c3
Add changelog for #356/#358; BoxList * accepts __index__ and supports…
cdgriffith Nov 11, 2026
2ed8e6b
Document in-place repetition count fix
anishpentyala Nov 12, 2026
b3effff
Sort imports in test_box_list
cdgriffith Nov 11, 2026
cde9770
Add contributor credit for repetition fix
anishpentyala Nov 12, 2026
a0d5939
Add audit tests for BoxList comment sync, box_dots consistency and ro…
cdgriffith Nov 11, 2026
0ef1a6a
Test index-protocol repetition comment consistency
anishpentyala Nov 12, 2026
db1a175
Fix the 8.0.1 audit findings: frozen lists, slices, dotted paths, cop…
cdgriffith Nov 11, 2026
6558b11
Place repetition regression tests in the existing test directory
anishpentyala Nov 12, 2026
d14b81f
Revert negative dotted index data access; keep it a comment API conve…
cdgriffith Nov 11, 2026
c786e25
Apply configured insertion rules in Box.update
peterolkhov Nov 11, 2026
2eac9cf
Merge PR #361: Normalize BoxList in-place repetition counts before co…
cdgriffith Nov 13, 2026
518f65e
Merge PR #360: Apply configured insertion rules in Box.update
cdgriffith Nov 13, 2026
df55c71
Document the 8.0.1 update() changes as breaking changes in the changelog
cdgriffith Nov 13, 2026
1a35520
Credit all 8.0.1 contributors in AUTHORS and the changelog
cdgriffith Nov 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ reporting, all input is greatly appreciated!

Code contributions:

- Alexandre Bertho (alexbertho)
- parijat1222q
- Alexandre Decan (AlexandreDecan)
- dhilipsiva (dhilipsiva)
- MAA (FooBarQuaxx)
Expand Down Expand Up @@ -57,7 +59,8 @@ Code contributions:
- machines-6657 (machines-6657)
- Chris (ChrisJr404)
- gyanu2507 (gyanu2507)

- Rahul (rahul05ranjan)
- Anish Pentyala (anishpentyala)

Suggestions and bug reporting:

Expand Down Expand Up @@ -121,3 +124,7 @@ Suggestions and bug reporting:
- bwanshoom
- tan_unique (vinson0522)
- lin (theman6660)
- HocVoNgThai

- Mrunmayee Daware (Mrun25)
- Peter Olkhovets (peterolkhov)
39 changes: 38 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
Changelog
=========

Version 8.0.1
-------------

Breaking changes

These change how Box.update (and the ``|`` and ``|=`` operators, which use it) store values. Code that relied on the old behavior may see different data after upgrading. See the "Box 8.0.1" section of the Major Version Breaking Changes wiki page for before / after examples.

* Fixing #356 update() storing dotted keys literally with box_dots, they are now paths like box["a.b"] = 1 and the Box constructor. Every earlier version (6.x, 7.x, 8.0.0) stored them literally:

* Box({"a": {"x": 1}}, box_dots=True).update({"a.b": 2}) now gives {"a": {"x": 1, "b": 2}}, previously {"a": {"x": 1}, "a.b": 2}
* Box(box_dots=True).update({"a.b": 1}) now raises BoxKeyError because "a" does not exist (as box["a.b"] = 1 already did), previously it stored {"a.b": 1}. Add default_box=True to create the missing parents

(thanks to rahul05ranjan)

* update() now applies the same rules as box[key] = value and the Box constructor, previously it skipped them (thanks to peterolkhov):

* box_recast converts updated values, and raises BoxValueError when a value cannot be converted, e.g. update(n="12") with box_recast={"n": int} now stores 12, previously "12"
* camel_killer_box converts updated keys, e.g. update(TimeOut=2) now replaces time_out, previously it added a second TimeOut key
* box_duplicates checks updated keys, so "error" now raises BoxError and "warn" now warns for keys that collide on attribute access
* Box subclasses that override __setitem__ now have it called for every updated key

Changes

* Fixing #358 BoxList + and * returning a plain list instead of a BoxList (thanks to rahul05ranjan)
* Fixing #354 BoxList insert, pop, deletion and ``*=`` with an object with __index__ raising TypeError after changing the list when the list has item comments (thanks to parijat1222q and Anish Pentyala)
* Fixing ConfigBox.merge_update consuming one-shot key/value pairs before merging them (thanks to alexbertho)
* Fixing BoxList in-place repetition by zero or a negative count leaving the removed items' comments behind for new items to inherit (thanks to Mrunmayee Daware)
* Fixing a BoxList in a frozen Box only being half frozen: append, extend, insert, pop, remove, clear, sort and reverse still changed it, and it was shared with the Box it came from. A frozen Box now gets its own frozen copy, and a BoxList checks frozen_box on every change
* Fixing copy.deepcopy of a frozen Box leaving the nested Boxes mutable
* Fixing BoxList slice assignment and slice deletion dropping the item comments of every item, comments now stay with the items they were on
* Fixing from_yaml(frozen_box=True) dropping the comments on list items, frozen lists (tuples) now keep comments, and box_dots list paths such as "l[0].x" now work in a frozen Box
* Fixing Box.pop(path, default) raising IndexError for an out of range list index in a box_dots path, it returns the default like get()
* Fixing a failed dotted del creating the parent key with default_box_create_on_get
* Fixing BoxList box_dots indexes such as "[x][1]" being accepted and acting on the wrong item, they now raise BoxTypeError
* BoxList ``+`` and ``*`` (and ``n * BoxList``) keep the item comments, also those of the added BoxList, and ``*=`` repeats them
* Fixing #349 BoxList item comments moving to the wrong items after a box_dots deletion, e.g. del bl["[0]"] (thanks to HocVoNgThai)
* Fixing #349 BoxList.sort() calling the key function twice when the list has comments (thanks to HocVoNgThai)

Version 8.0.0
-------------
Expand Down
2 changes: 1 addition & 1 deletion box/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

__author__ = "Chris Griffith"
__version__ = "8.0.0"
__version__ = "8.0.1"

from box.box import Box
from box.box_list import BoxList
Expand Down
109 changes: 96 additions & 13 deletions box/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,77 @@ def _camel_killer(attr):
return re.sub(" *_+", "_", s2.lower())


_tuple_pos_re = re.compile(r"\[(\d+)\]")
_tuple_comment_index_re = re.compile(r"\[(-?\d+)\]")


class _BoxTuple(tuple):
"""
The tuple ``frozen_box`` stores a list as. Unlike a plain tuple it can carry comments (#184) and
follows ``box_dots`` list paths such as ``"[0].key"``.
"""

def __getitem__(self, item, _ignore_default=False):
if isinstance(item, str) and item.startswith("["):
list_pos = _tuple_pos_re.match(item)
if list_pos is None:
raise BoxTypeError(f"Invalid list index: {item!r}")
value = tuple.__getitem__(self, int(list_pos.group(1)))
children = item[list_pos.end() :].lstrip(".")
if not children:
return value
if _ignore_default and isinstance(value, (Box, box.BoxList, _BoxTuple)):
return value.__getitem__(children, _ignore_default=True)
return value[children]
return tuple.__getitem__(self, item)

def _comment_store(self, create: bool = True) -> Any:
store = self.__dict__.get("_box_comments")
if store is None and create:
store = self.__dict__["_box_comments"] = {}
return store

def _comment_document(self, create: bool = True) -> Any:
document = self.__dict__.get("_box_comments_document")
if document is None and create:
document = self.__dict__["_box_comments_document"] = {}
return document

def _comment_check_frozen(self):
raise BoxError("Box is frozen")

def _comment_owner(self, key):
if key is None:
return self, None
if isinstance(key, int) and not isinstance(key, bool):
index = key + len(self) if key < 0 else key
if 0 <= index < len(self):
return self, index
elif isinstance(key, str) and key.startswith("["):
list_pos = _tuple_comment_index_re.match(key)
if list_pos is not None:
index = int(list_pos.group(1))
index = index + len(self) if index < 0 else index
rest = key[list_pos.end() :].lstrip(".")
if 0 <= index < len(self):
if not rest:
return self, index
child = tuple.__getitem__(self, index)
if hasattr(child, "_comment_owner"):
return child._comment_owner(rest)
raise BoxKeyError(f"{key!r} is not in the frozen list, comments can only be attached to existing items")

def get_comment(self, key: Any = None) -> dict | None:
"""The comments of an item (see ``BoxList.get_comment``), ``None`` for the document comments."""
return _comments.get_comment(self, key)

def set_comment(self, key: Any, text: Any, position: str | None = None) -> None:
_comments.set_comment(self, key, text, position)

def del_comment(self, key: Any = None) -> None:
_comments.del_comment(self, key)


def _recursive_tuples(iterable, box_class, recreate_tuples=False, **kwargs):
out_list = []
for i in iterable:
Expand All @@ -94,7 +165,10 @@ def _recursive_tuples(iterable, box_class, recreate_tuples=False, **kwargs):
out_list.append(_recursive_tuples(i, box_class, recreate_tuples, **kwargs))
else:
out_list.append(i)
return tuple(out_list)
out = _BoxTuple(out_list)
if isinstance(iterable, box.BoxList):
iterable._comment_copy_to(out)
return out


def _to_native_tuple(iterable):
Expand Down Expand Up @@ -449,7 +523,7 @@ def __contains__(self, item):
it = self[first_item]
if isinstance(it, Box):
return it.__dots_int_key(children) in it
if isinstance(it, box.BoxList):
if isinstance(it, (box.BoxList, _BoxTuple)):
# children is the rest of the path starting with "[", e.g. "[0].x"
try:
it.__getitem__(children, _ignore_default=True)
Expand Down Expand Up @@ -523,16 +597,18 @@ def __copy__(self):
return self.copy()

def __deepcopy__(self, memodict=None):
frozen = self._box_config["frozen_box"]
config = self.__box_config()
config["frozen_box"] = False
out = self.__class__(**config)
memodict = memodict if memodict is not None else {}
memodict[id(self)] = out
# Fill it like __init__ does, still "being created", so nested Boxes get the frozen config too
frozen = self._box_config["frozen_box"]
if frozen:
out._box_config["__created"] = False
for k, v in self.items():
out[copy.deepcopy(k, memodict)] = copy.deepcopy(v, memodict)
out._box_config["__created"] = True
self.__copy_internals(out, deep=True, memo=memodict)
out._box_config["frozen_box"] = frozen
return out

def __setstate__(self, state):
Expand Down Expand Up @@ -693,7 +769,11 @@ def __convert_and_store(self, item, value):
else:
value = box.BoxList(value, **self.__box_config(extra_namespace=item))
elif isinstance(value, box.BoxList):
value.box_options.update(self.__box_config(extra_namespace=item))
if self._box_config["frozen_box"]:
# A frozen Box gets its own frozen copy, the list it was given stays as it was
value = box.BoxList(value, **self.__box_config(extra_namespace=item))
else:
value.box_options.update(self.__box_config(extra_namespace=item))
elif self._box_config["modify_tuples_box"] and isinstance(value, tuple):
value = _recursive_tuples(value, recreate_tuples=True, **self.__box_config(extra_namespace=item))
super().__setitem__(item, value)
Expand Down Expand Up @@ -721,7 +801,7 @@ def __getitem__(self, item, _ignore_default=False):
if isinstance(child, Box):
children = child.__dots_int_key(children)
if hasattr(child, "__getitem__"):
if _ignore_default and isinstance(child, (Box, box.BoxList)):
if _ignore_default and isinstance(child, (Box, box.BoxList, _BoxTuple)):
return child.__getitem__(children, _ignore_default=True)
return child[children]
if self.__camel_destructive() and isinstance(item, str):
Expand Down Expand Up @@ -822,7 +902,7 @@ def _comment_owner(self, key):
child = dict.__getitem__(self, first)
if isinstance(child, Box):
return child._comment_owner(child.__dots_int_key(children))
if isinstance(child, box.BoxList):
if isinstance(child, (box.BoxList, _BoxTuple)):
return child._comment_owner(children)
raise BoxKeyError(f"{key!r} is not in the Box, comments can only be attached to existing keys")

Expand Down Expand Up @@ -923,7 +1003,7 @@ def __delitem__(self, key):
if key not in self.keys() and self.__process_dotted_key(key):
first_item, children = _parse_box_dots(self, key)
first_item = self.__dots_int_key(first_item)
child = self[first_item]
child = self.__getitem__(first_item, _ignore_default=True)
if isinstance(child, Box):
children = child.__dots_int_key(children)
if hasattr(child, "__delitem__"):
Expand Down Expand Up @@ -986,7 +1066,7 @@ def pop(self, key, *args):
raise BoxError('pop() takes only one optional argument "default"')
try:
item = self.__getitem__(key, _ignore_default=True)
except KeyError:
except (KeyError, IndexError):
return args[0]
else:
del self[key]
Expand Down Expand Up @@ -1058,12 +1138,15 @@ def update(self, *args, **kwargs):
if single_arg:
if hasattr(single_arg, "keys"):
for k in single_arg:
self.__convert_and_store(k, single_arg[k])
self._update_store(k, single_arg[k])
else:
for k, v in single_arg:
self.__convert_and_store(k, v)
self._update_store(k, v)
for k in kwargs:
self.__convert_and_store(k, kwargs[k])
self._update_store(k, kwargs[k])

def _update_store(self, key, value):
self.__setitem__(key, value)

def merge_update(self, *args, **kwargs):
"""
Expand Down
Loading
Loading