Return the point from HNSW.setdefault, not the internal _Node#330
Open
chuenchen309 wants to merge 1 commit into
Open
Return the point from HNSW.setdefault, not the internal _Node#330chuenchen309 wants to merge 1 commit into
chuenchen309 wants to merge 1 commit into
Conversation
`HNSW` implements `MutableMapping` with points as the values, and every other accessor (`__getitem__`, `get`, `pop`, `popitem`, `items`, `values`) returns `node.point`. `setdefault` returned `self._nodes[key]` — the private `_Node` wrapper — violating `MutableMapping.setdefault` semantics, its own `-> np.ndarray` signature, and its docstring. Return `.point` like the sibling accessors.
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.
HNSWimplementsMutableMappingwith points as the values, and every accessor returns the point — exceptsetdefault, which returns the private_Nodewrapper:This violates
MutableMapping.setdefaultsemantics (must return the value, likedict.setdefault), the method's own-> np.ndarraysignature, and its docstring ("return its associated point ... return default").Root cause (
datasketch/hnsw.py):setdefaultreturnsself._nodes[key]instead ofself._nodes[key].point.Fix (1 line):
Verification (re-runnable from the diff): added
TestHNSW.test_setdefaultcovering all three branches (existing non-deleted key returns its point without overwriting; absent key inserts and returns the default; soft-removed key reinserts and returns the default), written type-agnostically so it also runs under theMinHashJaccard/Jaccard/ReversedEdgessubclasses. All four variants fail on the current tree (AssertionError: _Node(...)) and pass with the fix.test/test_hnsw.pygoes from 40 to 44 passing with no new failures (the 4 pre-existing collection errors are an unrelatedflaky-marker/plugin mismatch, identical before and after).Disclosure: This PR was authored by an AI coding agent (Claude Code) running on this account: the AI found the bug, ran the repro, wrote the test, and wrote this description. The human account holder reviews every change and is accountable for it. The verification above is real and re-runnable from the diff. If this isn't the kind of contribution you want, say so and I'll close it.