pyld
pyld copied to clipboard
Fix a KeyError in N-Quads normalization
I wanted to try using this package with my new JSON-LD API, with URLs such as http://api.conceptnet.io/c/en/example .
It seems to be able to do simple transformations of the data, but it fails when I ask it to convert to N-Quads format:
>>> from pyld import jsonld
>>> jsonld.to_rdf('http://api.conceptnet.io/c/en/example', options={'format': 'application/nquads'})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/rspeer/.virtualenvs/lum/lib/python3.5/site-packages/pyld/jsonld.py", line 295, in to_rdf
return JsonLdProcessor().to_rdf(input_, options)
File "/home/rspeer/.virtualenvs/lum/lib/python3.5/site-packages/pyld/jsonld.py", line 1147, in to_rdf
return self.to_nquads(dataset)
File "/home/rspeer/.virtualenvs/lum/lib/python3.5/site-packages/pyld/jsonld.py", line 1562, in to_nquads
quads.append(JsonLdProcessor.to_nquad(triple, graph_name))
File "/home/rspeer/.virtualenvs/lum/lib/python3.5/site-packages/pyld/jsonld.py", line 1617, in to_nquad
if o['language']:
KeyError: 'language'
Changing this to o.get('language')
seems to fix the problem, so that's what this patch does.
This patch passes the tests in normalization/tests
. It doesn't pass json-ld.org/test-suite
, but it seems the master branch doesn't either.
There should be a test that runs this code path to avoid regressions. This was probably converted from js and there's never been a test run that didn't have the "language" key set. Do you have minimal JSON-LD and expected output that causes this issue?
Aha! I've noticed that this is a bug in my context file -- I declared a value as a langString
when it's actually just a string. But the library should tell me that there's a problem with my context, instead of just crashing with a KeyError.
Minimal example:
>>> example = {'@context': {'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'text': {'@id': 'http://example.com/text', '@type': 'rdf:langString'}}, 'text': 'foo'}
>>> jsonld.to_rdf(example, options={'format': 'application/nquads'})
Traceback (most recent call last):
File "<ipython-input-23-f64152bf1496>", line 1, in <module>
jsonld.to_rdf(example, options={'format': 'application/nquads'})
File "/home/rspeer/.virtualenvs/lum/lib/python3.5/site-packages/pyld/jsonld.py", line 295, in to_rdf
return JsonLdProcessor().to_rdf(input_, options)
File "/home/rspeer/.virtualenvs/lum/lib/python3.5/site-packages/pyld/jsonld.py", line 1147, in to_rdf
return self.to_nquads(dataset)
File "/home/rspeer/.virtualenvs/lum/lib/python3.5/site-packages/pyld/jsonld.py", line 1562, in to_nquads
quads.append(JsonLdProcessor.to_nquad(triple, graph_name))
File "/home/rspeer/.virtualenvs/lum/lib/python3.5/site-packages/pyld/jsonld.py", line 1617, in to_nquad
if o['language']:
KeyError: 'language'