more-itertools: bucket and phantom keys
Merged into more-itertools. Testing a key against a bucket permanently added that key to the ones iteration returns, so a membership test and a list of the keys disagreed for the same object at the same moment. I reported the issue and sent the fix.
What was wrong
The cache behind a bucket was a defaultdict, and the lookup path opened by reading it. Reading a missing key inserts an empty entry, and iteration reports every cache key, so a plain membership test created a key that then showed up forever. Misses were sticky and accumulated.
The effect reached the documented behavior. The docstring own keys example returned four keys after a single membership test that should have found nothing, because that test had quietly registered the key it was asking about.
The change
The cache becomes a plain dict, so a lookup can no longer insert. A key is registered only where an item with that key is actually seen, through setdefault on the two paths that store an item and one more on the path that hands an item straight to the caller.
That last path is the one worth flagging. It yields a match without ever caching it, so without an explicit registration the key would go unreported and break an existing test that drains every bucket first. Registering at the point the first matching item is produced records the key because an item had it, not because someone asked about it.
Tests
- Four tests added, covering membership, indexing, a validator, and the rule that the keys are only those actually seen.
- All four are confirmed to fail without the change and pass with it.
- The full suite is 924 tests and all pass, with the more.py and recipes.py doctests clean.