minimongo
minimongo copied to clipboard
Empty model instances shouldn't evaluate to False
The behavior of empty model instances is unexpected IMO. When you create a new model instance without any properties set, it evaluates to False in boolean expression. For example the following code snippet raises an assertion error either if some_id is None or some_id = 'DOES_NOT_EXIST':
if some_id is None: obj = MyModel() # create a new instance else: obj = MyModel.collection.find_one({'_id': ObjectId(some_id)}) # retrieve from datastore assert obj
I would have expected that only the second path raises an assertion error.
Quick solution would be to use assert obj is not None -- since that's what pymongo returns if document wasn't found.