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

when a model has many optional field, it will raise unwanted validation type error #407

Closed
brightgems opened this issue Jan 16, 2024 · 4 comments · Fixed by #416
Closed
Labels
bug Something isn't working

Comments

@brightgems
Copy link

Bug

it will raise unwanted validation type error when a model has many optional field;
pydantic won't raise any error

from odmantic import Model,Field
from pydantic import BaseModel
from typing import Optional, Dict

class DummpyEmbedModel(Model):
"""item url and title, thumbnail
treat as task queue
"""
infos: Optional[Dict] = None
# all image text in item page
pso: Optional[str] = None
gptTags: Optional[Dict] = None

Current Behavior

self = DummpyEmbedModel(), data = {'infos': {}, 'pso': 'xx'}

    def __init__(self, **data: Any):
>       super().__init__(**data)
E       pydantic_core._pydantic_core.ValidationError: 1 validation error for DummpyEmbedModel
E       pso
E         Input should be a valid dictionary [type=dict_type, input_value='xx', input_type=str]
E           For further information visit https://errors.pydantic.dev/2.5/v/dict_type

Expected behavior

should no raise any error as pydantic

Environment

  • ODMantic version: ...
  • MongoDB version: ...
  • Pydantic infos (output of python -c "import pydantic.utils; print(pydantic.utils.version_info())):

class DummpyEmbedModel(Model):
    """item url and title, thumbnail
    treat as task queue
    """
    infos: Optional[Dict] = None
    # all image text in item page
    pso: Optional[str] = None
    gptTags: Optional[Dict] = None
    
...
  • Version of additional modules (if relevant):
    • ...

Additional context

v1.0.0

@brightgems brightgems added the bug Something isn't working label Jan 16, 2024
@laveolus
Copy link

In v1.0.0, all optional fields are aliased to the (first) field type, see #379.

@francisdeh
Copy link

francisdeh commented Jan 21, 2024

@laveolus and @brightgems and @Jibola, until this is addressed, have you found any temporary workaround to this? I also encountered same.

@pr0gg3d
Copy link

pr0gg3d commented Feb 1, 2024

We are also plenty of Optional[] fields with different types, and this makes odmantic 1.0.0 pretty much unusable.

Anyone found a workaround for this? Thanks!!

@Jibola
Copy link

Jibola commented Feb 26, 2024

@laveolus and @brightgems and @Jibola, until this is addressed, have you found any temporary workaround to this? I also encountered same.

Currently, I have just leveraged the Pydantic BaseModel class as my workaround for now. If it's unavoidable, I've settled for removing the Optional field usage.

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

Successfully merging a pull request may close this issue.

5 participants