ArangoDB-Community / pyArango

Python Driver for ArangoDB with built-in validation
https://pyarango.readthedocs.io/en/latest/
Apache License 2.0
238 stars 90 forks source link

`dict` of a `Document` instance leads to `TypeError` #231

Open larsborn opened 2 years ago

larsborn commented 2 years ago

I expected that the following code will print all key/value pairs from the document with key existing_document_key:

doc = connection['your_database']['your_collection']['existing_document_key']
for key, value in dict(doc).items():
    print(f'{key}: {value}')

But it causes a TypeError: 'NoneType' object is not callable exception in the for key, value in dict(doc).items(): line. After some digging, it looks as if the __dict__ implementation of the pyArango.document.Document class is not correct:

def __dict__(self):
    if not self._store:
        return {}
    return dict(self._store)

But DocumentStore does not implement any sensible __dict__ method. Might it be that the __dict__ method ofDocument` should rather be:

def __dict__(self):
    if not self._store:
        return {}
    return self._store.getStore()

P.S.: After writing up this issue, I was also able to rewrite my initial code to

doc = connection['your_database']['your_collection']['existing_document_key']
for key, value in doc.getStore().items():
    print(f'{key}: {value}')

I would be happy create an MR for the behavior change of __dict__ in Document or, alternatively, to add a paragraph to the README.md or documentation. If there is an even better solution, I'd also be happy to contribute. Just let me know what you prefer!

Environment: