diff --git a/rdflib/graph.py b/rdflib/graph.py index f90b3acbb..b01d33f82 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -558,7 +558,10 @@ def __isub__(self, other): def __add__(self, other): """Set-theoretic union BNode IDs are not changed.""" - retval = Graph() + try: + retval = type(self)() + except TypeError: + retval = Graph() for (prefix, uri) in set(list(self.namespaces()) + list(other.namespaces())): retval.bind(prefix, uri) for x in self: @@ -570,7 +573,10 @@ def __add__(self, other): def __mul__(self, other): """Set-theoretic intersection. BNode IDs are not changed.""" - retval = Graph() + try: + retval = type(self)() + except TypeError: + retval = Graph() for x in other: if x in self: retval.add(x) @@ -579,7 +585,10 @@ def __mul__(self, other): def __sub__(self, other): """Set-theoretic difference. BNode IDs are not changed.""" - retval = Graph() + try: + retval = type(self)() + except TypeError: + retval = Graph() for x in self: if x not in other: retval.add(x) diff --git a/test/test_graph_operator.py b/test/test_graph_operator.py new file mode 100644 index 000000000..87502c54b --- /dev/null +++ b/test/test_graph_operator.py @@ -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)