fix: honour a falsey key in all_unique

all_unique applied the key with `map(key, iterable) if key else iterable`, so a falsey callable was silently ignored. it now checks `key is not None` like the other key functions, same fix as #1309.
This commit is contained in:
dualfroz committed 2026-10-07 20:10:16 +02:00
1 parent d1d5e24692
commit 8780c614af
3 files changed
+15 -1

No files matched your search

+13
View File
@@ -5153,6 +5153,19 @@ class AllUniqueTests(TestCase):
self.assertEqual(mi.all_unique(iterable, lambda x: x), True)
self.assertEqual(mi.all_unique(iterable, str.lower), False)
def test_falsey_key_is_honoured(self):
# A falsey callable is still a valid key and must not be ignored
class CaseFold:
def __bool__(self):
return False
def __call__(self, x):
return x.lower()
key = CaseFold()
self.assertFalse(key) # Sanity: the key itself is falsey
self.assertEqual(mi.all_unique(['A', 'B', 'C', 'b'], key), False)
def test_infinite(self):
self.assertEqual(mi.all_unique(mi.prepend(3, count())), False)