From: Mike Bayer Date: Tue, 6 Dec 2011 18:22:59 +0000 (-0500) Subject: - [feature] IdentitySet supports the - operator X-Git-Tag: rel_0_7_4~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a8dc787ff02490da17e8704ca564bf0c6b46c04d;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - [feature] IdentitySet supports the - operator as the same as difference(), handy when dealing with Session.dirty etc. [ticket:2301] --- diff --git a/CHANGES b/CHANGES index 3988ed22a4..7ff9744a83 100644 --- 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] diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py index 3adbf99133..1a965e30d1 100644 --- a/lib/sqlalchemy/util/_collections.py +++ b/lib/sqlalchemy/util/_collections.py @@ -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()') diff --git a/test/base/test_utils.py b/test/base/test_utils.py index 61c7ad9d60..3a7ce07f27 100644 --- a/test/base/test_utils.py +++ b/test/base/test_utils.py @@ -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