Skip to content

Commit

Permalink
fix: eliminate some mutable default arguments in SPARQL code (#2301)
Browse files Browse the repository at this point in the history
This change eliminates some situations where a mutable object (i.e., a dictionary) was used as the default value for functions in the `rdflib.plugins.sparql.processor` module and related code. It replaces these situations with `typing.Optinal` that defaults to None, and is then handled within the function. Luckily, some of the code that the SPARQL Processor relied on already had this style, meaning not a lot of changes had to be made.

This change also makes a small update to the logic in the SPARQL Processor's query function to simplify the if/else statement. This better mirrors the implementation in the `UpdateProcessor`.
cthoyt authored Apr 7, 2023
1 parent 4fb468d commit 89982f8
Showing 3 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions rdflib/plugins/sparql/evaluate.py
Original file line number Diff line number Diff line change
@@ -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)

34 changes: 20 additions & 14 deletions rdflib/plugins/sparql/processor.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 4 additions & 2 deletions rdflib/plugins/sparql/update.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 89982f8

Please sign in to comment.