Skip to content

Add clear edges method as a method to be frozen by nx.freeze#6190

Merged
dschult merged 2 commits intonetworkx:mainfrom
AdamWRichardson:bugfix-clear-edges-not-frozen
Nov 15, 2022
Merged

Add clear edges method as a method to be frozen by nx.freeze#6190
dschult merged 2 commits intonetworkx:mainfrom
AdamWRichardson:bugfix-clear-edges-not-frozen

Conversation

@AdamWRichardson
Copy link
Copy Markdown
Contributor

…freeze function

nx.freeze was freezing most of the attributes of a graph but didn't freeze the clear_edges method. This PR adds that method into the list of methods to be frozen.

A different design choice would be to re-implement the freeze method to call into a __freeze__ method on the graph. This would allow subclasses to modify the freeze method based on their needs and would also make any future ommisions like this easier to fix in subclasses. However since this is my first time contributing I went for a minimal changes.

Happy to have comments and suggestions on this!

Copy link
Copy Markdown
Contributor

@rossbar rossbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AdamWRichardson , it does indeed seem that clear_edges not being included in the "frozen" methods is an oversight and the tests look good as well (with a minor suggestion).

Comment on lines +264 to +268
def test_node_attributes_are_still_mutable_on_frozen_graph(self):
G = nx.freeze(self.G)
node = G.nodes[0]
node["node_attribute"] = True
assert node["node_attribute"] == True
Copy link
Copy Markdown
Contributor

@rossbar rossbar Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests look good, but let's use a fresh graph instance instead of the class instance to prevent potential crosstalk with other tests, e.g.:

Suggested change
def test_node_attributes_are_still_mutable_on_frozen_graph(self):
G = nx.freeze(self.G)
node = G.nodes[0]
node["node_attribute"] = True
assert node["node_attribute"] == True
def test_node_attributes_are_still_mutable_on_frozen_graph(self):
G = nx.freeze(nx.path_graph(3))
G.nodes[0]["node_attribute"] = True
assert node["node_attribute"] == True

The same for the edge attr test as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the suggestion is missing a line to define node. But the basic idea is to refrain from freezing self.G because that will likely be used by other tests. Instead create a new graph to freeze -- like nx.path_graph(3).

Thanks for this!

Copy link
Copy Markdown
Contributor Author

@AdamWRichardson AdamWRichardson Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks for the review and comments and I have now made the relevant changes!

Has the team thought of using pytest fixtures to ensure each test is isolated?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK... now I think I'm confusing myself... :)
This is a test class with a setup_method. So self.G should be created fresh and clean for each test method. I think I am getting confused because we are switching tests (slowly over time) from class methods to functions, using pytest fixtures where appropriate. The test class system provides some of the fixture-like features. And the details of when a test class calls setup_method vs setup vs ??other options?? escape me sometimes.

But if I understand correctly, it should be fine to use self.G in a test class that creates self.G in a setup method.

@rossbar here's a chance for you to state the case for fixtures vs classes. :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test class with a setup_method

Nice catch - this is indeed correct; my comment was based on the (incorrect) notion that G was defined as a class attribute. This is the pattern that of course leads to crosstalk between tests as class attributes modified in any test method will remain modified in any subsequent tests. This pattern does appear in the test suite (though pytest-randomly helps weed them out) but @dschult is correct that it is not relevant here.

Sorry for the walls of text @AdamWRichardson - none of these comments are blockers for your change!

Copy link
Copy Markdown
Contributor

@rossbar rossbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fix + tests @AdamWRichardson !

Copy link
Copy Markdown
Member

@dschult dschult left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dschult dschult merged commit 8959637 into networkx:main Nov 15, 2022
@AdamWRichardson AdamWRichardson deleted the bugfix-clear-edges-not-frozen branch November 15, 2022 21:57
@jarrodmillman jarrodmillman added this to the networkx-3.0 milestone Dec 2, 2022
@dschult dschult changed the title Add clear edges method to the list of methods to be frozen by the nx.… Add clear edges method as a method to be frozen by nx.freeze Jan 7, 2023
@dschult dschult mentioned this pull request Jan 7, 2023
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
networkx#6190)

* Add clear edges method to the list of methods to be frozen by the nx.freeze function

* Change tests to create new graph instead of using class attribute
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
networkx#6190)

* Add clear edges method to the list of methods to be frozen by the nx.freeze function

* Change tests to create new graph instead of using class attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants