Skip to content

A URIRef subclass breaks SPARQL querying #1332

@anatoly-scherbakov

Description

@anatoly-scherbakov

Context. I tried to subclass rdflib.URIRef and use it my code because I wanted to add additional functionality to it.

Expectations. I expected a subclass of URIRef to work exactly like URIRef itself works.

Reality. However, it appeared that SPARQL querying will not properly work in that case. Here is a full fledged example.

import rdflib
from rdflib import Graph, FOAF


class IRI(rdflib.URIRef):
    """Internationalized Resource Identifier."""

    def __repr__(self):
        """Format IRI."""
        return f'<{self.toPython()}>'


def test_uriref_subclass():
    graph = Graph()
    graph.add((
        IRI('https://example.com/Alice'),
        IRI(FOAF.knows),
        IRI('https://example.com/Bob'),
    ))

    bindings = graph.query(
        'SELECT * WHERE { ?s <http://xmlns.com/foaf/0.1/knows> ?o . }',
    ).bindings

    # Here we see that bindings are empty: nothing was found.
    assert not bindings

(This code should be runnable as-is.)

Since we added a triple to graph, I expected to find that triple via a SPARQL query but to no avail because FOAF.knows is saved as a IRI, not as a URIRef.

This is a non-standard pattern of usage, but would be great to see if anyone had circumvented such a problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SPARQLbugSomething isn't workingreconfirmThis issue may be solved already and should be reconfirmed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions