backends and not_implemented_for
This is a bit related to https://github.com/networkx/networkx/issues/7623 but using a separate issue to discuss something more specific. Based on the backends docs https://networkx.org/documentation/stable/reference/backends.html
Defining a Backend Graph class
The backend must create an object with an attribute __networkx_backend__ that holds a string with the entry point name:
class BackendGraph:
__networkx_backend__ = "backend_name"
...
A backend graph instance may have a G.__networkx_cache__ dict to enable caching, and care should be taken to clear the cache when appropriatHowever, _not_implemented_for decorator is called before dispatching, causing an exception if the graph doesn't implement either is_multigraph or is_directed.
def _not_implemented_for(g):
if (mval is None or mval == g.is_multigraph()) and (
dval is None or dval == g.is_directed()
):
raise nx.NetworkXNotImplemented(errmsg)
return gWe should probably add that to documents in the short-term but wanted to discuss if this should really be necessary. This forces users to implement those methods which might not be possible in case the class passed is not part of the code base (external library). Of course we can always wrap it like nx_parallel does (https://github.com/networkx/nx-parallel/blob/main/nx_parallel/interface.py#L59) but that adds an extra layer of indirection.
What could be done here?
- Make
_not_implemented_fornot raise if is_multigraph/is_directed are not defined? - Change the order of decorators to dispatch before validating the type?
Source: networkx/networkx