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

Schema generated for Literal[MyEnum.MEMBER] is too lax? #2535

Closed
3 tasks done
nymous opened this issue Mar 16, 2021 · 0 comments · Fixed by #2536
Closed
3 tasks done

Schema generated for Literal[MyEnum.MEMBER] is too lax? #2535

nymous opened this issue Mar 16, 2021 · 0 comments · Fixed by #2536
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@nymous
Copy link

nymous commented Mar 16, 2021

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

When creating a model with a property typed as Literal[MyEnum.MEMBER], Pydantic generates a schema that is too lax, describing the property as being any value of the enum instead of a specific one.

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

             pydantic version: 1.8.1
            pydantic compiled: True
                 install path: /tmp/tmp.iid8D0iv22/.venv/lib/python3.9/site-packages/pydantic
               python version: 3.9.2 (default, Feb 20 2021, 18:40:11)  [GCC 10.2.0]
                     platform: Linux-5.11.6-arch1-1-x86_64-with-glibc2.33
     optional deps. installed: ['typing-extensions']
from typing import Literal
from enum import Enum

from pydantic import BaseModel


class MyEnum(str, Enum):
    FOO = "foo"
    BAR = "bar"


class ModelWithLiteralEnumMemberAndFullEnum(BaseModel):
    kind: Literal[MyEnum.FOO]
    possible_kinds: MyEnum  # This property is there as a workaround for #2534

print(ModelWithLiteralEnumMemberAndFullEnum.schema_json())  # This schema is too lax

This gives the following schema:

{
  "title": "ModelWithLiteralEnumMemberAndFullEnum",
  "type": "object",
  "properties": {
    "kind": {
      "title": "Kind",
      "$ref": "#/definitions/MyEnum"
    },
    "possible_kinds": {
      "$ref": "#/definitions/MyEnum"
    }
  },
  "required": [
    "kind",
    "possible_kinds"
  ],
  "definitions": {
    "MyEnum": {
      "title": "MyEnum",
      "description": "An enumeration.",
      "enum": [
        "foo",
        "bar"
      ],
      "type": "string"
    }
  }
}

On the other hand, when typing the property as Literal[MyEnum.MEMBER.value], the correct JSON schema is generated, accepting only the specific value. This solution though has the drawback of placing the value in the object instead of the enum member (so you lose is MyEnum.FOO comparisons).

from typing import Literal
from enum import Enum

from pydantic import BaseModel


class MyEnum(str, Enum):
    FOO = "foo"
    BAR = "bar"


class ModelWithLiteralEnumValue(BaseModel):
    kind: Literal[MyEnum.FOO.value]

print(ModelWithLiteralEnumValue.schema_json())  # But this one is specific about what value is accepted

which gives this schema:

{
  "title": "ModelWithLiteralEnumValue",
  "type": "object",
  "properties": {
    "kind": {
      "title": "Kind",
      "enum": [
        "foo"
      ],
      "type": "string"
    }
  },
  "required": [
    "kind"
  ]
}

This depends on #2534 being fixed, because we cannot even put a property typed as Literal[MyEnum.MEMBER] alone without the .schema() method crashing.

You can also look at a more complete example in different Pydantic versions there: https://gist.github.com/nymous/686d38ba1aa150fefe6da6d78b1cb441 (same gist as #2534).

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.

1 participant