diff --git a/dotmap/__init__.py b/dotmap/__init__.py index c4a74bf..3aa14a3 100755 --- a/dotmap/__init__.py +++ b/dotmap/__init__.py @@ -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): diff --git a/dotmap/test.py b/dotmap/test.py index 63a41e8..6206a3d 100644 --- a/dotmap/test.py +++ b/dotmap/test.py @@ -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): @@ -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) @@ -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}})