Update graph operator overloading for subclasses by shreyasnagare · Pull Request #1349 · RDFLib/rdflib (original) (raw)
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 ofGraph()
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.