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

(2.4.0) JSON Schema for Generics models with Literal as agrument is broken #7630

Closed
1 task done
Tracked by #7617
bogdandm opened this issue Sep 26, 2023 · 5 comments · Fixed by #7654
Closed
1 task done
Tracked by #7617

(2.4.0) JSON Schema for Generics models with Literal as agrument is broken #7630

bogdandm opened this issue Sep 26, 2023 · 5 comments · Fixed by #7654
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@bogdandm
Copy link

bogdandm commented Sep 26, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Straightforward example:

pydantic v2.3.0

{'$defs': {'G_Literal_str__str__': {'properties': {'foo': {'enum': ['a', 'b'],
                                                           'title': 'Foo',
                                                           'type': 'string'}},
                                    'required': ['foo'],
                                    'title': 'G[Literal[str, str]]',
                                    'type': 'object'},
           'M': {'properties': {'foo': {'enum': ['a', 'b'],
                                        'title': 'Foo',
                                        'type': 'string'}},
                 'required': ['foo'],
                 'title': 'M',
                 'type': 'object'}}}

pydantic v2.4.0

{'$defs': {'G': {'properties': {'foo': {'title': 'Foo'}},
                 'required': ['foo'],
                 'title': 'G',
                 'type': 'object'},
           'M': {'properties': {'foo': {'enum': ['a', 'b'],
                                        'title': 'Foo',
                                        'type': 'string'}},
                 'required': ['foo'],
                 'title': 'M',
                 'type': 'object'}}}

Example Code

from pprint import pprint
from pydantic import BaseModel, RootModel
from pydantic.json_schema import models_json_schema
from typing import Generic, TypeVar, Literal

T = TypeVar('T')

class G(BaseModel, Generic[T]):
	foo: T

class M(BaseModel):
	foo: Literal['a', 'b']

pprint(models_json_schema([
	(G[Literal['a', 'b']], 'serialization'),
	(G[Literal['a', 'b']], 'validation'),
	(M, 'validation'),
])[1])

Python, Pydantic & OS Version

venv ❯ python -c "import pydantic.version; print(pydantic.version.version_info())"
             pydantic version: 2.4.0
        pydantic-core version: 2.10.0
          pydantic-core build: profile=release pgo=true
                 install path: /opt/projects/tradingview/venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.5 (main, Aug 25 2023, 13:19:50) [GCC 11.4.0]
                     platform: Linux-6.2.0-33-generic-x86_64-with-glibc2.35
             related packages: typing_extensions-4.7.1 mypy-1.0.1

Works if I downgrade to
             pydantic version: 2.3.0
        pydantic-core version: 2.6.3
@bogdandm bogdandm added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Sep 26, 2023
@bogdandm
Copy link
Author

Actually I think all generics lost its type arguments:

pprint(models_json_schema([
	(G[int], 'serialization'),
	(G[float], 'validation'),
	(M, 'validation'),
])[1])

does not generate any type for field foo too

@hramezani hramezani removed the unconfirmed Bug not yet confirmed as valid/applicable label Sep 26, 2023
@hramezani
Copy link
Member

Thanks @bogdandm for reporting this 🙏

We will provide a fix for this

@hramezani
Copy link
Member

@bogdandm Could you please test this PR to see whether it fixes your problem.

@bogdandm
Copy link
Author

bogdandm commented Sep 27, 2023

Thank you, it is definetly working now. Also it partially fixes #7376 - no more G_Literal_str__str__ in schema 🥳

{'$defs': {'G_Literal__a____b___': {'properties': {'foo': {'enum': ['a', 'b'],
                                                           'title': 'Foo',
                                                           'type': 'string'}},
                                    'required': ['foo'],
                                    'title': "G[Literal['a', 'b']]",
                                    'type': 'object'},
           'M': {'properties': {'foo': {'enum': ['a', 'b'],
                                        'title': 'Foo',
                                        'type': 'string'}},
                 'required': ['foo'],
                 'title': 'M',
                 'type': 'object'}}}

@hramezani
Copy link
Member

Thanks @bogdandm for your feedback. it will be available in next release soon

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

Successfully merging a pull request may close this issue.

2 participants