MongoEngine / MongoEngine/mongoengine

Reload doesn't check if attribute is writable

Open
#1,276 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

Hello!

I noticed a weird behavior and managed to isolate the problem.

I think this situation is not as uncommon as it might appear:

from mongoengine import Document, IntField, connect

__author__ = 'gabriele'

connect('test_mongoengine')


class MyModel(Document):
    meta = {
        'strict': False
    }

    int_field = IntField()
    deleted_field = IntField()


new_model = MyModel(int_field=3, deleted_field=4)
new_model.save()


class MyModel(Document):
    int_field = IntField()

    meta = {
        'strict': False
    }

    @property
    def deleted_field(self):
        return 'default'


obj = MyModel.objects(int_field=3).first()

obj.reload()

This will cause mongoengine to raise an AttributeError :

[...]/mongoengine/base/document.py", line 187, in __setattr__
    super(BaseDocument, self).__setattr__(name, value)
AttributeError: can't set attribute

This is an inconsistent behavior because the error is NOT raised when the object is first loaded, but when it is reloaded.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in the issue, then inspect mongoengine/base/document.py around setattr and the reload path. Compare reload behavior with initial loading and determine the intended handling for the read-only property; done means the inconsistent AttributeError behavior is resolved and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.