Skip to content

Reject a user-defined dtype in from_networkx - #647

Open
eriknw wants to merge 1 commit into
43-networkx-to-networkx-nodesfrom
46-networkx-udt-dtype
Open

eriknw wants to merge 1 commit into
43-networkx-to-networkx-nodesfrom
46-networkx-udt-dtype

Conversation

@eriknw

@eriknw eriknw commented Oct 19, 2026

Copy link
Copy Markdown
Member

With a UDT dtype=, numpy read the weights as the fields or elements of one
UDT value, so the result depended on the number of weights: FP64[2] with two
edges, or INT64[3] with three, came back as an FP64 or INT64 matrix without any
error, and other counts raised unrelated numpy errors. It now raises
ValueError: dtype must be a built-in bool, int, float or complex type, not the UDT ..., and the docstring says so. The structural path's own UDT guard, added by the PR
below, goes away: this check covers it. Built-in dtypes are unchanged.

@eriknw
eriknw added this pull request to stack #650 October 19, 2026 07:50
@eriknw
eriknw force-pushed the 46-networkx-udt-dtype branch from 2a5ae73 to 54905a2 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.69%. Comparing base (37b4eb4) to head (39ce7be).

Additional details and impacted files
@@                        Coverage Diff                        @@
##           43-networkx-to-networkx-nodes     #647      +/-   ##
=================================================================
- Coverage                          98.78%   98.69%   -0.10%     
=================================================================
  Files                                105      105              
  Lines                              30393    30406      +13     
  Branches                            3540     3542       +2     
=================================================================
- Hits                               30025    30008      -17     
- Misses                               243      258      +15     
- Partials                             125      140      +15     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eriknw
eriknw force-pushed the 46-networkx-udt-dtype branch 3 times, most recently from 4511725 to ba7bc56 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 46-networkx-udt-dtype branch from ba7bc56 to 55b90f0 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 46-networkx-udt-dtype branch from 55b90f0 to 8d3488d Compare October 28, 2026 13:21
from_networkx passed dtype= to np.array over the sequence of weights.
For a UDT, numpy read that sequence as the fields or elements of one
value of the type, so the result depended on how many weights there
were: FP64[2] with two edges, or INT64[3] with three, came back as an
FP64 or INT64 matrix without a word, a record type raised "could not
assign tuple of length 3 to structure with 2 fields", and other counts
raised a numpy IndexError or a broadcast error. The scipy route this
path replaced had the same fault (FP64[2] with one undirected edge gave
FP64 there).

A weight is one number, so a UDT has nothing to hold it as. Raise a
ValueError naming the type before any conversion, and document dtype=
as a built-in bool, int, float or complex type. The structural path no
longer needs its own guard against non-numeric dtypes.

The test covers a record type and two array types, with and without
weight, for one to three edges; on the parent 4 of its 18 cases built a
matrix, 4 raised IndexError and 10 raised ValueError with an unrelated
message. Built-in dtypes are unaffected: 124 conversions compared with
the parent are identical.
@eriknw
eriknw force-pushed the 46-networkx-udt-dtype branch from 8d3488d to 39ce7be 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