8000 Extra keyword arguments passed to fields based on `Enum` are not propagated to the schema. · Issue #2108 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content
Extra keyword arguments passed to fields based on Enum are not propagated to the schema. #2108
@bm424

Description

@bm424

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.7.2
            pydantic compiled: True
                 install path: /usr/local/lib/python3.7/site-packages/pydantic
               python version: 3.7.9 (default, Oct 13 2020, 20:59:51)  [GCC 8.3.0]
                     platform: Linux-4.15.0-122-generic-x86_64-with-debian-10.6
     optional deps. installed: ['typing-extensions', 'email-validator']

Detail

The bug is a regression, demonstrated with the following script.

from enum import Enum
from typing import List
from pydantic import BaseModel, Field


class FooEnum(str, Enum):
    foo = "foo"
    bar = "bar"
    baz = "baz"


class FoosModel(BaseModel):
    foos: List[FooEnum] = Field(examples=[["foo", "bar"]])


if __name__ == "__main__":
    print(FoosModel.schema())

This is the difference between outputs between pydantic==1.6.1 and pydantic==1.7.2 (the problem was introduced in pydantic==1.7.0).

{
    "title": "FoosModel",
    "type": "object",
    "properties": {
        "foos": {
+           "title": "Foos",
+           "examples": [["foo", "bar"]],
            "type": "array",
            "items": {"$ref": "#/definitions/FooEnum"},
        }
    },
    "required": ["foos"],
    "definitions": {
        "FooEnum": {
            "title": "FooEnum",
            "description": "An enumeration.",
            "enum": ["foo", "bar", "baz"],
            "type": "string",
        }
    },
}

The problem arises because the function which adds the extra fields to the field schema now drops out early for Enum fields: https://github.com/samuelcolvin/pydantic/blob/31bc2435d7968fdf8d1f0c5c67f0f851c1bef54e/pydantic/schema.py#L262-L264 and therefore never runs the later code which attaches the extra fields:
https://github.com/samuelcolvin/pydantic/blob/31bc2435d7968fdf8d1f0c5c67f0f851c1bef54e/pydantic/schema.py#L278-L279

I'm preparing a merge request which will fix this.

Metadata

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

      0