First check
Example
Here's a self-contained, minimal, reproducible, example with my use case:
from typing import Type
from pydantic import BaseModel
from fastapi import FastAPI
app = FastAPI()
def create_endpoint(model: Type[BaseModel]):
@app.post("/")
def post(data: model):
pass
Description
- Run mypy against this snippet
- Receive the error
Variable "model" is not valid as a type
The provided example works as intended, but mypy - rightfully - complains about this since type variables cannot be used as types. The problem however is that using Type[...] to annotate create_endpoint is correct but there's no way (that I am aware of) in FastAPI to define the type of the request body other than with an annotation.
It would be really great to have a different way to achieve this, because as of now, dynamically creating endpoints like this is a bit inconvenient.
Environment
- OS: Debian 10.8
- FastAPI Version: 0.63.0
- Python version: 3.8.6
First check
Example
Here's a self-contained, minimal, reproducible, example with my use case:
Description
Variable "model" is not valid as a typeThe provided example works as intended, but mypy - rightfully - complains about this since type variables cannot be used as types. The problem however is that using
Type[...]to annotatecreate_endpointis correct but there's no way (that I am aware of) in FastAPI to define the type of the request body other than with an annotation.It would be really great to have a different way to achieve this, because as of now, dynamically creating endpoints like this is a bit inconvenient.
Environment