Skip to content

Nested Models with optional members fail on 1.6.1 #1736

Closed
@roycollins

Description

Bug

Defining a model with Optional nested recursive submodels is throwing a required value error when validating the model. The nested models also contain nested submodels that container other recursive models potentially. This works on 1.6.0 and all versions prior that we have been using the last year. It only breaks when we move to version 1.6.1

Here is a sample of the model that we are using with basic one letter named attributes.

class Spec(BaseModel):
    spec_fields: List[str] = Field(..., alias="fields")
    filter: Optional[str]
    sort: Optional[str]


class PSpec(Spec):
    g: Optional[GSpec]

class GSpec(Spec):
    p: Optional[PSpec]

class Filter(BaseModel):
    g: Optional[GSpec]
    p: Optional[PSpec]

Trying with the following values will generate an error

        data = {"p": {"sort": "some_field:asc", "fields": []}}
        result = Filter(**data)

The error generated is:

E   pydantic.error_wrappers.ValidationError: 1 validation error for Filter
E   p -> g
E     field required (type=value_error.missing)

It seems to be disregarding the Optional attribute for GSpec field in the nested PSpec model.
Same code on 1.6.0 works as expected.

Thanks!

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

pydantic version: 1.6.1
            pydantic compiled: True
                 install path: /Users/..../.local/share/virtualenvs/..../lib/python3.7/site-packages/pydantic
               python version: 3.7.3 (default, Mar 27 2019, 09:23:15)  [Clang 10.0.1 (clang-1001.0.46.3)]
                     platform: Darwin-19.5.0-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions', 'email-validator']
import pydantic

...

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions