Replies: 3 comments 4 replies
-
Sounds reasonable to me. Would you be up for making a PR with what you'd like to see? It's a lot easier for me to comment than to do it 😂. If such a protocol existed, I would update it as changes were made. To be a netowrkx backend, the installed package must have an entry point. For networkx version >= 3.2, this is done as: [project.entry-points."networkx.backends"]
backend_name = "package.module_path:interface" For networkx version < 3.2, this entry point was named The
The optional functions are:
The There is another entry point that backends may define: [project.entry-points."networkx.backend_info"]
backend_name = "package.module_path:get_info" For networkx version < 3.2, this entry point was named The purpose of this entry point is to provide information about the backend. It can also avoid importing the backend until the backend is used. Would the protocol be able to capture entry points and the items of the It is indeed kinda messy atm for a backend to support many networkx versions. @Schefflera-Arboricola created a "just for fun" example backend that was used in a discussion: This PR may also be of interest: #7305 |
Beta Was this translation helpful? Give feedback.
-
@eriknw Yeah - happy to have a go at this. I'm thinking there might be a need for a 'minimal' and a 'maximal' interface, and both might be useful. I'm not sure on how a protocol could check entry_points, I'll need to do some research there. It's certainly not a given feature of a protocol, but there's probably some trickery to be done with metaclasses. The class would need visibility of package entry points before build time, and this is where I'm unsure.
This will be another tricky thing to do if we're not simply recreating a stub of the networkx API in the protocol (and I don't think we should). There may be something we could programmatically build from the registered dispatchable functions though. Question there: What package path should a backend dispatch from? E.g. if the backend has Dispatcher.pagerank Then will networkx automatically dispatch a call to both nx.algorithms.pagerank
nx.pagerank in the same way? |
Beta Was this translation helpful? Give feedback.
-
Okay, I've made a start on this @eriknw. #7369 discussion welcome! |
Beta Was this translation helpful? Give feedback.
-
When creating a new backend, it's not clear at all what methods should be on the interface (things like
can_run
, conversions, etc.). These features are also being rapidly added - to create a fully featured and futureproof backend, a developer should be developing againstnetworkx:main
branch rather than a release since things are changing. This adds another layer of difficulty.I know networkx doesn't use typing, but I think a
typing.Protocol
would go a long way here in helping developers, even without documentation. The great thing about protocols is that you can implement them without explicitly extending them, so it's down to each backend developer whether they choose to explicitly extend this class, check in a unit test whether they conform, or ignore it altogether. On the other hand it would give developers confidence that their work in a different repo somewhere is compatible with networkx. The protocol could have handy docstrings outlining each function. It would remove the need to have to check where exactly eachhasattr
innetworkx.utils.backend
is called and how it is used.The methods in the protocol could even be typehinted. Making an exception to networkx's code style for this doesn't seem too awful given this is all happening at a metaprogramming level far away from the actual graph analytics code. Being stricter on what is prescribed from backends type-wise can only be a good thing in my opinion.
Beta Was this translation helpful? Give feedback.
All reactions