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

sub-model cannot always re-write field infos from the same BaseModel. #3934

Closed
neverrop opened this issue Mar 25, 2022 · 2 comments · Fixed by #3935
Closed

sub-model cannot always re-write field infos from the same BaseModel. #3934

neverrop opened this issue Mar 25, 2022 · 2 comments · Fixed by #3935
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@neverrop
Copy link

neverrop commented Mar 25, 2022

Checks

  • [-] I added a descriptive title to this issue
  • [-] I have searched (google, github) for similar issues and couldn't find anything
  • [-] 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.9.0
            pydantic compiled: True
                 install path: /home/zuobinhua/anaconda3/lib/python3.7/site-packages/pydantic
               python version: 3.7.6 (default, Jan  8 2020, 19:59:22)  [GCC 7.3.0]
                     platform: Linux-4.15.0-159-generic-x86_64-with-debian-buster-sid
     optional deps. installed: ['email-validator', 'typing-extensions']

When creating two sub models(A, B) from Pydantic BaseModel, with fields infos defined in Config. Then creating another two sub models(Model1, Model2) from A and B, trying to rewrite the extra keys (we used level in the following code) in field info. Only one of the sub models can re-write the extra value correctly.
As shown in the following code, extra in m2.fields["root"].field_info got level=3, but for m1 it still got level=1.

from typing import Optional
from pydantic import BaseModel
from pydantic import Field


class A(BaseModel):
    root: Optional[str]
    class Config:
        fields = {
            "root": {"description": "root path of data", "level": 1},
        }

class B(BaseModel):
    root: Optional[str]
    class Config:
        field = {
            "root": {"description": "root path of data", "level": 1},
        }

class Model1(A):
    root: str = Field("asa", description="image height", level=3)

class Model2(B):
    root: str = Field("asa", description="image height", level=3)

m1 = Model1()
print(m1.__fields__["root"].field_info)
# default='asa' description='image height' extra={'level': 1}

m2 = Model2()
print(m2.__fields__["root"].field_info)
# default='asa' description='image height' extra={'level': 3}
@neverrop neverrop added the bug V1 Bug related to Pydantic V1.X label Mar 25, 2022
@PrettyWood
Copy link
Member

You wrote Config.field for model B instead of Config.fields hence the difference.
Now you're right the output is not the expected one. I'll open a fix shortly

@neverrop
Copy link
Author

You wrote Config.field for model B instead of Config.fields hence the difference.
Now you're right the output is not the expected one. I'll open a fix shortly

Sorry for the mistake Lol, and thanks for your reply!

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