Iterate over dataset return quads by juanjosegzl · Pull Request #1382 · RDFLib/rdflib (original) (raw)
Note: I'm just some guy commenting and should not be considered to be someone with any authority here.
I'd recommend putting a type hint here so that the codebase gradually shifts towards full type hint / mypy support. I believe that the following would work:
from typing import Optional, Union
# For python < 3.9, else built-in tuple and collections.abc.Generator
from typing import Generator, Tuple
# Type aliases to make unpacking what's going on a little more human friendly
ContextNode = Union[BNode, URIRef]
DatasetQuad = Tuple[Node, URIRef, Node, Optional[ContextNode]]
...
def __iter__(self) -> Generator[DatasetQuad, None, None]:
...
I think that the alias that I suggested above for DatasetQuad
may even be able to be made more specific. The usage of URIRef
reflects the RDF specification that the properties of triples are all URI references. Analogous restrictions exist on the subjects (I believe that literals cannot be subjects, for example). It may be helpful to look into what the context field can be. I believe the context here can only be None
, a BNode
, or a URIRef
, but it's been a little bit since I looked around.