-
First Check
Commit to Help
Example Codefrom typing import Union
from fastapi import FastAPI
from fastapi.testclient import TestClient
APP = FastAPI()
TESTER = TestClient(APP)
@APP.get('/', response_model=Union[int, bool])
def root():
return True
def test_root():
resp = TESTER.get("/")
assert resp.status_code == 200
assert resp.json() is True
"""
The test fails with:
def test_root():
resp = TESTER.get("/")
assert resp.status_code == 200
> assert resp.json() is True
E assert 1 is True
E + where 1 = <bound method Response.json of <Response [200]>>()
E + where <bound method Response.json of <Response [200]>> = <Response [200]>.json
"""DescriptionIf response-model is set to I expect it to return a bool. Operating SystemLinux Operating System DetailsNo response FastAPI Version0.70.0 Python Version3.7.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
The solution will be to use from typing import Union
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import StrictInt, StrictBool
APP = FastAPI()
TESTER = TestClient(APP)
@APP.get('/', response_model=Union[StrictInt, StrictBool])
def root():
return 1
def test_root():
resp = TESTER.get("/")
assert resp.status_code == 200
assert resp.json() is TrueMore info about |
Beta Was this translation helpful? Give feedback.
-
|
@uriyyo Thank you! I tried to search through the pydantic docs but couldn't find this. That's perfect. |
Beta Was this translation helpful? Give feedback.
pydantictries to convert values to destination type when it's possible.The solution will be to use
pydanticStrictIntandStrictBool:More info about
pydantic:data conversion - https://pydantic-docs.helpmanual.io/usage/models/#data-conversion
strict types - https://pydantic-docs.helpmanual.io/usage/types/#strict-types