diff --git a/rdflib/plugins/sparql/evaluate.py b/rdflib/plugins/sparql/evaluate.py index 4f8d687b4..764250c8c 100644 --- a/rdflib/plugins/sparql/evaluate.py +++ b/rdflib/plugins/sparql/evaluate.py @@ -642,7 +642,7 @@ def evalDescribeQuery(ctx: QueryContext, query) -> Dict[str, Union[str, Graph]]: def evalQuery( graph: Graph, query: Query, - initBindings: Mapping[str, Identifier], + initBindings: Optional[Mapping[str, Identifier]] = None, base: Optional[str] = None, ) -> Mapping[Any, Any]: """ @@ -661,7 +661,7 @@ def evalQuery( documentation. """ - initBindings = dict((Variable(k), v) for k, v in initBindings.items()) + initBindings = dict((Variable(k), v) for k, v in (initBindings or {}).items()) ctx = QueryContext(graph, initBindings=initBindings) diff --git a/rdflib/plugins/sparql/processor.py b/rdflib/plugins/sparql/processor.py index c2fb7e54b..f10f372bc 100644 --- a/rdflib/plugins/sparql/processor.py +++ b/rdflib/plugins/sparql/processor.py @@ -19,22 +19,30 @@ def prepareQuery( - queryString: str, initNs: Mapping[str, Any] = {}, base: Optional[str] = None + queryString: str, + initNs: Optional[Mapping[str, Any]] = None, + base: Optional[str] = None, ) -> Query: """ Parse and translate a SPARQL Query """ + if initNs is None: + initNs = {} ret = translateQuery(parseQuery(queryString), base, initNs) ret._original_args = (queryString, initNs, base) return ret def prepareUpdate( - updateString: str, initNs: Mapping[str, Any] = {}, base: Optional[str] = None + updateString: str, + initNs: Optional[Mapping[str, Any]] = None, + base: Optional[str] = None, ) -> Update: """ Parse and translate a SPARQL Update """ + if initNs is None: + initNs = {} ret = translateUpdate(parseUpdate(updateString), base, initNs) ret._original_args = (updateString, initNs, base) return ret @@ -43,8 +51,8 @@ def prepareUpdate( def processUpdate( graph: Graph, updateString: str, - initBindings: Mapping[str, Identifier] = {}, - initNs: Mapping[str, Any] = {}, + initBindings: Optional[Mapping[str, Identifier]] = None, + initNs: Optional[Mapping[str, Any]] = None, base: Optional[str] = None, ) -> None: """ @@ -73,8 +81,8 @@ def __init__(self, graph): def update( self, strOrQuery: Union[str, Update], - initBindings: Mapping[str, Identifier] = {}, - initNs: Mapping[str, Any] = {}, + initBindings: Optional[Mapping[str, Identifier]] = None, + initNs: Optional[Mapping[str, Any]] = None, ) -> None: """ .. caution:: @@ -108,8 +116,8 @@ def __init__(self, graph): def query( # type: ignore[override] self, strOrQuery: Union[str, Query], - initBindings: Mapping[str, Identifier] = {}, - initNs: Mapping[str, Any] = {}, + initBindings: Optional[Mapping[str, Identifier]] = None, + initNs: Optional[Mapping[str, Any]] = None, base: Optional[str] = None, DEBUG: bool = False, ) -> Mapping[str, Any]: @@ -132,9 +140,7 @@ def query( # type: ignore[override] documentation. """ - if not isinstance(strOrQuery, Query): - parsetree = parseQuery(strOrQuery) - query = translateQuery(parsetree, base, initNs) - else: - query = strOrQuery - return evalQuery(self.graph, query, initBindings, base) + if isinstance(strOrQuery, str): + strOrQuery = translateQuery(parseQuery(strOrQuery), base, initNs) + + return evalQuery(self.graph, strOrQuery, initBindings, base) diff --git a/rdflib/plugins/sparql/update.py b/rdflib/plugins/sparql/update.py index f27ee9b36..5ce86f393 100644 --- a/rdflib/plugins/sparql/update.py +++ b/rdflib/plugins/sparql/update.py @@ -280,7 +280,9 @@ def evalCopy(ctx: QueryContext, u: CompValue) -> None: def evalUpdate( - graph: Graph, update: Update, initBindings: Mapping[str, Identifier] = {} + graph: Graph, + update: Update, + initBindings: Optional[Mapping[str, Identifier]] = None, ) -> None: """ @@ -315,7 +317,7 @@ def evalUpdate( """ for u in update.algebra: - initBindings = dict((Variable(k), v) for k, v in initBindings.items()) + initBindings = dict((Variable(k), v) for k, v in (initBindings or {}).items()) ctx = QueryContext(graph, initBindings=initBindings) ctx.prologue = u.prologue