WestHealth / pyvis

Python package for creating and visualizing interactive network graphs.
http://pyvis.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
940 stars 162 forks source link

Numeric string node ID silently cast from `str` to `int` in in `add_nodes` but not `add_edges`, raises `AssertionError` #276

Open norweeg opened 3 months ago

norweeg commented 3 months ago
from pyvis.network import Network
from itertools import combinations

nodes = [str(n) for n in range(10)]
edges = list(combinations(nodes, 2))

nt = Network()

# casts all node IDs from str to int silently for no reason other than it can
nt.add_nodes(nodes)
# node IDs not cast to int, raises AssertionError indicating that nodes are missing
# even though the node IDs are the same datatype in the source
nt.add_edges(edges)
python 3.12.1
ipython 8.21.0
ipykernel 6.29.2
jupyter_client 8.6.0
pyvis 0.3.1

Just my $0.02, pyvis should not be type-casting node IDs at all and especially not silently and inconsistently. I spent hours beating my head into the keyboard trying to figure out what was wrong with my code and why I was getting an exception for a missing node when adding edges to my network. I thought that I was somehow filtering my dataset in such a way that I was losing a node that would be in an edge. Additionally, AssertionError is inappropriate to use here because it is disabled if the Python interpreter is running in any optimization mode. Raising TypeError would be more appropriate as it will not be disabled if the Python interpreter is running in an optimization mode.