Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a fix for parsing rdflib literals. #229

Merged
merged 7 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,45 @@ def test_parse_literal() -> None:
assert literal.lang is None
assert literal.datatype == RDF.HTML

literal = parse_literal(f'"""["a", 1, 2]"""^^<{RDF.JSON}>')
assert literal.value == '["a", 1, 2]'
assert literal.lang is None
assert literal.datatype == RDF.JSON

with pytest.warns(UserWarning, match="unknown datatype"):
literal = parse_literal('"value"^^http://example.com/vocab#mytype')
assert literal.value == "value"
assert literal.lang is None
assert literal.datatype == "http://example.com/vocab#mytype"


def test_rdflib_literal():
"""Test parsing rdflib literals."""
import pytest

rdflib = pytest.importorskip("rdflib")
from tripper import RDF, XSD
from tripper.utils import parse_literal

rdflib_literal = rdflib.Literal(1, datatype=rdflib.XSD.integer)
literal = parse_literal(rdflib_literal)
assert literal.value == 1
assert literal.lang is None
assert literal.datatype == XSD.integer

rdflib_literal = rdflib.Literal("abc", datatype=rdflib.XSD.string)
literal = parse_literal(rdflib_literal)
assert literal.value == "abc"
assert literal.lang is None
assert literal.datatype == XSD.string

rdflib_literal = rdflib.Literal('["a", 1, 2]', datatype=rdflib.RDF.JSON)
literal = parse_literal(rdflib_literal)
assert literal.value == '["a", 1, 2]'
assert literal.lang is None
assert literal.datatype == RDF.JSON


def test_equality() -> None:
"""Test equality."""
from tripper import RDF, XSD, Literal
Expand Down
12 changes: 9 additions & 3 deletions tripper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,16 @@ def parse_literal(literal: "Any") -> "Any":
):
datatype = str(literal.datatype)

# This will handle rdflib literals correctly and probably most other
# literal representations as well.
# This should handle rdflib literals correctly (and probably most other
# literal representations as well)
if hasattr(literal, "value"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But can a literal not have the attribute? Isn't it always there, but sometimes with value None? (I thought the reason for this fix is exactly that)

Copy link
Contributor Author

@jesper-friis jesper-friis Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, the argument name literal is not very well chosen. It may be any Python object that can be converted to a literal (most of these do not have a value attribute). It was changed by Casper from obj to literal in one of his early reviews. Should we revert?

return Literal(literal.value, lang=lang, datatype=datatype)
# Note that in rdflib 6.3, the `value` attribute may be None for some
# datatypes (like rdf:JSON) even though a non-empty value exists.
# As a workaround, we use the string representation if the value
# attribute is None.
if literal.value is not None:
return Literal(literal.value, lang=lang, datatype=datatype)
return Literal(str(literal), lang=lang, datatype=datatype)

if not isinstance(literal, str):
if isinstance(literal, tuple(Literal.datatypes)):
Expand Down