Skip to content
Merged
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
7 changes: 6 additions & 1 deletion dotmap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ def __len__(self):
def clear(self):
self._map.clear()
def copy(self):
return self.__class__(self, _default_factory=self._default_factory)
return self.__class__(
self,
_dynamic=self._dynamic,
_default_factory=self._default_factory,
_prevent_method_masking=self._prevent_method_masking,
)
def __copy__(self):
return self.copy()
def __deepcopy__(self, memo=None):
Expand Down
19 changes: 17 additions & 2 deletions dotmap/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ def test_none_means_non_dynamic(self):
with self.assertRaises(AttributeError):
m.missing

def test_copy_preserves_flags(self):
m = DotMap({'a': 1}, _dynamic=False, _prevent_method_masking=True)
c = m.copy()

self.assertFalse(c._dynamic)
self.assertTrue(c._prevent_method_masking)
self.assertEqual(c.a, 1)
with self.assertRaises(AttributeError):
c.missing
with self.assertRaises(KeyError):
c.get = 'mango'


class TestDefault(unittest.TestCase):
def test_missing_attribute_returns_default(self):
Expand Down Expand Up @@ -561,8 +573,8 @@ def test_toDict_returns_plain_dict(self):
self.assertNotIsInstance(d['sub'], DotMap)
self.assertEqual(d, {'a': 1, 'sub': {'b': 2}})

def test_copy_preserves_type_and_static(self):
m = StaticDotMap({'a': 1, 'sub': {'b': 2}})
def test_copy_preserves_type_and_flags(self):
m = StaticDotMap({'a': 1, 'sub': {'b': 2}}, _prevent_method_masking=True)
c = m.copy()
self.assertIsInstance(c, StaticDotMap)
self.assertIsInstance(c.sub, StaticDotMap)
Expand All @@ -572,6 +584,9 @@ def test_copy_preserves_type_and_static(self):
c.missing
with self.assertRaises(AttributeError):
c.sub.missing
self.assertTrue(c._prevent_method_masking)
with self.assertRaises(KeyError):
c.get = 'mango'

def test_deepcopy_preserves_type_and_static(self):
m = StaticDotMap({'a': 1, 'sub': {'b': 2}})
Expand Down