]> git.notmuchmail.org Git - notmuch/commitdiff
Add missing set methods to tagsets
authorFloris Bruynooghe <flub@devork.be>
Sun, 14 Jun 2020 18:33:55 +0000 (20:33 +0200)
committerDavid Bremner <david@tethera.net>
Mon, 15 Jun 2020 10:14:11 +0000 (07:14 -0300)
Even though we use collections.abc.Set which implements all these
methods under their operator names, the actual named variations of
these methods are shockingly missing.  So let's add them manually.

bindings/python-cffi/notmuch2/_tags.py
bindings/python-cffi/tests/test_tags.py

index 212852a88ea60c7a9887345606d612f5bdcedd64..3b14c981a4d9a09ed60430309184c0dae910a3b0 100644 (file)
@@ -110,6 +110,27 @@ class ImmutableTagSet(base.NotmuchObject, collections.abc.Set):
     def __eq__(self, other):
         return tuple(sorted(self.iter())) == tuple(sorted(other.iter()))
 
     def __eq__(self, other):
         return tuple(sorted(self.iter())) == tuple(sorted(other.iter()))
 
+    def issubset(self, other):
+        return self <= other
+
+    def issuperset(self, other):
+        return self >= other
+
+    def union(self, other):
+        return self | other
+
+    def intersection(self, other):
+        return self & other
+
+    def difference(self, other):
+        return self - other
+
+    def symmetric_difference(self, other):
+        return self ^ other
+
+    def copy(self):
+        return set(self)
+
     def __hash__(self):
         return hash(tuple(self.iter()))
 
     def __hash__(self):
         return hash(tuple(self.iter()))
 
index f12fa1e6b94dbdc4a14ae3efe574c233e1cf7ed3..faf3947b6090f901a63f5828c962fc64e395591f 100644 (file)
@@ -50,6 +50,22 @@ class TestImmutable:
         assert 'unread' in tagset
         assert 'foo' not in tagset
 
         assert 'unread' in tagset
         assert 'foo' not in tagset
 
+    def test_isdisjoint(self, tagset):
+        assert tagset.isdisjoint(set(['spam', 'ham']))
+        assert not tagset.isdisjoint(set(['inbox']))
+
+    def test_issubset(self, tagset):
+        assert {'inbox'} <= tagset
+        assert {'inbox'}.issubset(tagset)
+        assert tagset <= {'inbox', 'unread', 'spam'}
+        assert tagset.issubset({'inbox', 'unread', 'spam'})
+
+    def test_issuperset(self, tagset):
+        assert {'inbox', 'unread', 'spam'} >= tagset
+        assert {'inbox', 'unread', 'spam'}.issuperset(tagset)
+        assert tagset >= {'inbox'}
+        assert tagset.issuperset({'inbox'})
+
     def test_iter(self, tagset):
         expected = sorted(['unread', 'inbox'])
         found = []
     def test_iter(self, tagset):
         expected = sorted(['unread', 'inbox'])
         found = []
@@ -78,18 +94,30 @@ class TestImmutable:
         assert isinstance(common, set)
         assert isinstance(common, collections.abc.Set)
         assert common == {'unread'}
         assert isinstance(common, set)
         assert isinstance(common, collections.abc.Set)
         assert common == {'unread'}
+        common = tagset.intersection({'unread'})
+        assert isinstance(common, set)
+        assert isinstance(common, collections.abc.Set)
+        assert common == {'unread'}
 
     def test_or(self, tagset):
         res = tagset | {'foo'}
         assert isinstance(res, set)
         assert isinstance(res, collections.abc.Set)
         assert res == {'unread', 'inbox', 'foo'}
 
     def test_or(self, tagset):
         res = tagset | {'foo'}
         assert isinstance(res, set)
         assert isinstance(res, collections.abc.Set)
         assert res == {'unread', 'inbox', 'foo'}
+        res = tagset.union({'foo'})
+        assert isinstance(res, set)
+        assert isinstance(res, collections.abc.Set)
+        assert res == {'unread', 'inbox', 'foo'}
 
     def test_sub(self, tagset):
         res = tagset - {'unread'}
         assert isinstance(res, set)
         assert isinstance(res, collections.abc.Set)
         assert res == {'inbox'}
 
     def test_sub(self, tagset):
         res = tagset - {'unread'}
         assert isinstance(res, set)
         assert isinstance(res, collections.abc.Set)
         assert res == {'inbox'}
+        res = tagset.difference({'unread'})
+        assert isinstance(res, set)
+        assert isinstance(res, collections.abc.Set)
+        assert res == {'inbox'}
 
     def test_rsub(self, tagset):
         res = {'foo', 'unread'} - tagset
 
     def test_rsub(self, tagset):
         res = {'foo', 'unread'} - tagset
@@ -102,6 +130,10 @@ class TestImmutable:
         assert isinstance(res, set)
         assert isinstance(res, collections.abc.Set)
         assert res == {'inbox', 'foo'}
         assert isinstance(res, set)
         assert isinstance(res, collections.abc.Set)
         assert res == {'inbox', 'foo'}
+        res = tagset.symmetric_difference({'unread', 'foo'})
+        assert isinstance(res, set)
+        assert isinstance(res, collections.abc.Set)
+        assert res == {'inbox', 'foo'}
 
     def test_rxor(self, tagset):
         res = {'unread', 'foo'} ^ tagset
 
     def test_rxor(self, tagset):
         res = {'unread', 'foo'} ^ tagset
@@ -109,6 +141,12 @@ class TestImmutable:
         assert isinstance(res, collections.abc.Set)
         assert res == {'inbox', 'foo'}
 
         assert isinstance(res, collections.abc.Set)
         assert res == {'inbox', 'foo'}
 
+    def test_copy(self, tagset):
+        res = tagset.copy()
+        assert isinstance(res, set)
+        assert isinstance(res, collections.abc.Set)
+        assert res == {'inbox', 'unread'}
+
 
 class TestMutableTagset:
 
 
 class TestMutableTagset:
 
@@ -175,3 +213,27 @@ class TestMutableTagset:
             msg.tags.to_maildir_flags()
             flags = msg.path.name.split(',')[-1]
             assert 'F' not in flags
             msg.tags.to_maildir_flags()
             flags = msg.path.name.split(',')[-1]
             assert 'F' not in flags
+
+    def test_isdisjoint(self, tagset):
+        assert tagset.isdisjoint(set(['spam', 'ham']))
+        assert not tagset.isdisjoint(set(['inbox']))
+
+    def test_issubset(self, tagset):
+        assert {'inbox'} <= tagset
+        assert {'inbox'}.issubset(tagset)
+        assert not {'spam'} <= tagset
+        assert not {'spam'}.issubset(tagset)
+        assert tagset <= {'inbox', 'unread', 'spam'}
+        assert tagset.issubset({'inbox', 'unread', 'spam'})
+        assert not {'inbox', 'unread', 'spam'} <= tagset
+        assert not {'inbox', 'unread', 'spam'}.issubset(tagset)
+
+    def test_issuperset(self, tagset):
+        assert {'inbox', 'unread', 'spam'} >= tagset
+        assert {'inbox', 'unread', 'spam'}.issuperset(tagset)
+        assert tagset >= {'inbox'}
+        assert tagset.issuperset({'inbox'})
+
+    def test_union(self, tagset):
+        assert {'spam'}.union(tagset) == {'inbox', 'unread', 'spam'}
+        assert tagset.union({'spam'}) == {'inbox', 'unread', 'spam'}