Skip to content

Commit 832e693

Browse files
authored
fix: JSON-LD context construction from a dict (#2306)
A variable was only being initialized for string-valued inputs, but if a `dict` input was passed the variable would still be accessed, resulting in a `UnboundLocalError`. This change initializes the variable always, instead of only when string-valued input is used to construct a JSON-LD context. - Closes <#2303>.
1 parent 1ab4fc0 commit 832e693

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

rdflib/plugins/shared/jsonld/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,13 @@ def _prep_sources(
421421
):
422422
for source in inputs:
423423
source_url = in_source_url
424+
new_base = base
424425
if isinstance(source, str):
425426
source_url = source
426427
source_doc_base = base or self.doc_base
427428
new_ctx = self._fetch_context(
428429
source, source_doc_base, referenced_contexts
429430
)
430-
new_base = base
431431
if new_ctx is None:
432432
continue
433433
else:

test/jsonld/test_context.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from functools import wraps
6+
from pathlib import Path
67
from typing import Any, Dict
78

89
from rdflib.plugins.shared.jsonld import context, errors
@@ -213,3 +214,23 @@ def test_invalid_remote_context():
213214
ctx_url = "http://example.org/recursive.jsonld"
214215
SOURCES[ctx_url] = {"key": "value"}
215216
ctx = Context(ctx_url)
217+
218+
219+
def test_file_source(tmp_path: Path) -> None:
220+
"""
221+
A file URI source to `Context` gets processed correctly.
222+
"""
223+
file = tmp_path / "context.jsonld"
224+
file.write_text(r"""{ "@context": { "ex": "http://example.com/" } }""")
225+
ctx = Context(source=file.as_uri())
226+
assert "http://example.com/" == ctx.terms["ex"].id
227+
228+
229+
def test_dict_source(tmp_path: Path) -> None:
230+
"""
231+
A dictionary source to `Context` gets processed correctly.
232+
"""
233+
file = tmp_path / "context.jsonld"
234+
file.write_text(r"""{ "@context": { "ex": "http://example.com/" } }""")
235+
ctx = Context(source=[{"@context": file.as_uri()}])
236+
assert "http://example.com/" == ctx.terms["ex"].id

0 commit comments

Comments
 (0)