BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
1.9k stars 201 forks source link

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

Closed aksswami closed 1 week ago

aksswami commented 1 month ago

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.

github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open 30 days with no activity.

github-actions[bot] commented 1 week ago

This issue was closed because it has been stalled for 14 days with no activity.

GRASBOCK commented 1 week ago

Still an issue