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

Pydantic unable to generate schema() when one of the fields is of type Type #4051

Closed
3 tasks done
Koratun opened this issue May 7, 2022 · 2 comments · Fixed by #4154
Closed
3 tasks done

Pydantic unable to generate schema() when one of the fields is of type Type #4051

Koratun opened this issue May 7, 2022 · 2 comments · Fixed by #4154
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@Koratun
Copy link

Koratun commented May 7, 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: C:\Users\...\.venv\Lib\site-packages\pydantic
               python version: 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]
                     platform: Windows-10-10.0.22000-SP0
     optional deps. installed: ['typing-extensions']
from pydantic import BaseModel
from typing import Type

class TypeTest(BaseModel):
  t: Type[BaseModel]

print(TypeTest.schema())

Stack Trace:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pydantic\main.py", line 647, in pydantic.main.BaseModel.schema
  File "pydantic\schema.py", line 185, in pydantic.schema.model_schema
  File "pydantic\schema.py", line 617, in pydantic.schema.model_process_schema
  File "pydantic\schema.py", line 658, in pydantic.schema.model_type_schema
  File "pydantic\schema.py", line 258, in pydantic.schema.field_schema
  File "pydantic\schema.py", line 563, in pydantic.schema.field_type_schema
  File "pydantic\schema.py", line 922, in pydantic.schema.field_singleton_schema
  File "C:\Users\...\AppData\Local\Programs\Python\Python39\lib\abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

Instantiating the TypeTest with various types works and raises Validation errors as appropriate.

>>> TypeTest(t=BaseModel)
TypeTest(t=<class 'pydantic.main.BaseModel'>)
>>> TypeTest(t=TypeTest)  
TypeTest(t=<class '__main__.TypeTest'>)
>>> TypeTest(t=int)      
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pydantic\main.py", line 331, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for TypeTest
t
  subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel)
@Koratun Koratun added the bug V1 Bug related to Pydantic V1.X label May 7, 2022
@samuelcolvin
Copy link
Member

Is this specific to Type[BaseModel] or would it happen with Tuple[AnyClass]?

PR welcome to fix this, shouldn't be too hard to ignore or use something general for these fields.

@ofercaspi
Copy link

Looks like a duplicate of #3390

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.

3 participants