Skip to content

MAINT: Switch to abc-based isinstance checks in to_networkx_graph - #4136

Merged
jarrodmillman merged 2 commits into
networkx:masterfrom
rossbar:maint/to_nx_graph
Aug 7, 2020
Merged

jarrodmillman merged 2 commits into
networkx:masterfrom
rossbar:maint/to_nx_graph

Conversation

@rossbar

@rossbar rossbar commented Aug 6, 2020

Copy link
Copy Markdown
Contributor

Refactors to_networkx_graph to use ABCs from collections.abc for the isinstance checks 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:

  • I removed the hasattr checks for _adjdict and next though 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.
  • The execution order has to be modified for this check to work. Since the ABC-based check is the most general, I moved it to the last if (right before the final raise when the input type isn't recognized).

Closes #3945

rossbar added 2 commits August 6, 2020 13:04
Switch to using ABC-based isinstance checks in to_networkx_graph.

Note: order of execution matters - most general check must be last.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rossbar

rossbar commented Aug 7, 2020

Copy link
Copy Markdown
Contributor Author

Does a custom-container have to register somehow to be declared an instance of collections.abc.Container?

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 isinstance/issubclass/etc. checks, which are still based on duck typing::

>>> 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)
True

Note: I'm not sure if this behavior has changed over different Python versions.

@jarrodmillman jarrodmillman added this to the networkx-2.5 milestone Aug 7, 2020
@jarrodmillman
jarrodmillman merged commit 522c6f8 into networkx:master Aug 7, 2020
jarrodmillman added a commit to mbruhns/networkx that referenced this pull request Aug 11, 2020
* 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)
@rossbar
rossbar deleted the maint/to_nx_graph branch February 27, 2021 05:05
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

DiGraph.__init__ does not allow sets

3 participants