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

Use metadata in dataclass field to populate pydantic Field #2382

Closed
m10d opened this issue Feb 21, 2021 · 1 comment · Fixed by #2384
Closed

Use metadata in dataclass field to populate pydantic Field #2382

m10d opened this issue Feb 21, 2021 · 1 comment · Fixed by #2384

Comments

@m10d
Copy link

m10d commented Feb 21, 2021

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
                 install path: $VENV_PATH/lib/python3.8/site-packages/pydantic
               python version: 3.8.5 (default, Jul 28 2020, 12:59:40)  [GCC 9.3.0]
                     platform: Linux-5.8.0-43-generic-x86_64-with-glibc2.29
     optional deps. installed: ['typing-extensions']

from pydantic import BaseModel, Field
from pydantic.dataclasses import dataclass
from dataclasses import field

class DescriptionFromBasemodel(BaseModel):
    descr_present: int = Field(
        42,
        title='my title',
        description='descr text',)


print(DescriptionFromBasemodel.schema_json(indent=4))

@dataclass
class DataclassWithDescription:
   descr_missing: int = field(metadata=dict(
       title="my title",
       description="descr text"))

print(DataclassWithDescription.__pydantic_model__.schema_json(indent=4))

outputs as expected for raw basemodel:

{
    "title": "DescriptionFromBasemodel",
    "type": "object",
    "properties": {
        "descr_present": {
            "title": "my title",
            "description": "descr text",
            "default": 42,
            "type": "integer"
        }
    }
}

but for the model generated from the dataclass, no attrs are preserved:

    "title": "DataclassWithDescription",
    "type": "object",
    "properties": {
        "descr_missing": {
            "title": "Descr Missing",
            "type": "integer"
        }
    },
    "required": [
        "descr_missing"
    ]
}
@m10d m10d added the bug V1 Bug related to Pydantic V1.X label Feb 21, 2021
@PrettyWood
Copy link
Member

Hi @m10d
This is not a bug but a feature request related to #470.
There is no current support of what you're asking for pydantic dataclass
But I agree using metadata kwarg of field would make sense

@PrettyWood PrettyWood added feature request and removed bug V1 Bug related to Pydantic V1.X labels Feb 21, 2021
@PrettyWood PrettyWood changed the title pydantic.dataclasses not plumbing metadata for schema generation (namely, field description) Use metadata in dataclass field to populate pydantic Field Feb 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants