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

Fix triangles to avoid using is to compare nodes #7041

Merged
merged 2 commits into from
Oct 23, 2023

Conversation

dschult
Copy link
Member

@dschult dschult commented Oct 22, 2023

Fixes #7038

Using n is not node compares whether n and node are the same object, not whether they are equal.
Thanks to a bug report in #7038 we discovered that the string "46" can sometimes be created twice and not internedso that n is not node returns True even though both are equal to "46".

The error occurs in triangles when ensuring that a node in not included in its list of neighbors (ruling out self-loops).
n is the neighbor in question and node is the node being checked. This only goes to prove that "46" is not always the same object as "46". They are the same object if they are interned.

Unfortunately figuring out when interning occurs is quite complicated and it may change. So I could not think of a good test to write for this error. So, this PR just replaces the idiom n is not node with n != node which is really how node comparisons should be written always.

I don't think we usually do backports but if we release a 3.2.1 for any reason, we should include this fix. It does not occur in v3.1.

@dschult
Copy link
Member Author

dschult commented Oct 22, 2023

It would be nice if ruff or some other linter could flag it when we use a is not b. It is hard to search for repo wide.
I found a second use in similarity.py. Luckily it was in the pure python version of the similarity functions, which presumably isn't used as much as the scipy version.

@MridulS
Copy link
Member

MridulS commented Oct 23, 2023

There is indeed a rule https://docs.astral.sh/ruff/rules/is-literal/ that should catch this.

@MridulS MridulS merged commit 35a5574 into networkx:main Oct 23, 2023
35 checks passed
@jarrodmillman jarrodmillman added this to the 3.3 milestone Oct 23, 2023
dschult added a commit to BrunoBaldissera/networkx that referenced this pull request Oct 23, 2023
* change is not to !=

* replace another is not
Alex-Markham pushed a commit to Alex-Markham/networkx that referenced this pull request Oct 26, 2023
* change is not to !=

* replace another is not
@jarrodmillman jarrodmillman mentioned this pull request Oct 27, 2023
jarrodmillman pushed a commit that referenced this pull request Oct 27, 2023
* change is not to !=

* replace another is not
@dschult dschult deleted the fix_triangles branch February 21, 2024 18:59
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
* change is not to !=

* replace another is not
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

could_be_isomorphic returns False for an isomorphic graph on 3.2
3 participants