Implement __deepcopy__ on ImmutableDict

Closes #13408
This commit is contained in:
Nicolas Évrard committed 2024-07-19 19:04:01 +02:00
1 parent 63c6eacc89
commit 53bb613564
2 files changed
+18

No files matched your search

+12
View File
@@ -7,6 +7,7 @@ import doctest
import email.message
import sys
import unittest
from copy import deepcopy
from io import BytesIO
from unittest.mock import patch
@@ -585,6 +586,17 @@ class ImmutableDictTestCase(TestCase):
with self.assertRaises(TypeError):
d.update({'foo': 'bar'})
def test_deepcopy(self):
"deepcopying should be possible"
original = ImmutableDict(foo={'a': 1}, bar=2, baz=1.3)
copy = deepcopy(original)
self.assertEqual(original, copy)
self.assertIsNot(original, copy)
self.assertEqual(original['foo'], copy['foo'])
self.assertIsNot(original['foo'], copy['foo'])
class DomainInversionTestCase(TestCase):
"Test domain_inversion"
+6
View File
@@ -1,5 +1,7 @@
# This file is part of Tryton. The COPYRIGHT file at the toplevel of this
# repository contains the full copyright notices and license terms.
from copy import deepcopy
class ImmutableDict(dict):
@@ -8,6 +10,10 @@ class ImmutableDict(dict):
def _not_allowed(cls, *args, **kwargs):
raise TypeError("Operation not allowed on ImmutableDict")
def __deepcopy__(self, memo):
return ImmutableDict(
(k, deepcopy(v, memo=memo)) for k, v in self.items())
__setitem__ = _not_allowed
__delitem__ = _not_allowed
__ior__ = _not_allowed