beanie icon indicating copy to clipboard operation
beanie copied to clipboard

[BUG] pydantic computed properties omitted during `insert_many` operation on Document

Open aksswami opened this issue 9 months ago • 0 comments

Describe the bug Pydantic V2 have a bug where __iter__ does not include computed property. https://github.com/pydantic/pydantic/issues/8564

This cause the document to omit the computed properties during insert, as Encoder is using __iter__ to get all properties.

To Reproduce

from beanie import Document


class TestModel(Document):
    normal: int

    @pydantic.computed_field
    @property
    def computed(self) -> int:
        return 1


instance = TestModel(normal=42)

assert {field: value for field, value in instance} == instance.model_dump()  # fails

# or 
TestModel.insert_many([instance]) # This document in mongo will omit `computed` property.

Expected behavior Expect all properties to be included during the insert operation.

Additional context As this issue is still open on pydantic, not sure if we need to wait for a fix from pydantic.

aksswami avatar May 14 '24 14:05 aksswami