From f85e479caa67613a88490c08a99210629056989c Mon Sep 17 00:00:00 2001 From: Erik Welch Date: Mon, 12 Oct 2026 03:05:58 -0500 Subject: [PATCH] Reject unholdable weights in from_networkx without asking scipy from_networkx builds its coo arrays directly, but for weights a matrix cannot hold (strings, objects, ints beyond 64 bits, uniform-length sequences) it still called nx.to_scipy_sparse_array, only so that scipy would raise. That made the error depend on scipy: ValueError since 1.15, a TypeError or a NetworkXError blaming the sparse format before (which abf6f620 restated as ValueError), and, with scipy not installed, a ModuleNotFoundError from inside networkx. The fallback never returned a matrix, so from_networkx otherwise had no use for scipy at all. Raise the ValueError directly, naming the attribute and what it gave: edge weights must be bool, int (at most 64 bits), float or complex scalars; attribute 'weight' gives dtype csr conversion instead: scipy raises - # TypeError ("no supported conversion for types"), which networkx 3.4+ - # wraps in a NetworkXError that blames the sparse format while - # networkx <= 3.3 lets the TypeError propagate. - # Restate it so every supported stack reports the same error for the - # same graph. The graph and nodelist checks above are the ones - # nx.to_scipy_sparse_array makes, so a NetworkXError or TypeError from - # the fallback can only be that dtype complaint. - raise ValueError( - f"scipy.sparse does not support dtype {values.dtype}; " - "edge weights must be numeric scalars" - ) from err + # which ints beyond 64 bits also give, and e.g. all-string weights, which + # infer a