Skip to content

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

@Koratun

Description

@Koratun

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)

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