mirror of
https://github.com/sqlalchemy/sqlalchemy.git
synced 2026-05-28 03:26:01 -04:00
- 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:
Vendored
+8
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user