Keep SortedSet discard quiet after key changes#250
Open
CodingFeng101 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #239.
What changed
SortedSet.discard()now preserves the normal discard contract when a value's key changes after insertion: if the backingSortedKeyListcan 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 calledSortedKeyList.remove(). If the key changed while stored,SortedKeyList.remove()raisedValueError, and the set/list internals were left inconsistent. The fallback only runs on that exceptional path; the normalO(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 -qPYTHONPATH=src python -m pytest tests\test_coverage_sortedset.py -qPYTHONPATH=src python -m pytest tests -qpython -m py_compile src\sortedcontainers\sortedset.py tests\test_coverage_sortedset.pygit diff --checkNote:
PYTHONPATH=src python -m pytest -qalso runs docs doctests locally and fails on existing doctest context/output issues unrelated to this change; the code test suite undertestspasses.