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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,10 @@ def __isub__(self, other):
def __add__(self, other):
"""Set-theoretic union
BNode IDs are not changed."""
retval = Graph()
try:
retval = self.__class__()
nicholascar marked this conversation as resolved.
Show resolved Hide resolved
except TypeError:
retval = Graph()
for (prefix, uri) in set(list(self.namespaces()) + list(other.namespaces())):
retval.bind(prefix, uri)
for x in self:
Expand All @@ -570,7 +573,10 @@ def __add__(self, other):
def __mul__(self, other):
"""Set-theoretic intersection.
BNode IDs are not changed."""
retval = Graph()
try:
retval = self.__class__()
except TypeError:
retval = Graph()
for x in other:
if x in self:
retval.add(x)
Expand All @@ -579,7 +585,10 @@ def __mul__(self, other):
def __sub__(self, other):
"""Set-theoretic difference.
BNode IDs are not changed."""
retval = Graph()
try:
retval = self.__class__()
except TypeError:
retval = Graph()
for x in self:
if x not in other:
retval.add(x)
Expand Down
27 changes: 27 additions & 0 deletions test/test_graph_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from rdflib import Graph


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


def test_subclass_add_operator():
g = MyGraph()

g = g + g
assert "my_method" in dir(g)


def test_subclass_sub_operator():
g = MyGraph()

g = g - g
assert "my_method" in dir(g)


def test_subclass_mul_operator():
g = MyGraph()

g = g * g
assert "my_method" in dir(g)