jenkins-bot has submitted this change and it was merged.
Change subject: T150521: Don't crash on queries with optional values ......................................................................
T150521: Don't crash on queries with optional values
The current implementation assumes variables are always set. When using sparql OPTIONAL sometimes values are not set. In that case, set it to None
Change-Id: I4af22d4faf42ca1e20581c9dd094237cf3467f71 --- M pywikibot/data/sparql.py 1 file changed, 10 insertions(+), 6 deletions(-)
Approvals: Merlijn van Deen: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/sparql.py b/pywikibot/data/sparql.py index 70c6563..e95c916 100644 --- a/pywikibot/data/sparql.py +++ b/pywikibot/data/sparql.py @@ -65,13 +65,17 @@ for row in data['results']['bindings']: values = {} for var in qvars: - if full_data: - if row[var]['type'] not in VALUE_TYPES: - raise ValueError('Unknown type: %s' % row[var]['type']) - valtype = VALUE_TYPES[row[var]['type']] - values[var] = valtype(row[var], entity_url=self.entity_url) + if var in row: + if full_data: + if row[var]['type'] not in VALUE_TYPES: + raise ValueError('Unknown type: %s' % row[var]['type']) + valtype = VALUE_TYPES[row[var]['type']] + values[var] = valtype(row[var], entity_url=self.entity_url) + else: + values[var] = row[var]['value'] else: - values[var] = row[var]['value'] + # var is not available (OPTIONAL is probably used) + values[var] = None result.append(values) return result else:
pywikibot-commits@lists.wikimedia.org