Query fields with numeric types appear not to enforce allow_inf_nan=False, whereas gt=0, etc are enforced #11577
-
First Check
Commit to Help
Example Codeimport aiohttp
import asyncio
import math
import uvicorn
from fastapi import FastAPI, Query, Request, Response
from http import HTTPStatus
from typing import Annotated
app = FastAPI()
@app.get('/')
async def get(
x: Annotated[float | None, Query(gt=0, description='x')] = 1,
y: Annotated[float | None, Query(allow_inf_nan=False, description='y')] = 0) -> str:
assert x > 0
assert not math.isnan(y) and not math.isinf(y)
return 'OK'
async def main():
config = uvicorn.Config(app, host='127.0.0.1', port=8001)
server = uvicorn.Server(config)
task = asyncio.create_task(server.serve())
await asyncio.sleep(.1)
async with aiohttp.ClientSession() as session:
async with session.get('http://127.0.0.1:8001/?x=-1') as response:
assert response.status == HTTPStatus.UNPROCESSABLE_ENTITY
async with session.get('http://127.0.0.1:8001/?y=inf') as response:
assert response.status == HTTPStatus.UNPROCESSABLE_ENTITY
await server.shutdown()
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())DescriptionI would expect the To reproduce, save the example code above in the current directory in a file named and in the bash prompt inside the container The output will be similar to The expected response is Operating SystemLinux Operating System DetailsNo response FastAPI Version0.111.0 Pydantic Version2.7.1 Python Version3.10.14 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
I found the issue. It's here https://github.com/tiangolo/fastapi/blob/a32902606e8d7abab83a636293d62aff52fd2429/fastapi/params.py#L94 The line should read |
Beta Was this translation helpful? Give feedback.
I found the issue. It's here https://github.com/tiangolo/fastapi/blob/a32902606e8d7abab83a636293d62aff52fd2429/fastapi/params.py#L94
The line should read
allow_inf_nan=allow_inf_nan,