Skip to content

Commit

Permalink
Merge ff29a4e into 7795f88
Browse files Browse the repository at this point in the history
  • Loading branch information
cdgriffith committed Apr 26, 2019
2 parents 7795f88 + ff29a4e commit 36eda09
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions box.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@

BOX_PARAMETERS = ('default_box', 'default_box_attr', 'conversion_box',
'frozen_box', 'camel_killer_box', 'box_it_up',
'box_safe_prefix', 'box_duplicates', 'ordered_box')
'box_safe_prefix', 'box_duplicates', 'ordered_box',
'box_intact_types')

_first_cap_re = re.compile('(.)([A-Z][a-z]+)')
_all_cap_re = re.compile('([a-z0-9])([A-Z])')
Expand Down Expand Up @@ -253,7 +254,8 @@ def _get_box_config(cls, kwargs):
'camel_killer_box': kwargs.pop('camel_killer_box', False),
'modify_tuples_box': kwargs.pop('modify_tuples_box', False),
'box_duplicates': kwargs.pop('box_duplicates', 'ignore'),
'ordered_box': kwargs.pop('ordered_box', False)
'ordered_box': kwargs.pop('ordered_box', False),
'box_intact_types': tuple(kwargs.pop('box_intact_types', ())),
}


Expand All @@ -273,6 +275,7 @@ class Box(dict):
:param box_duplicates: "ignore", "error" or "warn" when duplicates exists
in a conversion_box
:param ordered_box: Preserve the order of keys entered into the box
:param box_intact_types: Keep data with given types intact
"""

_protected_keys = dir({}) + ['to_dict', 'tree_view', 'to_json', 'to_yaml',
Expand Down Expand Up @@ -460,7 +463,9 @@ def __box_config(self):
return out

def __convert_and_store(self, item, value):
if item in self._box_config['__converted']:
if (item in self._box_config['__converted'] or
(self._box_config['box_intact_types'] and
isinstance(value, self._box_config['box_intact_types']))):
return value
if isinstance(value, dict) and not isinstance(value, Box):
value = self.__class__(value, __box_heritage=(self, item),
Expand Down
9 changes: 9 additions & 0 deletions test/test_functional_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,15 @@ def test_ordered_box(self):
bx.pop('c')
bx.__delattr__('g')
assert bx.keys() == ['h', 'd']

def test_intact_types(self):
from collections import OrderedDict
bx = Box(a = OrderedDict([('y', 1), ('x', 2)]))
assert isinstance(bx.a, Box)
assert not isinstance(bx.a, OrderedDict)
bx = Box(a = OrderedDict([('y', 1), ('x', 2)]), box_intact_types = [OrderedDict])
assert isinstance(bx.a, OrderedDict)
assert not isinstance(bx.a, Box)

def test_pop(self):
bx = Box(a=4, c={"d": 3})
Expand Down

0 comments on commit 36eda09

Please sign in to comment.