Skip to content

Fix OrderedSet.index after removal and reinsertion - #27

Open
Gonghan-Princess wants to merge 1 commit into
grantjenks:masterfrom
Gonghan-Princess:codex/fix-orderedset-index
Open

Fix OrderedSet.index after removal and reinsertion#27
Gonghan-Princess wants to merge 1 commit into
grantjenks:masterfrom
Gonghan-Princess:codex/fix-orderedset-index

Conversation

@Gonghan-Princess

Copy link
Copy Markdown

OrderedSet.index() returns the insertion counter stored in _keys, which no longer matches an element's sequence position after earlier elements are removed. For example, s = OrderedSet('abc'); s.discard('a'); s.index('c') returns 2 even though list(s) is ['b', 'c'], so s[s.index('c')] raises IndexError.

Look up the stored counter's current position in the existing SortedDict index. This also fixes clearing and repopulating the set, and discarding then reinserting an element. Missing values continue to raise ValueError.

Validation on Python 3.12.10 with sortedcontainers 2.4.0:

  • All five new regression cases fail on the unmodified code and pass with the fix.
  • python -m pytest: 69 passed, 100% line and branch coverage (the configured threshold).
  • isort passes; Pylint exits successfully at 10.00/10, with existing legacy configuration diagnostics.
  • Checked against a clean archive of the same base: Flake8 reports the same two existing long lines; mypy reports the same existing ItemsView/Sequence.__contains__ incompatibility. Blue proposes exactly the same formatting changes to the same eight existing files. No check configuration was relaxed.

The project's older Python/OS matrix was not run locally.

AI assistance was used to investigate, implement, test and review this change and draft this description. No third-party code or assets were copied into the patch.

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