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

Pydantic "Not Recognizing" Field in BaseModel with Default Value of Type #2213

Closed
ntenenz opened this issue Dec 22, 2020 · 2 comments · Fixed by #2214
Closed

Pydantic "Not Recognizing" Field in BaseModel with Default Value of Type #2213

ntenenz opened this issue Dec 22, 2020 · 2 comments · Fixed by #2214
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@ntenenz
Copy link

ntenenz commented Dec 22, 2020

Checks

  • [X ] I added a descriptive title to this issue
  • [X ] I have searched (google, github) for similar issues and couldn't find anything
  • [X ] I have read and followed the docs and still think this is a bug

Bug

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

pydantic version: 1.7.3
pydantic compiled: True
python version: 3.8.2 (default, Mar 13 2020, 10:14:16)  [GCC 9.3.0]
platform: Linux-4.19.128-microsoft-standard-x86_64-with-glibc2.29
optional deps. installed: ['typing-extensions']
import pydantic
from typing import Type, Sequence, Union

class T:
    pass

class Model(pydantic.BaseModel):
    t: Union[Type[T], Sequence[Type[T]]] = T
    
    @pydantic.validator("t", always=True)
    def make_tuple(cls, v):
        if isinstance(v, Sequence):
            return tuple(v)
        return v,

# ConfigError: Validators defined with incorrect fields: make_tuple (use check_fields=False if you're inheriting from the model and intended this)

Note: If the field is marked optional and the default value is set to None, the code works without issue.

import pydantic
from typing import Type, Sequence, Union, Optional

class T:
    pass

class Model(pydantic.BaseModel):
    t: Optional[Union[Type[T], Sequence[Type[T]]]] = None
    
    @pydantic.validator("t", always=True)
    def make_tuple(cls, v):
        if not v:
            return T,
        elif isinstance(v, Sequence):
            return tuple(v)
        return v,

Model()
# Model(t=(<class '__main__.T'>,))

Perhaps I am missing something obvious in my sleep-deprived stupor?

@ntenenz ntenenz added the bug V1 Bug related to Pydantic V1.X label Dec 22, 2020
@PrettyWood
Copy link
Member

PrettyWood commented Dec 22, 2020

Hello @ntenenz and thanks for reporting!
It looks like a bug. It seems like the default value with this type is not taken into account in the metaclass causing Model.__fields__ to not have the t key, which causes this error.
In the meantime you can use Field to force it: t: Union[Type[T], Sequence[Type[T]]] = Field(T)

EDIT: opened a PR with the fix :)

Happy holiday season 🎅

@ntenenz
Copy link
Author

ntenenz commented Dec 22, 2020

Thanks for the prompt response and fix @PrettyWood. Happy holidays! ❄️❄️❄️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants