diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-12-11 11:21:46 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-12-11 11:21:46 -0500 |
commit | 5710a1e88bf669227959ca950b56d1072520b255 (patch) | |
tree | 6950c347704c1e0620904ddabedde9ce4160a924 | |
parent | 741b8af31bb436356b9e8950c045761a0e054fe0 (diff) | |
download | sqlalchemy-5710a1e88bf669227959ca950b56d1072520b255.tar.gz |
- Added support for the ``dict.pop()`` and ``dict.popitem()`` methods
to the :class:`.mutable.MutableDict` class.
fixes #3605
-rw-r--r-- | doc/build/changelog/changelog_10.rst | 8 | ||||
-rw-r--r-- | lib/sqlalchemy/ext/mutable.py | 10 | ||||
-rw-r--r-- | test/ext/test_mutable.py | 32 |
3 files changed, 50 insertions, 0 deletions
diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst index f27183277..723e8b290 100644 --- a/doc/build/changelog/changelog_10.rst +++ b/doc/build/changelog/changelog_10.rst @@ -19,6 +19,14 @@ :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 diff --git a/lib/sqlalchemy/ext/mutable.py b/lib/sqlalchemy/ext/mutable.py index 501b18f39..88b653f60 100644 --- a/lib/sqlalchemy/ext/mutable.py +++ b/lib/sqlalchemy/ext/mutable.py @@ -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() diff --git a/test/ext/test_mutable.py b/test/ext/test_mutable.py index a6bcdc47f..ed97a0d92 100644 --- a/test/ext/test_mutable.py +++ b/test/ext/test_mutable.py @@ -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() |