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 models are not valid Quart Response type #91

Open
naitian opened this issue Sep 30, 2024 · 3 comments
Open

Pydantic models are not valid Quart Response type #91

naitian opened this issue Sep 30, 2024 · 3 comments

Comments

@naitian
Copy link

naitian commented Sep 30, 2024

I've implemented a basic version of the quickstart example using pydantic models:

from pydantic import BaseModel
from datetime import datetime

from quart import Quart
from quart_schema import QuartSchema, validate_request, validate_response

app = Quart(__name__)
QuartSchema(app)

class Todo(BaseModel):
    task: str
    due: datetime | None

@app.route("/", methods=["POST"])
@validate_request(Todo)
@validate_response(Todo, 201)
async def create_todo(data: Todo) -> tuple[Todo, int]:
    """Create a Todo"""
    # Do something with data, e.g. save to the DB
    print(data)
    return data, 201

This works (the docs get generated), but pylance reports a type error (Argument of type "(data: Todo) -> Coroutine[Any, Any, tuple[Todo, int]]" cannot be assigned to parameter of type "T_route@route" -- see screenshot from VS Code). As far as I understand, it's because Pydantic are not one of the Quart return types, but I guess I would have expected the @validate_response decorator to have done some kind of type coercion?

image

I'm not totally sure how Quart-Schema works so sorry if this issue actually belongs elsewhere. Thanks.

@epistoteles
Copy link

Which version of pydantic are you using? I also had issues with pydantic BaseModels until I upgraded pydantic to the latest version.

@naitian
Copy link
Author

naitian commented Oct 17, 2024

I'm on 2.9.2, which afaik is the latest unfortunately :(. Were your issues functional or just related to the type hinting?

@pgjones
Copy link
Owner

pgjones commented Jan 1, 2025

I don't think it is possible to type hint correctly yet, I think Concatenate needs kwarg support to do so.

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

No branches or pull requests

3 participants