-
Notifications
You must be signed in to change notification settings - Fork 577
Description
I just hit an unpleasant surprise on running unit tests on a project that worked yesterday, but failed today. The issue I hit was in using mypy --strict on a SPARQL CONSTRUCT query. A similar issue exists for SELECT queries, but I've resolved that one.
With RDFLib 6.2.0, this worked:
for result in in_graph.query(construct_query):
assert isinstance(result[0], URIRef)
assert isinstance(result[1], URIRef)Those asserts are a pattern I've had to implement to convince mypy --strict about types when I have later code assuming e.g. result[0] (and [1]) is a URIRef.
With 6.3.0 (released today), I get a complaint that I haven't guaranteed result is indexable. This is true for SELECT and CONSTRUCT queries.
error: Value of type "Union[Tuple[Node, Node, Node], bool, ResultRow]" is not indexable [index]
I can get by with the following extra bits for SELECT queries:
#!/usr/bin/env/python
# ...
from rdflib.query import ResultRow
# ...
for result in graph.query(select_query):
assert isinstance(result, ResultRow)That pretty much followed from the mypy complaint I excerpted above, and checking for where ResultRow was defined. But, for CONSTRUCT queries, the Tuple syntax doesn't quite work the same.
#!/usr/bin/env/python
# ...
for result in graph.query(construct_query):
assert isinstance(result, Tuple[URIRef, URIRef, URIRef])mypy --strict throws a different error on that:
error: Argument 2 to "isinstance" has incompatible type "object"; expected "_ClassInfo" [arg-type]
I don't see a unit test demonstrating how to get this to work. I also am not understanding how the return type of rdflib.graph.Graph.query() -> rdflib.query.Result eventually ties to that Union that covers SELECT, CONSTRUCT and ASK return types.
How should the results of CONSTRUCT queries interact with mypy --strict? Is there a type alias I should use instead of that Tuple? I thought there was an alias somewhere in term.py to cover a general triple, but I'm having trouble finding the symbol now.