Fix TwoWayDict reverse map going stale on key reassignment#6551
Open
c-tonneslan wants to merge 2 commits into
Open
Fix TwoWayDict reverse map going stale on key reassignment#6551c-tonneslan wants to merge 2 commits into
c-tonneslan wants to merge 2 commits into
Conversation
added 2 commits
May 21, 2026 07:33
TwoWayDict.__setitem__ wrote the new value into both maps but never removed the old value from the reverse map. Reassigning an existing key, e.g. d[1] = 10 then d[1] = 20, left _reverse with both 10 and 20 pointing back at 1, so get_key(10) and contains_value(10) kept reporting the stale value. Drop the old value's reverse entry when a key already exists. (The existing TODO about duplicate *values* is a separate question and is left as-is.) Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
Member
|
Your PR has been closed due to a AI policy violation. Please read the following before submitting further PRs. https://github.com/Textualize/textual/blob/main/AI_POLICY.md |
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.
TwoWayDict.__setitem__writes the new value into_forwardand_reverse, but never removes the old value from_reversewhen a key is reassigned:So
_reversestops being a true inverse of_forward—get_keyandcontains_valuekeep reporting a value the key no longer maps to.The fix drops the old value's reverse entry when the key already exists. The pre-existing
TODOabout duplicate values (two keys → one value) is a separate design question and is left untouched.Added
test_set_item_reassigning_key_clears_old_reverse_entryalongside the existingtest_set_item.Worth noting on scope:
DataTable(the in-tree user ofTwoWayDict) currently rebuilds these dicts rather than reassigning keys, so this isn't a user-visible bug today — it's a correctness hole in the utility class itself, which has an explicit two-way invariant and its own test file.