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

Update graph operator overloading for subclasses #1349

Merged

Conversation

shreyasnagare
Copy link
Contributor

@shreyasnagare shreyasnagare commented Jun 30, 2021

This PR makes the set operations for rdflib.Graph subclasses (like g1 = g1 + g2) more intuitive and similar to the results of in-place set operations (like g1 += g2).

Example:

If you were to do a union of a ConjunctiveGraph (a subclass of Graph) instance with itself or any other ConjunctiveGraph instance using something like g = g + g, the instance would lose all the methods specific to the ConjunctiveGraph.

Steps to reproduce the issue:

from rdflib import Graph

class MyGraph(Graph):
    def perform_reasoning(self):
        pass

g = MyGraph()
"perform_reasoning" in dir(g)
# True

g += g
"perform_reasoning" in dir(g)
# True

g = g + g
"perform_reasoning" in dir(g)
# False

Proposed Changes

  • Use type(self)() instead of Graph() as the initial return value for set operations.
  • This would allow Graph to pass down similar behavior to its derived classes (unless overridden) without breaking any existing functionality.

Copy link
Member

@nicholascar nicholascar left a comment

Choose a reason for hiding this comment

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

Looks sensible to me.

The only test failure is an address assignment, so not a real one.

@nicholascar
Copy link
Member

@shreyasnagare thinks for this PR - looks good to me. Can you please add in your "steps to reproduce" as an actual test though? So test_graph_operator.py or similar? Thanks.

@nicholascar
Copy link
Member

@shreyasnagare thanks for that rapid turn-around! Just waiting for another review from one of my co-maintainers and then we'll likely merge.

Please do feel free to make more good updates like this!

rdflib/graph.py Outdated Show resolved Hide resolved
@nicholascar nicholascar merged commit 3783df5 into RDFLib:master Jul 2, 2021
@shreyasnagare shreyasnagare deleted the fix-graph-operator-overloading branch July 2, 2021 04:46
@gtfierro
Copy link
Contributor

Great to see this change, and thanks @shreyasnagare for finding and addressing it. I'd like to incorporate these changes in the brickschema package --- is there a timeline for the next release of RDFlib?

@nicholascar
Copy link
Member

@gtfierro 6.0.0 is only a couple of days away! We merged in the JSON-LD plug-in, sorted out namespace issues and merged in heaps of smaller PRs like this. Even updated docco. Just waiting for some final CI/CD steps and we will have the package into PyPI. Again, only a few days!

@gtfierro
Copy link
Contributor

Great news! Congrats on the release

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

Successfully merging this pull request may close these issues.

4 participants