Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Frozen fields not supported (Pydantic 2) - exception thrown on .save() #863

Open
TheBloke opened this issue Feb 17, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@TheBloke
Copy link

TheBloke commented Feb 17, 2024

Describe the bug
Beanie does not appear to support frozen fields.

Testing with Beanie 1.25.0 on Pydantic 2.6.1.

Trying to .save() a model with a frozen field throws a validation error. Using insert() does work, but then it will fail if you ever call .save() on it.

Exception thrown:

pydantic_core._pydantic_core.ValidationError: 1 validation error for ConfigModel
creation_datetime
  Field is frozen [type=frozen_field, input_value=datetime.datetime(2024, 2, 17, 11, 16, 52, 604000), input_type=datetime]
    For further information visit https://errors.pydantic.dev/2.6/v/frozen_field

Exception thrown from beanie/odm/utiils/parsing.py:

def merge_models(left: BaseModel, right: BaseModel) -> None:
    """
    Merge two models
    :param left: left model
    :param right: right model
    :return: None
    """
    from beanie.odm.fields import Link

    for k, right_value in right.__iter__():
        left_value = getattr(left, k)
        if isinstance(right_value, BaseModel) and isinstance(
            left_value, BaseModel
        ):
            if get_config_value(left_value, "frozen"):   <------- checks if the model is frozen, but not individual fields?
                left.__setattr__(k, right_value)
            else:
                merge_models(left_value, right_value)
            continue
        if isinstance(right_value, list):
            links_found = False
            for i in right_value:
                if isinstance(i, Link):
                    links_found = True
                    break
            if links_found:
                continue
            left.__setattr__(k, right_value)
        elif not isinstance(right_value, Link):
            left.__setattr__(k, right_value)   <-------------- exception thrown here on frozen=True field

To Reproduce

from pydantic import Field
from motor.motor_asyncio import AsyncIOMotorClient
from beanie import Document, init_beanie

class TestFrozen(Document):
    some_field: str = Field(frozen=True)

    class Settings:
        name = "test_frozen"

async def test_frozen():
    await init_beanie(database=client[DB_NAME], document_models=[TestFrozen])

    test_frozen = TestFrozen(some_field="test")

    # will throw pydantic.validation_error "Field is frozen [type=frozen_field, input_value='test', input_type=str]"
    await test_frozen.save() 
    # test_frozen.insert() does NOT throw, but will if you later .find_one()  and then .save() the row

await test_frozen()

Expected behavior

Frozen fields are handled, and do not break saving. I guess it will be necessary to modify merge_models() to check on a per field basis if a field is frozen, and handle it differently.

Thanks in advance.

@TheBloke
Copy link
Author

TheBloke commented Feb 17, 2024

Frozen models don't work either, but I guess this is expected because of the id field.

It'd be really nice if it was possible to mark a model as ConfigDict(frozen=True) and have this apply to all fields except the id field. But I guess that might be a bigger ask.

For completeness, here's a reproduction case for a frozen=True model in Pydantic 2:

from pydantic import Field, ConfigDict
from motor.motor_asyncio import AsyncIOMotorClient
from beanie import Document, init_beanie

class TestFrozen(Document):
    model_config = ConfigDict(frozen=True)
    some_field: str

    class Settings:
        name = "test_frozen"

async def test_frozen():
    await init_beanie(database=client[DB_NAME], document_models=[TestFrozen])

    test_frozen = TestFrozen(some_field="test")

    await test_frozen.save()
    # also fails with test_frozen.insert()

await test_frozen()

Result:

File ~/venv/pq_dev/lib/python3.11/site-packages/beanie/odm/documents.py:719, in Document.update(self, ignore_revision, session, bulk_writer, skip_actions, skip_sync, *args, **pymongo_kwargs)
    717     if use_revision_id and not ignore_revision and result is None:
    718         raise RevisionIdWasChanged
--> 719     merge_models(self, result)
    720 return self

File ~/venv/pq_dev/lib/python3.11/site-packages/beanie/odm/utils/parsing.py:46, in merge_models(left, right)
     44     left.__setattr__(k, right_value)
     45 elif not isinstance(right_value, Link):
---> 46     left.__setattr__(k, right_value)

File ~/venv/pq_dev/lib/python3.11/site-packages/pydantic/main.py:786, in BaseModel.__setattr__(self, name, value)
    783             self.__pydantic_private__[name] = value
    784     return
--> 786 self._check_frozen(name, value)
    788 attr = getattr(self.__class__, name, None)
    789 if isinstance(attr, property):

File ~/venv/pq_dev/lib/python3.11/site-packages/pydantic/main.py:850, in BaseModel._check_frozen(self, name, value)
    844     return
    845 error: pydantic_core.InitErrorDetails = {
    846     'type': typ,
    847     'loc': (name,),
    848     'input': value,
    849 }
--> 850 raise pydantic_core.ValidationError.from_exception_data(self.__class__.__name__, [error])

ValidationError: 1 validation error for TestFrozen
id
  Instance is frozen [type=frozen_instance, input_value=ObjectId('65d09abca8cf98113d6784ca'), input_type=PydanticObjectId]
    For further information visit https://errors.pydantic.dev/2.6/v/frozen_instance

@TheBloke TheBloke changed the title [BUG] Frozen fields not supported (only frozen models) [BUG] Frozen fields and frozen models not supported (Pydantic 2) - exception thrown on .save() Feb 17, 2024
@TheBloke TheBloke changed the title [BUG] Frozen fields and frozen models not supported (Pydantic 2) - exception thrown on .save() [BUG] Frozen fields not supported (Pydantic 2) - exception thrown on .save() Feb 17, 2024
@TheBloke
Copy link
Author

Possible workaround for frozen fields:

  • Use state_management = True in Settings
  • Then use .save_changes() instead of .save() or .update()
  • So far this seems to work, because .save_changes() only updates the fields which have changed, and frozen fields can never change because of Pylance's protection.

I'm still testing this to confirm there's no edge cases, but this might be a usable workaround for me.

I'd still raise the .save() issue as a bug though, because IMHO it should be possible for Beanie to detect frozen fields and not try to update them.

@TheBloke
Copy link
Author

TheBloke commented Feb 18, 2024

Unfortunately I don't think the state_management=True work around works, at least not if links are used.

Because I also get the frozen fields exception when I do something like:

from beanie import Document, Link, WriteRules
from pydantic import Field

class Door(Document):
    height: int = Field(default=2, frozen=True)
    width: int = 1

class House(Document):
    name: str
    door: Link[Door]

door = Door(height=10, width=20)
house = House(name="test", door=door)
await house.insert(link_rule=WriteRules.WRITE)

Even though nothing has changed on Door.height, the act of inserting it with link_rule seems to cause the model to get re-written, triggering the frozen error.

@roman-right
Copy link
Member

Hi! Good catch. I'll add a test suite for this and will fix on the next bug-fixing session.

@roman-right roman-right added the bug Something isn't working label Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants