Repository: pydantic/pydantic

Issue:
Pydantic unable to generate schema() when one of the fields is of type Type
### Checks

* [x] I added a descriptive title to this issue
* [x] I have searched (google, github) for similar issues and couldn't find anything
* [x] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this is a bug

<!-- Sorry to sound so draconian, but every second saved replying to issues is time spend improving pydantic :-) -->

# 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']
```
<!-- or if you're using pydantic prior to v1.3, manually include: OS, python version and pydantic version -->

<!-- Please read the [docs](https://pydantic-docs.helpmanual.io/) and search through issues to
confirm your bug hasn't already been reported. -->

<!-- Where possible please include a self-contained code snippet describing your bug: -->

```py
from pydantic import BaseModel
from typing import Type

class TypeTest(BaseModel):
  t: Type[BaseModel]

print(TypeTest.schema())
```

Stack Trace:
```py
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.
```py
>>> 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)
```
Value with type Type not declarable with JSON Schema
Hi everyone, I got a Value error when I try to document my python modules with sphinx autodoc pydantic, it seems like pydantic does not support typing Type field's type in json conversion.

I declare the problematic field as `element_class: Type[RadialElement] = Field(..., description="blablabla")` at the root of a RadialAssembly class inheriting from pydantic BaseModel. RadialElement is another class derived from BaseModel.

Here is the complete log :

```
# Sphinx version: 3.5.1
# Python version: 3.6.8 (CPython)
# Docutils version: 0.16 release
# Jinja2 version: 2.11.3
# Last messages:
#   [config changed ('autodoc_pydantic_model_show_json')]
#   
#   4 added, 0 changed, 0 removed
#   
#   reading sources... [ 25%] index
#   
#   reading sources... [ 50%] links
#   
#   reading sources... [ 75%] ttm1d
#   
# Loaded extensions:
#   sphinx.ext.mathjax (3.5.1) from C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\ext\mathjax.py
#   sphinxcontrib.applehelp (1.0.2) from c:\users\gastonant\pycharmprojects\soflex\venv\lib\site-packages\sphinxcontrib\applehelp\__init__.py
#   sphinxcontrib.devhelp (1.0.2) from c:\users\gastonant\pycharmprojects\soflex\venv\lib\site-packages\sphinxcontrib\devhelp\__init__.py
#   sphinxcontrib.htmlhelp (1.0.3) from c:\users\gastonant\pycharmprojects\soflex\venv\lib\site-packages\sphinxcontrib\htmlhelp\__init__.py
#   sphinxcontrib.serializinghtml (1.1.4) from c:\users\gastonant\pycharmprojects\soflex\venv\lib\site-packages\sphinxcontrib\serializinghtml\__init__.py
#   sphinxcontrib.qthelp (1.0.3) from c:\users\gastonant\pycharmprojects\soflex\venv\lib\site-packages\sphinxcontrib\qthelp\__init__.py
#   alabaster (0.7.12) from C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\alabaster\__init__.py
#   sphinx.ext.autodoc.type_comment (3.5.1) from C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\ext\autodoc\type_comment.py
#   sphinx.ext.autodoc (3.5.1) from C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\ext\autodoc\__init__.py
#   sphinx.ext.autosummary (3.5.1) from C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\ext\autosummary\__init__.py
#   numpydoc (1.1.0) from C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\numpydoc\__init__.py
#   sphinx-pydantic (0.1.1) from C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx-pydantic\__init__.py
#   sphinxcontrib.autodoc_pydantic (1.3.0) from c:\users\gastonant\pycharmprojects\soflex\venv\lib\site-packages\sphinxcontrib\autodoc_pydantic\__init__.py
Traceback (most recent call last):
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\cmd\build.py", line 280, in build_main
    app.build(args.force_all, filenames)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\application.py", line 352, in build
    self.builder.build_update()
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\builders\__init__.py", line 298, in build_update
    len(to_build))
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\builders\__init__.py", line 310, in build
    updated_docnames = set(self.read())
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\builders\__init__.py", line 417, in read
    self._read_serial(docnames)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\builders\__init__.py", line 438, in _read_serial
    self.read_doc(docname)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\builders\__init__.py", line 478, in read_doc
    doctree = read_doc(self.app, self.env, self.env.doc2path(docname))
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\io.py", line 221, in read_doc
    pub.publish()
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\core.py", line 218, in publish
    self.settings)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\io.py", line 126, in read
    self.parse()
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\readers\__init__.py", line 77, in parse
    self.parser.parse(self.input, document)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\parsers.py", line 104, in parse
    self.statemachine.run(inputlines, document, inliner=self.inliner)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\parsers\rst\states.py", line 171, in run
    input_source=document['source'])
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\statemachine.py", line 242, in run
    context, state, transitions)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\statemachine.py", line 459, in check_line
    return method(match, context, next_state)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\parsers\rst\states.py", line 2769, in underline
    self.section(title, source, style, lineno - 1, messages)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\parsers\rst\states.py", line 327, in section
    self.new_subsection(title, lineno, messages)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\parsers\rst\states.py", line 395, in new_subsection
    node=section_node, match_titles=True)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\parsers\rst\states.py", line 282, in nested_parse
    node=node, match_titles=match_titles)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\parsers\rst\states.py", line 196, in run
    results = StateMachineWS.run(self, input_lines, input_offset)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\statemachine.py", line 242, in run
    context, state, transitions)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\statemachine.py", line 459, in check_line
    return method(match, context, next_state)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\parsers\rst\states.py", line 2342, in explicit_markup
    nodelist, blank_finish = self.explicit_construct(match)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\parsers\rst\states.py", line 2354, in explicit_construct
    return method(self, expmatch)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\parsers\rst\states.py", line 2097, in directive
    directive_class, match, type_name, option_presets)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\docutils\parsers\rst\states.py", line 2146, in run_directive
    result = directive_instance.run()
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\ext\autodoc\directive.py", line 167, in run
    documenter.generate(more_content=self.content)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\ext\autodoc\__init__.py", line 970, in generate
    self.document_members(all_members)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\ext\autodoc\__init__.py", line 853, in document_members
    check_module=members_check_module and not isattr)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\ext\autodoc\__init__.py", line 1728, in generate
    all_members=all_members)
  File "C:\Users\gastonant\PycharmProjects\SoFlex\venv\lib\site-packages\sphinx\ext\autodoc\__init__.py", line 967, in generate
    self.add_content(more_content)
  File "c:\users\gastonant\pycharmprojects\soflex\venv\lib\site-packages\sphinxcontrib\autodoc_pydantic\autodoc.py", line 201, in add_content
    self.add_collapsable_schema()
  File "c:\users\gastonant\pycharmprojects\soflex\venv\lib\site-packages\sphinxcontrib\autodoc_pydantic\autodoc.py", line 217, in add_collapsable_schema
    schema_json = ModelWrapper(self.object).get_safe_schema_json()
  File "c:\users\gastonant\pycharmprojects\soflex\venv\lib\site-packages\sphinxcontrib\autodoc_pydantic\inspection.py", line 297, in get_safe_schema_json
    return self.model.schema_json()
  File "pydantic\main.py", line 721, in pydantic.main.BaseModel.schema_json
  File "pydantic\main.py", line 710, in pydantic.main.BaseModel.schema
  File "pydantic\schema.py", line 167, in pydantic.schema.model_schema
  File "pydantic\schema.py", line 548, in pydantic.schema.model_process_schema
  File "pydantic\schema.py", line 589, in pydantic.schema.model_type_schema
  File "pydantic\schema.py", line 241, in pydantic.schema.field_schema
  File "pydantic\schema.py", line 495, in pydantic.schema.field_type_schema
  File "pydantic\schema.py", line 863, in pydantic.schema.field_singleton_schema
ValueError: Value not declarable with JSON Schema, field: name='element_class' type=Type required=True

```


