FrankenNetworkX

Guide/Start here

Migration From NetworkX: Patterns

1 min readFrom the README

On this page 7
Pattern: import networkx as nx → no change (backend mode)Pattern: import networkx as nx → import franken_networkx as nx (standalone mode)Pattern: type checks on nx.GraphPattern: picklingPattern: subclassing nx.GraphPattern: NetworkX-only algorithms via fallbackPattern: existing test suites

A reference catalog of common NetworkX patterns and their fnx equivalents. The migration is almost always trivial; the patterns below cover the edge cases that aren't.

Pattern: import networkx as nx → no change (backend mode)

# Before
import networkx as nx
G = nx.karate_club_graph()
print(nx.shortest_path(G, 0, 33))

# After (just enable the backend at startup)
import networkx as nx
nx.config.backend_priority = ["franken_networkx"]   # ← one line
G = nx.karate_club_graph()
print(nx.shortest_path(G, 0, 33))

Pattern: import networkx as nx → import franken_networkx as nx (standalone mode)

# Before
import networkx as nx

# After: drop-in alias, NetworkX still importable for exception classes
import franken_networkx as nx

This works unchanged for paths marked present in docs/coverage.md. Paths marked partial may still execute through a wrapper or NetworkX fallback, but their documented signature, binding-kind, or value gap means they are not claimed as drop-in portable.

Pattern: type checks on nx.Graph

# Before
if isinstance(g, nx.Graph): ...

# After: fnx graphs are NOT subclasses of nx.Graph; use duck typing or
# the __networkx_backend__ attribute fnx graphs carry.
if hasattr(g, "__networkx_backend__") or isinstance(g, nx.Graph):
    ...
# Or convert at the boundary:
if not isinstance(g, nx.Graph):
    g = nx.Graph(g.edges(data=True))   # or use fnx.node_link helpers

Pattern: pickling

# Before
import pickle
pickle.dumps(nx.path_graph(5))   # works

# After
import pickle, franken_networkx as fnx
pickle.dumps(fnx.path_graph(5))  # also works; fnx graphs are pickleable

Pattern: subclassing nx.Graph

# Before
class MyGraph(nx.Graph):
    def my_method(self): ...

# After: fnx.Graph is a PyO3 #[pyclass(subclass)] so Python subclassing
# works, but you can't override Rust-side methods. Override at the
# Python wrapper layer (the public algorithm surface).
class MyGraph(fnx.Graph):
    def my_method(self): ...

Pattern: NetworkX-only algorithms via fallback

# Before: calling an algorithm fnx doesn't natively support
import networkx as nx
nx.config.backend_priority = ["franken_networkx"]
# nx will fall through to its own implementation for unsupported algorithms
result = nx.some_obscure_algorithm(G)   # → pure-Python nx

If you want to force the nx implementation for a single call (e.g., for an A/B comparison), drop the backend= kwarg or override backend_priority temporarily with nx.config(backend_priority=[]): context manager.

Pattern: existing test suites

Run your existing nx-based test suite against fnx by setting the backend priority in conftest.py:

# tests/conftest.py
import networkx as nx
import franken_networkx  # ensure the entry point registers
nx.config.backend_priority = ["franken_networkx"]

If any test fails after the change, you've found either a fnx bug worth reporting or an instance of docs/upstream_divergence_ledger.md your code was relying on.