Skip to content

Allow custom store implementations to not inherit from rdflib.store.Store #1435

@ashleysommer

Description

@ashleysommer

Opening this as a discussion topic.

Currently, when instantiating a Graph instance with a pass-in store instance, the store parameter must be either:

  1. An instance of something that is a subclass of rdflib.store.Store, or
  2. A string, to dereference a store implementation from a registered plugin

See:

rdflib/rdflib/graph.py

Lines 325 to 329 in ab31c5e

if not isinstance(store, Store):
# TODO: error handling
self.__store = store = plugin.get(store, Store)()
else:
self.__store = store

However, I've twice encountered the situation where I want to pass in a store that looks like and acts like a rdflib.store.Store (via duck-typing) but does not inherit from rdflib.store.Store.

Eg, this doesn't work:

from nonrdflibmodule.store import MySpecialStore
from rdflib import Graph
mystore = MySpecialStore()
g = Graph(store=mystore)  #<- fails

This situation, the Graph constructor sees that MySpecialStore does not inherit from rdflib.store.Store so it uses that as a lookup key to find a plugin, and fails because it is not a valid lookup key.

There is a workaround I've found, by temporarily registering the store:

from rdflib.plugin import register
register("tempstore", Store, "nonrdflibmodule.store", "MySpecialStore")
g = Graph(store="tempstore")
mystore = g.store

I know this is a niche situation, but I wonder if we could use some kind of duck-typing mechanism to check if a store implements a basic set of features, and allow it, even if it not a subclass of rdflib.store.Store.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions