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

More informative GraphML exceptions #5058

Merged
merged 4 commits into from Sep 15, 2021

Conversation

rossbar
Copy link
Contributor

@rossbar rossbar commented Sep 3, 2021

Closes #5024

Modifies how the dict that defines supported GraphML types is accessed so that an informative exception is raised if a user tries to save an attribute of an unsupported type.

This solution is technically backward incompatible, as it changes the (currently public) xml_types attr of the GraphML class from a dict to a method. The reason for doing it this way is that it provides an easy way to ensure that the exception is raised for all of node, edge, and graph attrs. If this is a problem though, we can always go with the clunkier (but backward compatible) approach which would add try-excepts each time the dict is accessed. LMK what you think!

@rossbar rossbar changed the title More informative gml exceptions More informative GraphML exceptions Sep 3, 2021
@dschult
Copy link
Member

dschult commented Sep 3, 2021

Nice approach! -- I like it.

Would it help backward compatibility if we could tell people (who have adapted the public xml_types for their problem say to a dict new_xml_types) that they can replace the new public xml_types function using new_xml_types.__getitem__? Does that actually make it work like the old code?

I'm not actually sure we want to advertise this way of doing things, but have I understood the backward compatibility issue? Are there other things people may have used the public xml_types for? I guess it is hard to know....

I'm definitely good for leaving it as you have proposed. I'm not sure a deprecation is even needed. Maybe a release note and a discussion of the extent of backward incompatibility here in this PR will be sufficient....??..

@dschult dschult added this to the networkx-2.7 milestone Sep 3, 2021
@rossbar
Copy link
Contributor Author

rossbar commented Sep 3, 2021

Thanks for the feedback @dschult , you're right that there's actually a very straightforward way to do this without breaking compatibility by simply changing the naming scheme around a little bit. The way I've done it in 8a739f9 will break anyone who is modifying/extending/inheriting from GraphML class downstream if they're expecting xml_types to be a dict. I was hung up on the access pattern where there is a private attribute (with an underscore) and a public method/property of the same name (without underscore) that implements access. AFAICT from reading other code this is a common pattern, but it's really just convention.

I've changed the naming around a bit in 42968da to avoid this problem. Adding a method that starts with get_ feels unPythonic but... practicality over purity

Copy link
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.

Clever! :}
I approve this PR.

@dschult dschult merged commit 4e31e9e into networkx:main Sep 15, 2021
@rossbar rossbar deleted the more-informative-gml-exceptions branch December 13, 2021 05:56
dschult pushed a commit to dschult/networkx that referenced this pull request Jan 25, 2022
* Add tests that fail on uninformative exception.

* Raise informative exception when non-supported type used.

* Add importorskip to new tests.

* Change naming scheme for backward compatibility.
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
* Add tests that fail on uninformative exception.

* Raise informative exception when non-supported type used.

* Add importorskip to new tests.

* Change naming scheme for backward compatibility.
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
* Add tests that fail on uninformative exception.

* Raise informative exception when non-supported type used.

* Add importorskip to new tests.

* Change naming scheme for backward compatibility.
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.

Failed to save graph generated using stochastic_block_model
3 participants