Implement __deepcopy__ on ImmutableDict

Closes #13408
(grafted from cdb34d214369c682c1d8f21fcb8c143371557cb9)

--HG--
branch : 7.0
This commit is contained in:
Nicolas Évrard
2024-07-19 19:04:01 +02:00
parent 55c1728d2d
commit 0a773cfe24
2 changed files with 18 additions and 0 deletions
+12
View File
@@ -6,6 +6,7 @@ import datetime as dt
import doctest
import sys
import unittest
from copy import deepcopy
from io import BytesIO
import sql
@@ -547,6 +548,17 @@ class ImmutableDictTestCase(unittest.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(unittest.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