Provide parameter for set_node_attributes() to throw key errors #7296
AlexHodgson
started this conversation in
Ideas
Replies: 1 comment
-
Since dict keys support some set-like operations, checking that all attributes in the key dictionary are present in the Graph should be relatively straightforward, something like: >>> G = nx.Graph()
>>> G.add_node(0)
>>> attr_dict = {0: {'name': 'foo'}, 1: {'name': 'bar'}}
>>> len(attr_dict.keys() - set(G)) # should == 0 for the case you're describing Since this is a one-liner, my preference would be that performing this check is on the user. OTOH, since the user has to know that |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello all,
I see there has been discussion on this topic in the past (#2434, #4341), and the decision was made to fail silently if you attempt to set an attribute on a node that doesn't exist in the graph. This seems rather risky and could lead to some difficult to find bugs if someone was expcting that their values are getting mapped, and was unaware of the unusual error handling of this function.
For my current usecase it is essential every attribute I pass in gets assigned to a node, and I would get errornous results if a node is missing the attribute. So I would like to be sure that everything I pass in has been assigned, and en error to be thrown if I made a mistake in labelling the attributes. I see there were valid reasons for choosing to hide this error, but would it be possible to add an optional paramater to raise the exception if
raise_errors=True
for example.I had a look at the function and this would be a super quick change so I can make a PR if the community agrees.
Beta Was this translation helpful? Give feedback.
All reactions