Azure/azure-cosmos-python

TypeError: 'dict_keys' object does not support indexing

Closed this issue · 9 comments

Hi

I'm trying to do a query on documentdb like this (tested with python 3.4 and 3.6):

query = { 'query': 'SELECT VALUE MAX(c.counter) FROM c' }    
#        
options = {} 
options['enableCrossPartitionQuery'] = True
#options['maxItemCount'] = 2
#
result_iterable = client.QueryDocuments(myCollection, query, options)
  
results = list(result_iterable);
print(results)

And get:

Traceback (most recent call last):
  File "documentdb.py", line 98, in <module>
    for i in result_iterable:
  File "/usr/local/lib/python3.4/dist-packages/pydocumentdb/query_iterable.py", line 107, in __next__
    return next(self._ex_context)
  File "/usr/local/lib/python3.4/dist-packages/pydocumentdb/execution_context/base_execution_context.py", line 103, in __next__
    return self.next()
  File "/usr/local/lib/python3.4/dist-packages/pydocumentdb/execution_context/execution_dispatcher.py", line 70, in next
    return next(self._execution_context)
  File "/usr/local/lib/python3.4/dist-packages/pydocumentdb/execution_context/base_execution_context.py", line 103, in __next__
    return self.next()
  File "/usr/local/lib/python3.4/dist-packages/pydocumentdb/execution_context/execution_dispatcher.py", line 146, in next
    return next(self._endpoint)
  File "/usr/local/lib/python3.4/dist-packages/pydocumentdb/execution_context/endpoint_component.py", line 42, in __next__
    return self.next()
  File "/usr/local/lib/python3.4/dist-packages/pydocumentdb/execution_context/endpoint_component.py", line 98, in next
    operator.aggregate(item[item.keys()[0]])
TypeError: 'dict_keys' object does not support indexing

See this link: https://stackoverflow.com/questions/18552001/accessing-dict-keys-element-by-index-in-python3

Could it be that line 98 in endpoint_component.py should have used next(iter(item)) instead of item.keys()[0] ?

Br. Rune

@khdang Can you look into this please? This seems to be related to aggregate query support.

Minimal test case:

a = dict()
a.keys()[0]

Replace with:

a = dict()
next(iter(a))

Same problem here:

client.QueryDocuments(coll['_self'], {
    'query': 'SELECT VALUE COUNT(c.id) FROM c'
})

results in:

File "/home/lenilson/anaconda3/envs/orcamentos/lib/python3.6/site-packages/pydocumentdb/execution_context/endpoint_component.py", line 98, in next
    operator.aggregate(item[item.keys()[0]])
TypeError: 'dict_keys' object does not support indexing

@runemy thanks for catching this, and the suggestion. I'll fix it shortly. In the meantime, feel free to send a PR and we'll be happy to merge it.

@lnlwd thanks for reporting

Thanks @khdang :) Would be nice if you can fix this and verify that an aggregated query works.

Can you guys review my PR? It's working with @runemy and my queries.

@khdang: Please review and merge.

Any progress on this?