]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [feature] IdentitySet supports the - operator
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Dec 2011 18:22:59 +0000 (13:22 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Dec 2011 18:22:59 +0000 (13:22 -0500)
as the same as difference(), handy when dealing
with Session.dirty etc. [ticket:2301]

CHANGES
lib/sqlalchemy/util/_collections.py
test/base/test_utils.py

diff --git a/CHANGES b/CHANGES
index 3988ed22a4b0fdbd20c55d4e252ad221099227c6..7ff9744a8369aa72520fa9bc4c47a93eff694be7 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -108,6 +108,10 @@ CHANGES
     this might be better as an exception but
     it's not critical either way.  [ticket:2325]
 
+  - [feature] IdentitySet supports the - operator
+    as the same as difference(), handy when dealing 
+    with Session.dirty etc. [ticket:2301]
+
 - sql
    - [bug] related to [ticket:2316], made some 
      adjustments to the change from [ticket:2261]
index 3adbf991330cd538bd42f43df396c9422d644838..1a965e30d11d4ca26397e77205fddaf31ab9a14f 100644 (file)
@@ -376,6 +376,9 @@ class IdentitySet(object):
     def clear(self):
         self._members.clear()
 
+    def __sub__(self, other):
+        return self.difference(other)
+
     def __cmp__(self, other):
         raise TypeError('cannot compare sets using cmp()')
 
index 61c7ad9d606183bca0b6d8f4384d337a9849bf06..3a7ce07f27d7ee9e972d4e6e2e9a5ee4d3ca9bac 100644 (file)
@@ -290,6 +290,19 @@ class IdentitySetTest(fixtures.TestBase):
                 ids.add(data[i])
             self.assert_eq(ids, data)
 
+    def test_dunder_sub(self):
+        IdentitySet = util.IdentitySet
+        o1, o2, o3 = object(), object(), object()
+        ids1 = IdentitySet([o1])
+        ids2 = IdentitySet([o1, o2, o3])
+        eq_(
+            ids2 - ids1,
+            IdentitySet([o2, o3])
+        )
+        
+        ids2 -= ids1
+        eq_(ids2, IdentitySet([o2, o3]))
+
     def test_basic_sanity(self):
         IdentitySet = util.IdentitySet