Change `Dataset.__iter__` to match RDF spec
Also change Graph/Context identifier annotations to match RDF spec.
Graph/Context identifiers should be URIRef or BNode - in RDFLib these
are now the only extant subclasses of IdentifiedNode, so I'm using that.
@@ -43,10 +43,6 @@ assert Namespace # avoid warning
logger = logging.getLogger(__name__)
-# Type aliase to make unpacking what's going on a little more human friendly
-ContextNode = Union[IdentifiedNode, Literal, str, None]
-DatasetQuad = Tuple[Node, URIRef, Node, Optional[ContextNode]]
-
__doc__ = """\
RDFLib defines the following kinds of Graphs:
@@ -329,7 +325,7 @@ class Graph(Node):
def __init__(
self,
store: Union[Store, str] = "default",
- identifier: Optional[Union[Node, str]] = None,
+ identifier: Optional[Union[IdentifiedNode, str]] = None,
namespace_manager: Optional[NamespaceManager] = None,
base: Optional[str] = None,
):
@@ -1558,7 +1554,7 @@ class ConjunctiveGraph(Graph):
def __init__(
self,
store: Union[Store, str] = "default",
- identifier: Optional[Union[Node, str]] = None,
+ identifier: Optional[Union[IdentifiedNode, str]] = None,
default_graph_base: Optional[str] = None,
):
super(ConjunctiveGraph, self).__init__(store, identifier=identifier)
@@ -1803,9 +1799,16 @@ class ConjunctiveGraph(Graph):
format=format,
)
- g_id = publicID and publicID or source.getPublicId()
+ # NOTE on type hint: `xml.sax.xmlreader.InputSource.getPublicId` has no
+ # type annotations but given that systemId should be a string, and
+ # given that there is no specific mention of type for publicId, it
+ # seems reasonable to assume it should also be a string. Furthermore,
+ # create_input_source will ensure that publicId is not None, though it
+ # would be good if this guaruntee was made more explicit i.e. by type
+ # hint on InputSource (TODO/FIXME).
+ g_id: str = publicID and publicID or source.getPublicId()
if not isinstance(g_id, Node):
- g_id = URIRef(g_id)
+ g_id = URIRef(g_id) # type: ignore[arg-type]
context = Graph(store=self.store, identifier=g_id)
context.remove((None, None, None)) # hmm ?
@@ -2034,7 +2037,11 @@ class Dataset(ConjunctiveGraph):
else:
yield s, p, o, c.identifier
- def __iter__(self) -> Generator[DatasetQuad, None, None]:
+ # FIXME: Currently graph identifiers/context can be any Node, even
+ # though this goes outside of the RDF 1.1. specificiation. This should be
+ # fixed, but the fix should not only fix the return value here but also
+ # narrow the type annotations of Graph.__new__.
+ def __iter__(self) -> Generator[Tuple[Node, URIRef, Node, Optional[Node]], None, None]:
"""Iterates over all quads in the store"""
return self.quads((None, None, None, None))
