Skip to content

Commit 89982f8

Browse files
authored
fix: eliminate some mutable default arguments in SPARQL code (#2301)
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`.
1 parent 4fb468d commit 89982f8

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

rdflib/plugins/sparql/evaluate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ def evalDescribeQuery(ctx: QueryContext, query) -> Dict[str, Union[str, Graph]]:
642642
def evalQuery(
643643
graph: Graph,
644644
query: Query,
645-
initBindings: Mapping[str, Identifier],
645+
initBindings: Optional[Mapping[str, Identifier]] = None,
646646
base: Optional[str] = None,
647647
) -> Mapping[Any, Any]:
648648
"""
@@ -661,7 +661,7 @@ def evalQuery(
661661
documentation.
662662
"""
663663

664-
initBindings = dict((Variable(k), v) for k, v in initBindings.items())
664+
initBindings = dict((Variable(k), v) for k, v in (initBindings or {}).items())
665665

666666
ctx = QueryContext(graph, initBindings=initBindings)
667667

rdflib/plugins/sparql/processor.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,30 @@
1919

2020

2121
def prepareQuery(
22-
queryString: str, initNs: Mapping[str, Any] = {}, base: Optional[str] = None
22+
queryString: str,
23+
initNs: Optional[Mapping[str, Any]] = None,
24+
base: Optional[str] = None,
2325
) -> Query:
2426
"""
2527
Parse and translate a SPARQL Query
2628
"""
29+
if initNs is None:
30+
initNs = {}
2731
ret = translateQuery(parseQuery(queryString), base, initNs)
2832
ret._original_args = (queryString, initNs, base)
2933
return ret
3034

3135

3236
def prepareUpdate(
33-
updateString: str, initNs: Mapping[str, Any] = {}, base: Optional[str] = None
37+
updateString: str,
38+
initNs: Optional[Mapping[str, Any]] = None,
39+
base: Optional[str] = None,
3440
) -> Update:
3541
"""
3642
Parse and translate a SPARQL Update
3743
"""
44+
if initNs is None:
45+
initNs = {}
3846
ret = translateUpdate(parseUpdate(updateString), base, initNs)
3947
ret._original_args = (updateString, initNs, base)
4048
return ret
@@ -43,8 +51,8 @@ def prepareUpdate(
4351
def processUpdate(
4452
graph: Graph,
4553
updateString: str,
46-
initBindings: Mapping[str, Identifier] = {},
47-
initNs: Mapping[str, Any] = {},
54+
initBindings: Optional[Mapping[str, Identifier]] = None,
55+
initNs: Optional[Mapping[str, Any]] = None,
4856
base: Optional[str] = None,
4957
) -> None:
5058
"""
@@ -73,8 +81,8 @@ def __init__(self, graph):
7381
def update(
7482
self,
7583
strOrQuery: Union[str, Update],
76-
initBindings: Mapping[str, Identifier] = {},
77-
initNs: Mapping[str, Any] = {},
84+
initBindings: Optional[Mapping[str, Identifier]] = None,
85+
initNs: Optional[Mapping[str, Any]] = None,
7886
) -> None:
7987
"""
8088
.. caution::
@@ -108,8 +116,8 @@ def __init__(self, graph):
108116
def query( # type: ignore[override]
109117
self,
110118
strOrQuery: Union[str, Query],
111-
initBindings: Mapping[str, Identifier] = {},
112-
initNs: Mapping[str, Any] = {},
119+
initBindings: Optional[Mapping[str, Identifier]] = None,
120+
initNs: Optional[Mapping[str, Any]] = None,
113121
base: Optional[str] = None,
114122
DEBUG: bool = False,
115123
) -> Mapping[str, Any]:
@@ -132,9 +140,7 @@ def query( # type: ignore[override]
132140
documentation.
133141
"""
134142

135-
if not isinstance(strOrQuery, Query):
136-
parsetree = parseQuery(strOrQuery)
137-
query = translateQuery(parsetree, base, initNs)
138-
else:
139-
query = strOrQuery
140-
return evalQuery(self.graph, query, initBindings, base)
143+
if isinstance(strOrQuery, str):
144+
strOrQuery = translateQuery(parseQuery(strOrQuery), base, initNs)
145+
146+
return evalQuery(self.graph, strOrQuery, initBindings, base)

rdflib/plugins/sparql/update.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ def evalCopy(ctx: QueryContext, u: CompValue) -> None:
280280

281281

282282
def evalUpdate(
283-
graph: Graph, update: Update, initBindings: Mapping[str, Identifier] = {}
283+
graph: Graph,
284+
update: Update,
285+
initBindings: Optional[Mapping[str, Identifier]] = None,
284286
) -> None:
285287
"""
286288
@@ -315,7 +317,7 @@ def evalUpdate(
315317
"""
316318

317319
for u in update.algebra:
318-
initBindings = dict((Variable(k), v) for k, v in initBindings.items())
320+
initBindings = dict((Variable(k), v) for k, v in (initBindings or {}).items())
319321

320322
ctx = QueryContext(graph, initBindings=initBindings)
321323
ctx.prologue = u.prologue

0 commit comments

Comments
 (0)