Skip to content

Preserve key order when dissoc removes many keys - #640

Closed
Eric3-jp wants to merge 1 commit into
pytoolz:masterfrom
Eric3-jp:fix-dissoc-key-order
Closed

Eric3-jp wants to merge 1 commit into
pytoolz:masterfrom
Eric3-jp:fix-dissoc-key-order

Conversation

@Eric3-jp

Copy link
Copy Markdown

dissoc can reorder the remaining keys when removing enough keys to select its set-based strategy. For example:

from collections import OrderedDict
from toolz import dissoc

source = OrderedDict((k, str(k)) for k in range(5, -1, -1))
list(dissoc(source, 0, 1, 2, 3, factory=OrderedDict))
# Before: [4, 5]; expected: [5, 4]

Iterate the original mapping and use the existing remaining-key set only for membership checks. This preserves iteration order for dictionaries and OrderedDict factories on both sides of the size heuristic, while keeping the source unchanged. The regression includes no deletions, few deletions, many deletions, and repeated/missing keys.

Validation on Windows, CPython 3.14.0:

  • New cases before the fix: 4 failed, 4 passed; afterward: all 8 passed.
  • Full module tests and doctests: 277 passed, 1 skipped; 99% coverage.
  • Benchmark tests: 19 passed (using the benchmark's Shakespeare fixture).
  • The changed implementation passes the CI pycodestyle selection; git diff --check passes.

Prepared and validated with OpenAI Codex assistance.

@Eric3-jp Eric3-jp closed this by deleting the head repository Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant