[Python-checkins] Add tests and design notes for Counter subset/superset operations. (GH-17625)

Raymond Hettinger webhook-mailer at python.org
Mon Dec 16 04:54:28 EST 2019


https://github.com/python/cpython/commit/1ca8fb187eb320f87a74b82c2a20acb89f429841
commit: 1ca8fb187eb320f87a74b82c2a20acb89f429841
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-12-16T01:54:14-08:00
summary:

Add tests and design notes for Counter subset/superset operations. (GH-17625)

files:
M Lib/test/test_collections.py

diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index a2a66a7572204..92520b09bb8f2 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -2064,6 +2064,29 @@ def test_multiset_operations(self):
                 set_result = setop(set(p.elements()), set(q.elements()))
                 self.assertEqual(counter_result, dict.fromkeys(set_result, 1))
 
+    def test_subset_superset_not_implemented(self):
+        # Verify that multiset comparison operations are not implemented.
+
+        # These operations were intentionally omitted because multiset
+        # comparison semantics conflict with existing dict equality semantics.
+
+        # For multisets, we would expect that if p<=q and p>=q are both true,
+        # then p==q.  However, dict equality semantics require that p!=q when
+        # one of sets contains an element with a zero count and the other
+        # doesn't.
+
+        p = Counter(a=1, b=0)
+        q = Counter(a=1, c=0)
+        self.assertNotEqual(p, q)
+        with self.assertRaises(TypeError):
+            p < q
+        with self.assertRaises(TypeError):
+            p <= q
+        with self.assertRaises(TypeError):
+            p > q
+        with self.assertRaises(TypeError):
+            p >= q
+
     def test_inplace_operations(self):
         elements = 'abcd'
         for i in range(1000):



More information about the Python-checkins mailing list