Skip to content

Reject unholdable weights in from_networkx without asking scipy - #644

Open
eriknw wants to merge 1 commit into
20-networkx-ingestionfrom
41-networkx-error-without-scipy
Open

eriknw wants to merge 1 commit into
20-networkx-ingestionfrom
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. 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 99.30%. Comparing base (7453688) to head (f85e479).

Additional details and impacted files
@@                    Coverage Diff                    @@
##           20-networkx-ingestion     #644      +/-   ##
=========================================================
+ Coverage                  97.74%   99.30%   +1.55%     
=========================================================
  Files                        106      106              
  Lines                      32930    32931       +1     
  Branches                    3827     3829       +2     
=========================================================
+ Hits                       32189    32701     +512     
+ Misses                       656      153     -503     
+ Partials                      85       77       -8     
🚀 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 2 times, most recently from 1d20b11 to 5f883b9 Compare October 29, 2026 02:01
@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch 2 times, most recently from 00d0249 to 75fcc19 Compare November 3, 2026 12:10
@eriknw
eriknw removed this pull request from stack #657 November 3, 2026 16:44
@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch from 75fcc19 to 25b9b65 Compare November 3, 2026 16:44
@eriknw
eriknw added this pull request to stack #660 November 3, 2026 16:45
@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch 2 times, most recently from a5e4b11 to aa83f70 Compare November 3, 2026 23:09
@eriknw
eriknw removed this pull request from stack #660 November 3, 2026 23:09
@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch 17 times, most recently from f64d00c to 1d01e55 Compare November 11, 2026 05:42
@eriknw
eriknw removed this pull request from stack #666 November 12, 2026 06:13
@eriknw
eriknw changed the base branch from 29-io-config-coverage to main November 12, 2026 06:14
@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch from 1d01e55 to 4643139 Compare November 12, 2026 06:15
@eriknw
eriknw changed the base branch from main to 20-networkx-ingestion November 12, 2026 06:16
@eriknw
eriknw added this pull request to stack #670 November 12, 2026 06:17
@eriknw
eriknw force-pushed the 41-networkx-error-without-scipy branch 5 times, most recently from b1154df to d899375 Compare November 13, 2026 04:30
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.
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