Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
norweeg opened this issue Apr 3, 2024 · 0 comments

Comments

@norweeg
Copy link

norweeg commented Apr 3, 2024

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.

@norweeg norweeg changed the title Numeric string node ID silently cast to in in add_nodes but not add_edges, rasies Numeric string node ID silently cast from str to int in in add_nodes but not add_edges, rasies AssertionError Apr 3, 2024
norweeg added a commit to norweeg/pyvis that referenced this issue Apr 3, 2024
* removes unnecessary and silent type conversion of numeric node IDs
* add_edges using node IDs' original type no longer raises exception
* refactored coincidental inappropriate usage of assert for type-checking/control logic in add_node and add_nodes to raise appropriate exceptions instead
@norweeg norweeg changed the title Numeric string node ID silently cast from str to int in in add_nodes but not add_edges, rasies AssertionError Numeric string node ID silently cast from str to int in in add_nodes but not add_edges, raises AssertionError Apr 3, 2024
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

No branches or pull requests

1 participant