-
-
Notifications
You must be signed in to change notification settings - Fork 140
Description
A recent change added type checks to route functions, pulling from Flask's approach. But the documentation has examples that return ORM models directly, letting Marshmallow/Pydantic convert whatever arbitrary type into JSON for you after the return. This is only an issue when type checking, I don't see any issue with actual functionality from this change.
I would guess the issue is partially due to the examples not having a return type, which the code I'm working on requires for all functions. So, using the example from the docs, with a container class for :
# This is similar to what we're doing, by having a container class we put
# all of our responses into.
@dataclass
class ApiResponse:
message: str
data: typing.Any
@app.get('/pets/<int:pet_id>')
@app.output(PetOut)
def get_pet(pet_id: int) -> ApiResponse: #or anything that isn't flask Response / dict / str
pet = Pet.query.get(pet_id)
return ApiResponse(message="Success", data=pet)Even if you made the return type Pet, you'd run into the same issue. This causes something like MyPy to give an error like:
src/api/healthcheck.py:57:2: error: Value of type variable "T_route" of function cannot be "Callable[[], ApiResponse]"I'm not sure of a way to work around this - I'm assuming the prior approach that was the equivalent of typing.Any isn't desirable, but is there a way to configure this so we can add our own types (I'm not even sure if that's valid in Python)?
Environment:
- Python version: 3.14
- Flask version: 3.1.2
- APIFlask version: 3.0.2