From d6aa01db18739aea43177cfcec41f8667aebdb3d Mon Sep 17 00:00:00 2001 From: shreyasnagare Date: Wed, 30 Jun 2021 03:27:26 -0400 Subject: [PATCH 1/4] Update graph operator overloading for subclasses --- rdflib/graph.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rdflib/graph.py b/rdflib/graph.py index f90b3acbb..c354a7705 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -558,7 +558,7 @@ def __isub__(self, other): def __add__(self, other): """Set-theoretic union BNode IDs are not changed.""" - retval = Graph() + retval = self.__class__() for (prefix, uri) in set(list(self.namespaces()) + list(other.namespaces())): retval.bind(prefix, uri) for x in self: @@ -570,7 +570,7 @@ def __add__(self, other): def __mul__(self, other): """Set-theoretic intersection. BNode IDs are not changed.""" - retval = Graph() + retval = self.__class__() for x in other: if x in self: retval.add(x) @@ -579,7 +579,7 @@ def __mul__(self, other): def __sub__(self, other): """Set-theoretic difference. BNode IDs are not changed.""" - retval = Graph() + retval = self.__class__() for x in self: if x not in other: retval.add(x) From 529caa24feca237ae31b84adabb7c90c2805ba83 Mon Sep 17 00:00:00 2001 From: shreyasnagare Date: Wed, 30 Jun 2021 19:33:17 -0400 Subject: [PATCH 2/4] Add tests for operators --- test/test_graph_operator.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/test_graph_operator.py 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) From db531bf65a7780204d670387affdb5d14fcd4d91 Mon Sep 17 00:00:00 2001 From: shreyasnagare Date: Wed, 30 Jun 2021 19:35:08 -0400 Subject: [PATCH 3/4] Add fallback for subclasses with required init args --- rdflib/graph.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/rdflib/graph.py b/rdflib/graph.py index c354a7705..8cbc066d2 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 = self.__class__() + try: + retval = self.__class__() + 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 = self.__class__() + try: + retval = self.__class__() + 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 = self.__class__() + try: + retval = self.__class__() + except TypeError: + retval = Graph() for x in self: if x not in other: retval.add(x) From d605a7086d80686e07252eae333d36e85723b7a3 Mon Sep 17 00:00:00 2001 From: shreyasnagare Date: Thu, 1 Jul 2021 17:18:30 -0400 Subject: [PATCH 4/4] Use type(self)() instead of self.__class__(). --- rdflib/graph.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rdflib/graph.py b/rdflib/graph.py index 8cbc066d2..b01d33f82 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -559,7 +559,7 @@ def __add__(self, other): """Set-theoretic union BNode IDs are not changed.""" try: - retval = self.__class__() + retval = type(self)() except TypeError: retval = Graph() for (prefix, uri) in set(list(self.namespaces()) + list(other.namespaces())): @@ -574,7 +574,7 @@ def __mul__(self, other): """Set-theoretic intersection. BNode IDs are not changed.""" try: - retval = self.__class__() + retval = type(self)() except TypeError: retval = Graph() for x in other: @@ -586,7 +586,7 @@ def __sub__(self, other): """Set-theoretic difference. BNode IDs are not changed.""" try: - retval = self.__class__() + retval = type(self)() except TypeError: retval = Graph() for x in self: