- Added support for the `dict.pop() and dict.popitem()` methods

to the :class:`.mutable.MutableDict` class.
fixes #3605

(cherry picked from commit 5710a1e88b)
This commit is contained in:
Mike Bayer
2015-12-11 11:21:46 -05:00
parent 601e873045
commit 9b7870dd59
3 changed files with 50 additions and 0 deletions
+8
View File
@@ -18,6 +18,14 @@
.. changelog::
:version: 1.0.10
.. change::
:tags: bug, ext
:tickets: 3605
:versions: 1.1.0b1
Added support for the ``dict.pop()`` and ``dict.popitem()`` methods
to the :class:`.mutable.MutableDict` class.
.. change::
:tags: change, tests
:versions: 1.1.0b1
+10
View File
@@ -658,6 +658,16 @@ class MutableDict(Mutable, dict):
dict.update(self, *a, **kw)
self.changed()
def pop(self, key):
result = dict.pop(self, key)
self.changed()
return result
def popitem(self):
result = dict.popitem(self)
self.changed()
return result
def clear(self):
dict.clear(self)
self.changed()
+32
View File
@@ -136,6 +136,38 @@ class _MutableDictTestBase(_MutableDictTestFixture):
eq_(f1.data, {'a': 'z'})
def test_pop(self):
sess = Session()
f1 = Foo(data={'a': 'b', 'c': 'd'})
sess.add(f1)
sess.commit()
eq_(f1.data.pop('a'), 'b')
sess.commit()
eq_(f1.data, {'c': 'd'})
def test_popitem(self):
sess = Session()
orig = {'a': 'b', 'c': 'd'}
# the orig dict remains unchanged when we assign,
# but just making this future-proof
data = dict(orig)
f1 = Foo(data=data)
sess.add(f1)
sess.commit()
k, v = f1.data.popitem()
assert k in ('a', 'c')
orig.pop(k)
sess.commit()
eq_(f1.data, orig)
def test_setdefault(self):
sess = Session()