changeset 2211:5edb163780e2

caller can ask for Node and BNode is allowed
author drewp@bigasterisk.com
date Tue, 23 May 2023 11:44:54 -0700
parents ddff5ce676eb
children b6f8f1b08959
files light9/typedgraph.py light9/typedgraph_test.py
diffstat 2 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/light9/typedgraph.py	Mon May 22 13:55:04 2023 -0700
+++ b/light9/typedgraph.py	Tue May 23 11:44:54 2023 -0700
@@ -1,7 +1,7 @@
 from typing import List, Type, TypeVar, cast, get_args
 
 from rdfdb.syncedgraph.syncedgraph import SyncedGraph
-from rdflib import XSD, Graph, Literal, URIRef
+from rdflib import XSD, BNode, Graph, Literal, URIRef
 from rdflib.term import Node
 
 # todo: this ought to just require a suitable graph.value method
@@ -75,10 +75,11 @@
     try:
         if ConvFrom == URIRef and _typeIncludes(ConvTo, URIRef):
             conv = obj
+        elif ConvFrom == BNode and issubclass(BNode, ConvTo):
+            conv = obj
         elif ConvFrom == Literal:
             conv = _convLiteral(objType, cast(Literal, obj))
         else:
-            # e.g. BNode is not handled yet
             raise ConversionError
     except ConversionError:
         raise ConversionError(f'graph contains {type(obj)}, caller requesting {objType}')
--- a/light9/typedgraph_test.py	Mon May 22 13:55:04 2023 -0700
+++ b/light9/typedgraph_test.py	Tue May 23 11:44:54 2023 -0700
@@ -1,8 +1,8 @@
 from typing import NewType, Optional, cast
 
 import pytest
-from rdflib import Graph, Literal, URIRef
-
+from rdflib import BNode, Graph, Literal, URIRef
+from rdflib.term import Node
 from light9.mock_syncedgraph import MockSyncedGraph
 from light9.namespaces import L9, XSD
 from light9.typedgraph import ConversionError, _typeIncludes, typedValue
@@ -13,6 +13,7 @@
     @prefix : <http://light9.bigasterisk.com/> .
     :subj
         :uri :c;
+        :bnode [];
         # see https://w3c.github.io/N3/spec/#literals for syntaxes.
         :int 0;
         :float1 0.0;
@@ -48,6 +49,14 @@
     def test_getsUri(self):
         assert typedValue(URIRef, g, subj, L9['uri']) == L9['c']
 
+    def test_getsBNode(self):
+        # this is unusual usage since users ought to always be able to replace BNode with URIRef
+        assert typedValue(BNode, g, subj, L9['bnode']) == g.value(subj,  L9['bnode'])
+
+    def test_getsBNodeAsNode(self):
+        assert typedValue(Node, g, subj, L9['bnode']) == g.value(subj,  L9['bnode'])
+
+
     def test_getsNumerics(self):
         assert typedValue(float, g, subj, L9['int']) == 0
         assert typedValue(float, g, subj, L9['float1']) == 0