I already solved it and the result is a bugreport. :-) https://bugzilla.wikimedia.org/show_bug.cgi?id=34029
Not a big problem, but it would be nicer to solve this within query.py, because the same piece of code will be multipleied this way each time somebody uses it.