art049 / odmantic

Sync and Async ODM (Object Document Mapper) for MongoDB based on python type hints
http://art049.github.io/odmantic
ISC License
1.06k stars 92 forks source link

Engine not saving the model if it contains an Optional EmbeddedModel set to None #272

Open M4rk3tt0 opened 1 year ago

M4rk3tt0 commented 1 year ago

Bug

I have an Optional EmbeddedModel inside my main model. If I don't set a value for it, when the engine tries to save the data in the database, I get the error: TypeError: 'NoneType' object is not iterable

Current Behavior

This is a basic example that shows this behavior (it's the same with the AIOEngine):

from odmantic import Model, EmbeddedModel, SyncEngine

class Profile(EmbeddedModel):
    name: str
    age: int

class User(Model):
    username: str
    password: str
    profile: Profile | None

engine = SyncEngine()

user = User(username="John", password="password")
engine.save(user)

Expected behavior

The engine should save the model even if the EmbeddedModel is initially not set.

Environment

             pydantic version: 1.10.2
            pydantic compiled: True
                 install path: D:\Projects\Python\odmantic-test\.venv\Lib\site-packages\pydantic
               python version: 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]
                     platform: Windows-10-10.0.19043-SP0
     optional deps. installed: ['typing-extensions']
art049 commented 1 year ago

Hello, thanks for the bug report.

I'll fix this asap.

paulovitorweb commented 1 year ago

Hello. Thanks for the library.

Any predictions on when this will be merged?

Creadeyh commented 1 year ago

Hello, this is still an issue in odmantic 0.9.2