-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Defining the request body type in a dynamically created endpoint - typing issues #2901
Comments
There's a difference between runtime and when the type checking happens. References: Maybe using Generic class can help you? |
@Kludex I am aware of the differences between runtime typed and type checking, I'd just like to get them to play along nicely. Because as of now, I can either satisfy the type checker (i.e. using generics) or define the endpoint in a way FastAPI understands. Could you elaborate how you think using generics could solve this problem? Because so far, every approach I've tried leads to FastAPI not receiving the correct type for the body parameter. |
I got some issues with the generics approach... And then I recalled that I've done something "similar". Would you be satisfied with this https://github.com/Kludex/fastapi-health/blob/main/fastapi_health/route.py ? You'd need to change the implementation, but what I wanted to show you is the idea of it. EDIT: I'm going to copy paste here because I don't know if I'll change that package later on: from inspect import Parameter, Signature
from typing import Callable, List
from fastapi import Depends, Response
def health(conditions: List[Callable]):
def endpoint(**dependencies):
if all([dependency for dependency in dependencies.values()]):
return Response(status_code=200)
return Response(status_code=503)
params = []
for condition in conditions:
params.append(
Parameter(
f"param_{condition.__name__}",
kind=Parameter.POSITIONAL_OR_KEYWORD,
annotation=bool,
default=Depends(condition),
)
)
endpoint.__signature__ = Signature(params)
return endpoint |
That's a very nice solution and indeed an approach that I can use. I'd just hoped that I wouldn't have to resort to such measures to achieve such a simple task. It'd be great if FastAPI could provide a solution for this. Otherwise, maybe this could be included in the docs somewhere because I do think this is a relatively common thing to do. |
I don't know if this should be closed since the given solution doesn't have checkable type annotations. You might as well stick with the original code and tell mypy to ignore the line. See also microsoft/pyright#3510 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First check
Example
Here's a self-contained, minimal, reproducible, example with my use case:
Description
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 annotatecreate_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
The text was updated successfully, but these errors were encountered: