3030 build_sparql_query_accept_header ,
3131 build_spo_param ,
3232 rdf_payload_to_stream ,
33+ validate_graph_name ,
34+ validate_no_bnodes ,
3335)
3436from rdflib .graph import DATASET_DEFAULT_GRAPH_ID , Dataset , Graph
3537from rdflib .query import Result
3638from rdflib .term import IdentifiedNode , Literal , URIRef
3739
38- SubjectType = t .Union [IdentifiedNode , None ]
40+ SubjectType = t .Union [URIRef , None ]
3941PredicateType = t .Union [URIRef , None ]
40- ObjectType = t .Union [IdentifiedNode , Literal , None ]
42+ ObjectType = t .Union [URIRef , Literal , None ]
4143
4244
4345@dataclass (frozen = True )
@@ -198,8 +200,14 @@ def identifier(self):
198200 @staticmethod
199201 def _build_graph_name_params (graph_name : URIRef | str ):
200202 params = {}
201- if isinstance (graph_name , URIRef ) and graph_name == DATASET_DEFAULT_GRAPH_ID :
202- # Do nothing; GraphDB does not work with `?default=`, which is the default
203+ if (
204+ isinstance (graph_name , URIRef )
205+ and graph_name == DATASET_DEFAULT_GRAPH_ID
206+ or isinstance (graph_name , str )
207+ and graph_name == str (DATASET_DEFAULT_GRAPH_ID )
208+ ):
209+ # Do nothing; GraphDB does not work with `?default=`
210+ # (note the trailing equal character), which is the default
203211 # behavior of httpx when setting the param value to an empty string.
204212 # httpx completely omits query parameters whose values are `None`, so that's
205213 # not an option either.
@@ -231,6 +239,7 @@ def get(self, graph_name: URIRef | str) -> Graph:
231239 """
232240 if not graph_name :
233241 raise ValueError ("Graph name must be provided." )
242+ validate_graph_name (graph_name )
234243 headers = {
235244 "Accept" : self ._content_type ,
236245 }
@@ -260,6 +269,7 @@ def add(self, graph_name: URIRef | str, data: str | bytes | BinaryIO | Graph):
260269 """
261270 if not graph_name :
262271 raise ValueError ("Graph name must be provided." )
272+ validate_graph_name (graph_name )
263273 stream , should_close = rdf_payload_to_stream (data )
264274 headers = {
265275 "Content-Type" : self ._content_type ,
@@ -290,6 +300,7 @@ def overwrite(self, graph_name: URIRef | str, data: str | bytes | BinaryIO | Gra
290300 """
291301 if not graph_name :
292302 raise ValueError ("Graph name must be provided." )
303+ validate_graph_name (graph_name )
293304 stream , should_close = rdf_payload_to_stream (data )
294305 headers = {
295306 "Content-Type" : self ._content_type ,
@@ -318,6 +329,7 @@ def clear(self, graph_name: URIRef | str):
318329 """
319330 if not graph_name :
320331 raise ValueError ("Graph name must be provided." )
332+ validate_graph_name (graph_name )
321333 params = self ._build_graph_name_params (graph_name ) or None
322334 response = self .http_client .delete (self ._build_url (graph_name ), params = params )
323335 response .raise_for_status ()
@@ -412,9 +424,7 @@ def health(self) -> bool:
412424 f"Repository { self ._identifier } is not healthy. { err .response .status_code } - { err .response .text } "
413425 )
414426
415- def size (
416- self , graph_name : IdentifiedNode | Iterable [IdentifiedNode ] | str | None = None
417- ) -> int :
427+ def size (self , graph_name : URIRef | Iterable [URIRef ] | str | None = None ) -> int :
418428 """The number of statements in the repository or in the specified graph name.
419429
420430 Parameters:
@@ -431,6 +441,7 @@ def size(
431441 Raises:
432442 RepositoryFormatError: Fails to parse the repository size.
433443 """
444+ validate_graph_name (graph_name )
434445 params : dict [str , str ] = {}
435446 build_context_param (params , graph_name )
436447 response = self .http_client .get (
@@ -541,12 +552,16 @@ def get(
541552 subj : SubjectType = None ,
542553 pred : PredicateType = None ,
543554 obj : ObjectType = None ,
544- graph_name : IdentifiedNode | Iterable [IdentifiedNode ] | str | None = None ,
555+ graph_name : URIRef | Iterable [URIRef ] | str | None = None ,
545556 infer : bool = True ,
546557 content_type : str | None = None ,
547558 ) -> Graph | Dataset :
548559 """Get RDF statements from the repository matching the filtering parameters.
549560
561+ !!! Note
562+ The terms for `subj`, `pred`, `obj` or `graph_name` cannot be
563+ [`BNodes`][rdflib.term.BNode].
564+
550565 Parameters:
551566 subj: Subject of the statement to filter by, or `None` to match all.
552567 pred: Predicate of the statement to filter by, or `None` to match all.
@@ -568,6 +583,7 @@ def get(
568583 A [`Graph`][rdflib.graph.Graph] or [`Dataset`][rdflib.graph.Dataset] object
569584 with the repository namespace prefixes bound to it.
570585 """
586+ validate_no_bnodes (subj , pred , obj , graph_name )
571587 if content_type is None :
572588 content_type = "application/n-quads"
573589 headers = {"Accept" : content_type }
@@ -632,7 +648,7 @@ def upload(
632648 def overwrite (
633649 self ,
634650 data : str | bytes | BinaryIO | Graph | Dataset ,
635- graph_name : IdentifiedNode | Iterable [IdentifiedNode ] | str | None = None ,
651+ graph_name : URIRef | Iterable [URIRef ] | str | None = None ,
636652 base_uri : str | None = None ,
637653 content_type : str | None = None ,
638654 ):
@@ -652,7 +668,7 @@ def overwrite(
652668 `application/n-quads` when the value is `None`.
653669 """
654670 stream , should_close = rdf_payload_to_stream (data )
655-
671+ validate_graph_name ( graph_name )
656672 try :
657673 headers = {"Content-Type" : content_type or "application/n-quads" }
658674 params : dict [str , str ] = {}
@@ -675,10 +691,14 @@ def delete(
675691 subj : SubjectType = None ,
676692 pred : PredicateType = None ,
677693 obj : ObjectType = None ,
678- graph_name : IdentifiedNode | Iterable [IdentifiedNode ] | str | None = None ,
694+ graph_name : URIRef | Iterable [URIRef ] | str | None = None ,
679695 ) -> None :
680696 """Deletes statements from the repository matching the filtering parameters.
681697
698+ !!! Note
699+ The terms for `subj`, `pred`, `obj` or `graph_name` cannot be
700+ [`BNodes`][rdflib.term.BNode].
701+
682702 Parameters:
683703 subj: Subject of the statement to filter by, or `None` to match all.
684704 pred: Predicate of the statement to filter by, or `None` to match all.
@@ -690,6 +710,7 @@ def delete(
690710 To query just the default graph, use
691711 [`DATASET_DEFAULT_GRAPH_ID`][rdflib.graph.DATASET_DEFAULT_GRAPH_ID].
692712 """
713+ validate_no_bnodes (subj , pred , obj , graph_name )
693714 params : dict [str , str ] = {}
694715 build_context_param (params , graph_name )
695716 build_spo_param (params , subj , pred , obj )
@@ -808,9 +829,7 @@ def ping(self):
808829 f"Transaction ping failed: { response .status_code } - { response .text } "
809830 )
810831
811- def size (
812- self , graph_name : IdentifiedNode | Iterable [IdentifiedNode ] | str | None = None
813- ):
832+ def size (self , graph_name : URIRef | Iterable [URIRef ] | str | None = None ):
814833 """The number of statements in the repository or in the specified graph name.
815834
816835 Parameters:
@@ -828,6 +847,7 @@ def size(
828847 RepositoryFormatError: Fails to parse the repository size.
829848 """
830849 self ._raise_for_closed ()
850+ validate_graph_name (graph_name )
831851 params = {"action" : "SIZE" }
832852 build_context_param (params , graph_name )
833853 response = self .repo .http_client .put (self .url , params = params )
@@ -913,12 +933,16 @@ def get(
913933 subj : SubjectType = None ,
914934 pred : PredicateType = None ,
915935 obj : ObjectType = None ,
916- graph_name : IdentifiedNode | Iterable [IdentifiedNode ] | str | None = None ,
936+ graph_name : URIRef | Iterable [URIRef ] | str | None = None ,
917937 infer : bool = True ,
918938 content_type : str | None = None ,
919939 ) -> Graph | Dataset :
920940 """Get RDF statements from the repository matching the filtering parameters.
921941
942+ !!! Note
943+ The terms for `subj`, `pred`, `obj` or `graph_name` cannot be
944+ [`BNodes`][rdflib.term.BNode].
945+
922946 Parameters:
923947 subj: Subject of the statement to filter by, or `None` to match all.
924948 pred: Predicate of the statement to filter by, or `None` to match all.
@@ -940,6 +964,7 @@ def get(
940964 A [`Graph`][rdflib.graph.Graph] or [`Dataset`][rdflib.graph.Dataset] object
941965 with the repository namespace prefixes bound to it.
942966 """
967+ validate_no_bnodes (subj , pred , obj , graph_name )
943968 if content_type is None :
944969 content_type = "application/n-quads"
945970 headers = {"Accept" : content_type }
0 commit comments