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

🐛 Allow_inf_nan option for Param subclasses #11579

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastapi/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(
max_length=max_length,
discriminator=discriminator,
multiple_of=multiple_of,
allow_nan=allow_inf_nan,
allow_inf_nan=allow_inf_nan,
max_digits=max_digits,
decimal_places=decimal_places,
**extra,
Expand Down
61 changes: 61 additions & 0 deletions test_param_allow_inf_nan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from typing import Optional

from fastapi import FastAPI
from fastapi.params import Query
from fastapi.testclient import TestClient

app = FastAPI()


@app.get("/")
def get(
x: Optional[float] = Query(default=0, allow_inf_nan=False),
y: Optional[float] = Query(default=0, allow_inf_nan=True),
z: Optional[float] = Query(default=0),
) -> str: # type: ignore
return "OK"


client = TestClient(app)


def test_allow_inf_nan_false():
response = client.get("/?x=inf")
assert response.status_code == 422, response.text

response = client.get("/?x=-inf")
assert response.status_code == 422, response.text

response = client.get("/?x=nan")
assert response.status_code == 422, response.text

response = client.get("/?x=0")
assert response.status_code == 200, response.text


def test_allow_inf_nan_true():
response = client.get("/?y=inf")
assert response.status_code == 200, response.text

response = client.get("/?y=-inf")
assert response.status_code == 200, response.text

response = client.get("/?y=nan")
assert response.status_code == 200, response.text

response = client.get("/?y=0")
assert response.status_code == 200, response.text


def test_allow_inf_nan_not_specified():
response = client.get("/?z=inf")
assert response.status_code == 200, response.text

response = client.get("/?z=-inf")
assert response.status_code == 200, response.text

response = client.get("/?z=nan")
assert response.status_code == 200, response.text

response = client.get("/?z=0")
assert response.status_code == 200, response.text