Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

optional parameters FastAPI

please help me. In the function I set the optional parameter param as a boolean value, but the docs (swagger) do not display the data type for param. How to fix it? I'm using python 3.12.1

from fastapi import FastAPI
from datetime import date


app=FastAPI()



@app.get ("/sales")
def sales(
    date_from: date, 
    date_to: date,
    param: bool | None  = None,
):
    return date_from, date_to, param

enter image description here

on version python 3.9 everything is fine

like image 283
Leonidktoto Avatar asked Jul 22 '26 22:07

Leonidktoto


1 Answers

set the optional parameter "param" as a boolean value, but the docs do not display the data type

Well, it's not a bool, is it? You decided to make it a bool | None instead. (Sometimes pronounced Optional[bool], same thing.)

Let's fix that:

    param: bool = False,
like image 139
J_H Avatar answered Jul 25 '26 12:07

J_H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!