minimongo icon indicating copy to clipboard operation
minimongo copied to clipboard

Empty model instances shouldn't evaluate to False

Open andialbrecht opened this issue 14 years ago • 1 comments

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.

andialbrecht avatar Jul 01 '11 04:07 andialbrecht

Quick solution would be to use assert obj is not None -- since that's what pymongo returns if document wasn't found.

superbobry avatar Jul 01 '11 05:07 superbobry