-
First Check
Commit to Help
Example Codefrom typing import Annotated, Literal, Optional
from fastapi import FastAPI, Form
from fastapi.testclient import TestClient
app = FastAPI()
@app.post("/")
async def read_main(
attribute : Annotated[Optional[Literal["abc", "def"]], Form()]
):
print(attribute)
client = TestClient(app)
data = {}
data["attribute"] = None
response = client.post("/", data=data)
print(response.content)
assert response.status_code == 200DescriptionIn FastAPI 0.113 .0 the above code passes. Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.114.0 Pydantic Version2.9.1 Python Version3.12.3 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
I think this is the same issue as here: #12150 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
|
With FastAPI 0.113.0: {'detail': [{'type': 'missing', 'loc': ['body', 'attribute'], 'msg': 'Field required', 'input': None}]}
Traceback (most recent call last):
File "/Users/marcelotryle/dev/encode/starlette/main.py", line 20, in <module>
assert response.status_code == 200
AssertionErrorWith FastAPI 0.114.0: {
│ 'detail': [
│ │ {
│ │ │ 'type': 'literal_error',
│ │ │ 'loc': ['body', 'attribute'],
│ │ │ 'msg': "Input should be 'abc' or 'def'",
│ │ │ 'input': '',
│ │ │ 'ctx': {'expected': "'abc' or 'def'"}
│ │ }
│ ]
}
Traceback (most recent call last):
File "/Users/marcelotryle/dev/encode/starlette/main.py", line 20, in <module>
assert response.status_code == 200
AssertionErrorThere's a difference on behavior, but the "test" doesn't pass anyway... When you add a default value to from typing import Annotated, Literal, Optional
from fastapi import FastAPI, Form
from fastapi.testclient import TestClient
from rich.pretty import pprint
app = FastAPI()
@app.post("/")
async def read_main(attribute: Annotated[Optional[Literal["abc", "def"]], Form()] = None):
print(attribute)
client = TestClient(app)
response = client.post("/")
pprint(response.json()) # pragma: no branch
assert response.status_code == 200In any case, I'll create an issue from this because I can see a change in behavior... |
Beta Was this translation helpful? Give feedback.
-
|
Hi everyone, we ran into a samilar issue and were able narrow the issue down to the behaviour of the TestClient. see: #12245 (comment) |
Beta Was this translation helpful? Give feedback.
With FastAPI 0.113.0:
{'detail': [{'type': 'missing', 'loc': ['body', 'attribute'], 'msg': 'Field required', 'input': None}]} Traceback (most recent call last): File "/Users/marcelotryle/dev/encode/starlette/main.py", line 20, in <module> assert response.status_code == 200 AssertionErrorWith FastAPI 0.114.0:
{ │ 'detail': [ │ │ { │ │ │ 'type': 'literal_error', │ │ │ 'loc': ['body', 'attribute'], │ │ │ 'msg': "Input should be 'abc' or 'def'", │ │ │ 'input': '', │ │ │ 'ctx': {'expected': "'abc' or 'def'"} │ │ } │ ] } Traceback (most recent call last): File "/Users/marcelotryle/dev/encode/starlette/main.py", line 20, in <module> assert r…