New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mutates_input=
and returns_graph=
to _dispatchable
#7191
Conversation
if self._is_testing and self._automatic_backends and backend_name is None: | ||
# Special path if we are running networkx tests with a backend. | ||
return self._convert_and_call_for_tests( | ||
self._automatic_backends[0], | ||
args, | ||
kwargs, | ||
fallback_to_nx=self._fallback_to_nx, | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that I moved this code block to further below so that iterators of input graphs are turned into lists. This allows the "fallback to networkx" functionality to work even for iterator of graphs inputs (otherwise, the iterators would be consumed by the first call).
not ( | ||
args[arg_pos] | ||
if len(args) > arg_pos | ||
else kwargs.get(arg_name[4:], True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this implicitly assumes e.g. copy=True
is the default everywhere (which is true today).
mutates_input=True
to _dispatch
mutates_input=
and returns_graph=
to _dispatchable
I added Even if we don't include graph generators/constructors, there were more functions that return graphs than I was expecting! |
Aha, our tests worked and found a new function that returns a graph (and needs
This PR has been updated and is ready for review. |
Thanks! This is a good topic for dispatching, especially as we work toward getting caching in for NX 3.3
Can you explain the reason for the behavior change? My understanding is that a function that mutates the input graph - as a side effect or otherwise - will not result in those updates being propagated to the NX Graph if dispatched, and therefore dispatching the call will result in different behavior (ie. a difference that breaks user code). What I'm not clear on is what the expectation is if a user forces the conversion/dispatch using Either way, I think if a user sets
|
Chatted offline with @eriknw (Erik, please correct me as needed):
|
…x#7191) * Add `mutates_input=True` to `_dispatch` * Add `returns_graph=True` to `_dispatchable` * Don't auto-convert for functions that return Graphs; also, updates * more comments * Make `returns_graph` attribute private (for now)
Knowing which functions mutate input graphs may be helpful when implementing (or testing) caching.
This changes a behavior: if a function is known to mutate an input graph, then this does not automatically convert an input graph to a backend graph. Conversion can still happen by using
backend=
keyword.Some of the algorithms that now have
mutates_input=True
may be good candidates to addcopy=
arguments.Adding
mutates_input=
to dispatch decorator is "best effort". It's possible (even likely) that some were missed, especially functions that add data to.graph
. I think this is okay--we can fix them when found by backend implementers--but please share if you know of any other functions that mutate input graphs.I noticed that
negative_edge_cycle
andheld_karp_ascent
temporarily mutate input graphs. I was surprised by this, and it means these are not thread-safe (and who knows if they may be permanently modified if there's an exception).I also noticed that
lukes_partitioning
,is_kl_connected
, andkl_connected_subgraph
usedeepcopy
to copy the graph before mutating it. This is different than what is done elsewhere, and does not work if the input graphs have been frozen (side thought: why isn't there anunfreeze
function?).reverse
function hascopy=
argument, but it does not mutate the graph. Ifcopy=False
, then a read-only view is returned.Finally, I don't know what to do with
minimum_cut
. It can mutateresidual
ifresidual=
was passed in via**kwargs
. The dispatch machinery does not handle graphs passed in via**kwargs
. We can punt on this until a backend wants to implement it.CC @rlratzel