Skip to content
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 Backend Protocol to aid backend developers #7369

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

aaronzo
Copy link
Contributor

@aaronzo aaronzo commented Mar 23, 2024

based on discussion in #7334. This adds a backend protocol that devs can use to varying degrees to ensure their backend conforms to networkx's contract.

How does this help backend developers?

Easiest to give examples of various patterns and options this enables. All of these snippets would live in backend codebases, and assume the import

from networkx.utils.interface import BackendInterface
  1. No inheritance, just instance checks
class Dispatcher: 
    ...

def test_dispatcher_interface():
    # check all the methods networkx can call are implemented
    assert isinstance(Dispatcher, BackendInterface)
  1. With inheritance
class Dispatcher(BackendInterface[ga.Graph]):  # optionally specify backend graph type
    # checks method existence upon class definition
    # IDE autocompletes functions with correct signatures for backend, and have information for static type checkers
    ...
  1. With inheritance and plugin entry point check
class Dispatcher(BackendInterface, plugin=True): 
    # checks method existence upon class definition
    # checks that this class is the value for an entry point in the "networkx.backends" group
    ...
  1. With inheritance, plugin entry point check and get_info check
class Dispatcher(BackendInterface, plugin=True, info=True):
    # checks method existence upon class definition
    # checks that this class is the value for an entry point in the "networkx.backends" group
    # checks that a `get_info` function is given as an entry point
    # checks that info given for the plugin is consistent with this class
    # checks that this backend implements networkx-registered algorithms
    ...
  1. With inheritance and fallback function
class Dispatcher(BackendInterface, fallback=...):
    # checks method existence upon class definition
    # provides a fallback whenever this class doesn't have an attribute or method (like in nx-loopback)
    ...

fyi @eriknw @rossbar

@aaronzo aaronzo force-pushed the backend-protocol branch 5 times, most recently from 7045547 to 83879c2 Compare March 24, 2024 01:54
@aaronzo aaronzo marked this pull request as ready for review March 24, 2024 20:54
@aaronzo aaronzo changed the title WIP: Backend protocol add Backend Protocol to aid backend developers Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dispatching Related to dispatching and backend support type: Documentation type: Maintenance
Development

Successfully merging this pull request may close these issues.

None yet

3 participants