consistent KeyErrors across backends - #332
Open
TeunHuijben 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.
Raise a consistent
KeyErrorfor missing attribute keys on all backendsAsking for an attribute key that doesn't exist raised a different exception depending on which
backend you were using:
SQLGraphAttributeErrorAttributeErrorIndexedRXGraphKeyErrorColumnNotFoundErrorRustWorkXGraphKeyErrorKeyError(butColumnNotFoundErrorviafilter())AttributeErrorleaks out of SQLAlchemy'sgetattr, and polars'ColumnNotFoundErrorisn't aKeyErrorsubclass, soexcept KeyErrordidn't catch every case. Code that worked against thein-memory backends broke as soon as you switched it to SQL.
funtracks is the reason this goes first: its tests assert
KeyErrorfor a missing attribute keyand currently fail only on SQL.
What changed
All read paths now raise
KeyError, with a message that names the unknown key and lists the validones.
BaseGraph._validate_attr_keys(attr_keys, mode). It's the read-path counterpart of theexisting
_validate_attributes, which guards writes and raisesValueError. Reads raiseKeyErrorinstead because asking for a key that isn't there is a lookup miss, likedict["missing"], while passing an undeclared key to a write is a bad argument._sql_graph.py, 6 in_rustworkx_graph.py. SQL needs fewer because all its reads funnel through one column resolver;rustworkx has separate implementations for graph-level edge reads, filter-level edge reads, and
the neighbour walk in
_get_neighbors.filter()validates at construction rather than at collect time, so a typo points at thefilter()call instead of somewhere further downstream.Covered:
node_attrs,edge_attrs,nodes[id][key],edges[id][key],filter(NodeAttr/EdgeAttr(...)),successors,predecessors, and the filter-levelnode_attrs/edge_attrs.While writing the tests I found one path that wasn't in the original bug report:
filter().edge_attrsraisedColumnNotFoundErroronRustWorkXGraphtoo, not justIndexedRXGraph.Not in scope
Other backend inconsistencies we found but are handling separately:
update_node_attrssilentlydiscarding a write with an unknown key on SQL,
add_edgecreating dangling edges on SQL, and thediffering exception types for a missing node/edge id.
Tests
filter()failing eagerly.attr_keys=None, andattr_keyspassed as a bare string.(KeyError, AttributeError)or no error at all.Full suite: 1257 passed, 13 skipped, 1 xfailed.
Performance
No measurable cost. The check is one set lookup per call against the already-cached schema dict,
not per row. A/B on
node_attrsover 20k nodes: SQL 15.4 → 14.6 ms/call, rustworkx1.61 → 1.70 ms/call, both within run-to-run noise.
Breaking change
Anyone catching
AttributeErrorfor a missing attribute key needs to catchKeyErrornow.