Skip to content

Keep SortedSet discard quiet after key changes#250

Open
CodingFeng101 wants to merge 1 commit into
grantjenks:masterfrom
CodingFeng101:codex/discard-changed-key
Open

Keep SortedSet discard quiet after key changes#250
CodingFeng101 wants to merge 1 commit into
grantjenks:masterfrom
CodingFeng101:codex/discard-changed-key

Conversation

@CodingFeng101

Copy link
Copy Markdown

Fixes #239.

What changed

SortedSet.discard() now preserves the normal discard contract when a value's key changes after insertion: if the backing SortedKeyList can no longer find the value by its current key, the sorted list is rebuilt from the backing set after the set removal.

remove() is intentionally unchanged, since it is documented to raise when removal cannot be completed.

Why

With a key function, discard() first removed the value from the backing set and then called SortedKeyList.remove(). If the key changed while stored, SortedKeyList.remove() raised ValueError, and the set/list internals were left inconsistent. The fallback only runs on that exceptional path; the normal O(log n) path remains unchanged.

Validation

  • PYTHONPATH=src python -m pytest tests\test_coverage_sortedset.py::test_discard tests\test_coverage_sortedset.py::test_discard_with_changed_key -q
  • PYTHONPATH=src python -m pytest tests\test_coverage_sortedset.py -q
  • PYTHONPATH=src python -m pytest tests -q
  • python -m py_compile src\sortedcontainers\sortedset.py tests\test_coverage_sortedset.py
  • git diff --check

Note: PYTHONPATH=src python -m pytest -q also runs docs doctests locally and fails on existing doctest context/output issues unrelated to this change; the code test suite under tests passes.

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.

ValueError when discarding values with changing sort value

1 participant