Discussion:
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.
Looks like a duplicate of #3390 


Resolving patch:
diff --git a/pydantic/schema.py b/pydantic/schema.py
--- a/pydantic/schema.py
+++ b/pydantic/schema.py
@@ -383,6 +383,7 @@ def get_flat_models_from_field(field: ModelField, known_models: TypeModelSet) ->
     field_type = field.type_
     if lenient_issubclass(getattr(field_type, '__pydantic_model__', None), BaseModel):
         field_type = field_type.__pydantic_model__
+
     if field.sub_fields and not lenient_issubclass(field_type, BaseModel):
         flat_models |= get_flat_models_from_fields(field.sub_fields, known_models=known_models)
     elif lenient_issubclass(field_type, BaseModel) and field_type not in known_models:
@@ -850,7 +851,7 @@ def field_singleton_schema(  # noqa: C901 (ignore complexity)
             ref_template=ref_template,
             known_models=known_models,
         )
-    if field_type is Any or field_type is object or field_type.__class__ == TypeVar:
+    if field_type is Any or field_type is object or field_type.__class__ == TypeVar or get_origin(field_type) is type:
         return {}, definitions, nested_models  # no restrictions
     if is_none_type(field_type):
         return {'type': 'null'}, definitions, nested_models


Test patch:
diff --git a/tests/test_schema.py b/tests/test_schema.py
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -21,6 +21,7 @@
     Optional,
     Set,
     Tuple,
+    Type,
     TypeVar,
     Union,
 )
@@ -3029,3 +3030,22 @@ class Model(A):
             'level': 3,
         }
     }
+
+
+def test_model_with_type_attributes():
+    class Foo:
+        a: float
+
+    class Bar(BaseModel):
+        b: int
+
+    class Baz(BaseModel):
+        a: Type[Foo]
+        b: Type[Bar]
+
+    assert Baz.schema() == {
+        'title': 'Baz',
+        'type': 'object',
+        'properties': {'a': {'title': 'A'}, 'b': {'title': 'B'}},
+        'required': ['a', 'b'],
+    }