Repository navigation
MAINT: Switch to abc-based isinstance checks in to_networkx_graph - #4136
Conversation
Switch to using ABC-based isinstance checks in to_networkx_graph. Note: order of execution matters - most general check must be last.
dschult
left a comment
There was a problem hiding this comment.
This looks good -- it covers more list-like objects than what we were doing before.
Does a custom-container have to register somehow to be declared an instance of collections.abc.Container? I think that changed at some version of python and its
probably much better now. I'll look it up but it doesn't affect this PR from being merged.
My understanding is that though authors can register or explicitly derive from an appropriate ABC (it's generally recommended to do so), it's not required for >>> class MyCollection: # A custom (unregistered, underived) collection
... def __contains__(self, other): # Container
... pass
... def __len__(self): # Sized
... pass
... def __iter__(self): # Iterable
... pass
>>> foo = MyCollection()
>>> from collections.abc import Collection
>>> issubclass(MyContainer, Container)
True
>>> isinstance(foo, Collection)
TrueNote: I'm not sure if this behavior has changed over different Python versions. |
* upstream/master: Update apt-get on circleci image (networkx#4147) MAINT: fixups to parse_edgelist. (networkx#4128) Use dict instead of OrderedDict since dict is ordered by default from Python 3.6. (networkx#4145) MAINT: Switch to abc-based isinstance checks in to_networkx_graph (networkx#4136) Add tree isomorphism (networkx#4067) Updated draw_networkx to accept numpy array for edgelist (networkx#4132) CI: temporary fix for CI latex installation issues (networkx#4131) Fix parse_edgelist behavior with multiple attributes (networkx#4125)
…tworkx#4136) * MAINT: use ABCs in conversion function. Switch to using ABC-based isinstance checks in to_networkx_graph. Note: order of execution matters - most general check must be last. * DOC: Update docstring
Refactors
to_networkx_graphto use ABCs fromcollections.abcfor theisinstancechecks which map the input data to the corresponding sub-function that does the conversion. The main improvement is that this replaces a hand-curated list of types (list, set, tuple, etc.) with the more general ABCs. A couple points that need special attention:hasattrchecks for_adjdictandnextthough I'm not sure which kinds of objects they were designed to check. The test suite didn't complain about this, but if there are objects/types that depended on these (that perhaps aren't tested) please LMK.if(right before the finalraisewhen the input type isn't recognized).Closes #3945