Changeset - 165fc8ed28ab
[Not reviewed]
default
0 2 0
drewp@bigasterisk.com - 20 months ago 2023-05-23 23:26:32
drewp@bigasterisk.com
clearly this approach is wrong
2 files changed with 5 insertions and 0 deletions:
0 comments (0 inline, 0 general)
light9/typedgraph.py
Show inline comments
 
@@ -63,23 +63,25 @@ def typedValue(objType: Type[_ObjType], 
 
        raise TypeError('must allow non-None result type')
 
    obj = graph.value(subj, pred)
 
    if obj is None:
 
        if _typeIncludes(objType, None):
 
            return cast(objType, None)
 
        raise ValueError(f'No obj for {subj=} {pred=}')
 

	
 
    ConvFrom: Type[Node] = type(obj)
 
    ConvTo = objType
 
    try:
 
        if ConvFrom == URIRef and _typeIncludes(ConvTo, URIRef):
 
            conv = obj
 
        elif ConvFrom == URIRef and issubclass(URIRef, ConvTo) and not issubclass(str, ConvTo):  # rewrite please
 
            conv = obj
 
        elif ConvFrom == BNode and issubclass(BNode, ConvTo):
 
            conv = obj
 
        elif ConvFrom == Literal:
 
            conv = _convLiteral(objType, cast(Literal, obj))
 
        else:
 
            raise ConversionError
 
    except ConversionError:
 
        raise ConversionError(f'graph contains {type(obj)}, caller requesting {objType}')
 
    # if objType is float and isinstance(conv, decimal.Decimal):
 
    #     conv = float(conv)
 
    return cast(objType, conv)
 
\ No newline at end of file
light9/typedgraph_test.py
Show inline comments
 
@@ -40,24 +40,27 @@ class TestTypeIncludes:
 
    def test_explicitOptionalWorks(self):
 
        assert _typeIncludes(Optional[int], None)
 

	
 
    def test_3WayUnionWorks(self):
 
        assert _typeIncludes(int | str | float, int)
 

	
 

	
 
class TestTypedValueReturnsBasicTypes:
 

	
 
    def test_getsUri(self):
 
        assert typedValue(URIRef, g, subj, L9['uri']) == L9['c']
 

	
 
    def test_getsAsNode(self):
 
        assert typedValue(Node, 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
 
        assert typedValue(float, g, subj, L9['float2']) == 10
0 comments (0 inline, 0 general)