Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions 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:

- Muneeb Sheikh (muneikh)

- Alexandre Decan (AlexandreDecan)
- dhilipsiva (dhilipsiva)
- MAA (FooBarQuaxx)
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
Version 7.4.1
-------------

* Allow reassigning an existing key when duplicate conversion checks are enabled.

* Fixing #303 Wrong version number (7.3.3) in 7.4.0 release (thanks to Michał Górny)

Version 7.4.0
Expand Down
2 changes: 1 addition & 1 deletion box/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ def _conversion_checks(self, item):
"""
safe_item = self._safe_attr(item)

if safe_item in self._box_config["__safe_keys"]:
if safe_item in self._box_config["__safe_keys"] and self._box_config["__safe_keys"][safe_item] != item:
dups = [f"{item}({safe_item})", f'{self._box_config["__safe_keys"][safe_item]}({safe_item})']
if self._box_config["box_duplicates"].startswith("warn"):
warnings.warn(f"Duplicate conversion attributes exist: {dups}", BoxWarning)
Expand Down
22 changes: 22 additions & 0 deletions test/test_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pickle
import platform
import shutil
import warnings
from multiprocessing import Queue
from pathlib import Path
from io import StringIO
Expand All @@ -28,6 +29,7 @@
from ruamel.yaml import YAML

from box import Box, BoxError, BoxKeyError, BoxList, ConfigBox, SBox, DDBox
from box.exceptions import BoxWarning
from box.box import _get_dot_paths, _camel_killer, _recursive_tuples # type: ignore
from box.converters import BOX_PARAMETERS

Expand Down Expand Up @@ -630,6 +632,26 @@ def test_duplicate_errors(self):
with pytest.raises(BoxError):
my_box["^a"] = 3

@pytest.mark.parametrize("policy", ["error", "warn"])
@pytest.mark.parametrize("use_attribute", [False, True])
def test_duplicate_checks_allow_reassigning_same_key(self, policy, use_attribute):
my_box = Box({"a b": 1}, box_duplicates=policy)
with warnings.catch_warnings():
warnings.simplefilter("error", BoxWarning)
if use_attribute:
my_box.a_b = 2
else:
my_box["a b"] = 2
assert my_box.to_dict() == {"a b": 2}
assert my_box.a_b == 2
if policy == "error":
with pytest.raises(BoxError):
my_box["a-b"] = 3
assert my_box.to_dict() == {"a b": 2}
else:
with pytest.warns(BoxWarning):
my_box["a-b"] = 3

def test_copy(self):
my_box = Box(movie_data, camel_killer_box=True)
bb = my_box.copy()
Expand Down