Skip to content

Reject unholdable weights in from_networkx without asking scipy - #644

Open
eriknw wants to merge 1 commit into
29-io-config-coveragefrom
41-networkx-error-without-scipy
Open

eriknw wants to merge 1 commit into
29-io-config-coveragefrom
41-networkx-error-without-scipy

Conversation

@eriknw

@eriknw eriknw commented Oct 19, 2026

Copy link
Copy Markdown
Member

For strings, objects, ints beyond 64 bits and uniform-length sequences,
from_networkx still called nx.to_scipy_sparse_array only so that scipy would
raise. The error therefore depended on scipy: ValueError since 1.15, TypeError
or NetworkXError before, and ModuleNotFoundError from inside networkx with
scipy not installed. The fallback never returned a matrix, so it is removed and
the ValueError is raised directly:

edge weights must be bool, int (at most 64 bits), float or complex scalars;
attribute 'weight' gives dtype <U1

from_networkx no longer touches scipy at all. The docstring gains a Raises
section. Tests: four weight kinds on four graph classes, with scipy importable
and with it blocked through sys.modules (a small _block_scipy(monkeypatch)
helper), replacing the tests that skipped without scipy, including the two
added by the PR below that exercised the removed fallback. Only the message
changes when scipy is installed.

@eriknw
eriknw added this pull request to stack #650 October 19, 2026 07:50
@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch from 571d77d to f53eab1 Compare October 19, 2026 07:55
@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.02%. Comparing base (556052b) to head (5f883b9).

Additional details and impacted files
@@                    Coverage Diff                    @@
##           29-io-config-coverage     #644      +/-   ##
=========================================================
- Coverage                  98.78%   98.02%   -0.77%     
=========================================================
  Files                        105      105              
  Lines                      30373    30355      -18     
  Branches                    3534     3536       +2     
=========================================================
- Hits                       30004    29755     -249     
- Misses                       244      361     +117     
- Partials                     125      239     +114     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch 3 times, most recently from 6113fd6 to bbd4362 Compare October 21, 2026 08:38
@eriknw
eriknw removed this pull request from stack #650 October 28, 2026 04:29
@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch from bbd4362 to d507287 Compare October 28, 2026 04:29
@eriknw
eriknw added this pull request to stack #653 October 28, 2026 04:30
@eriknw
eriknw removed this pull request from stack #653 October 28, 2026 04:31
@eriknw
eriknw added this pull request to stack #655 October 28, 2026 04:32
@eriknw
eriknw removed this pull request from stack #655 October 28, 2026 04:33
@eriknw
eriknw added this pull request to stack #657 October 28, 2026 04:33
@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch from d507287 to 1d20b11 Compare October 28, 2026 13:21
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
abf6f62 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 <U1

and drop _from_networkx_via_scipy. The docstring gains a Raises section
for this and for the NetworkXError cases it shares with
nx.to_scipy_sparse_array. Only the message changes for callers with
scipy; callers without it get the ValueError instead of an ImportError.

The rejection tests are folded into one that covers each kind of weight
on all four graph classes, with scipy importable and with it blocked
through sys.modules, so they no longer skip without scipy. The
multigraph scipy-free test blocks scipy the same way instead of
monkeypatching the removed fallback.
@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch from 1d20b11 to 5f883b9 Compare October 29, 2026 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant