Browse Source

🎨 Auto format

pull/14766/head
pre-commit-ci-lite[bot] 6 months ago
committed by GitHub
parent
commit
5c4937cca6
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      fastapi/dependencies/utils.py
  2. 17
      tests/test_request_params/test_form/test_optional_literal_form.py

2
fastapi/dependencies/utils.py

@ -350,7 +350,7 @@ def add_non_field_param_to_dependency(
def _is_optional_type(annotation: Any) -> bool: def _is_optional_type(annotation: Any) -> bool:
""" """
Check if a type annotation is Optional (Union with None). Check if a type annotation is Optional (Union with None).
Optional[X] is equivalent to Union[X, None], so we check if: Optional[X] is equivalent to Union[X, None], so we check if:
1. The origin is Union 1. The origin is Union
2. NoneType is one of the union args 2. NoneType is one of the union args

17
tests/test_request_params/test_form/test_optional_literal_form.py

@ -1,4 +1,5 @@
from typing import Annotated, Literal, Optional from typing import Annotated, Literal, Optional
from fastapi import FastAPI, Form from fastapi import FastAPI, Form
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
@ -8,9 +9,7 @@ def test_optional_literal_form_none():
app = FastAPI() app = FastAPI()
@app.post("/") @app.post("/")
async def read_main( async def read_main(attribute: Annotated[Optional[Literal["abc", "def"]], Form()]):
attribute: Annotated[Optional[Literal["abc", "def"]], Form()]
):
return {"attribute": attribute} return {"attribute": attribute}
client = TestClient(app) client = TestClient(app)
@ -26,9 +25,7 @@ def test_optional_literal_form_valid_values():
app = FastAPI() app = FastAPI()
@app.post("/") @app.post("/")
async def read_main( async def read_main(attribute: Annotated[Optional[Literal["abc", "def"]], Form()]):
attribute: Annotated[Optional[Literal["abc", "def"]], Form()]
):
return {"attribute": attribute} return {"attribute": attribute}
client = TestClient(app) client = TestClient(app)
@ -49,9 +46,7 @@ def test_optional_literal_form_invalid_value():
app = FastAPI() app = FastAPI()
@app.post("/") @app.post("/")
async def read_main( async def read_main(attribute: Annotated[Optional[Literal["abc", "def"]], Form()]):
attribute: Annotated[Optional[Literal["abc", "def"]], Form()]
):
return {"attribute": attribute} return {"attribute": attribute}
client = TestClient(app) client = TestClient(app)
@ -70,9 +65,7 @@ def test_optional_literal_form_empty_string():
app = FastAPI() app = FastAPI()
@app.post("/") @app.post("/")
async def read_main( async def read_main(attribute: Annotated[Optional[Literal["abc", "def"]], Form()]):
attribute: Annotated[Optional[Literal["abc", "def"]], Form()]
):
return {"attribute": attribute} return {"attribute": attribute}
client = TestClient(app) client = TestClient(app)

Loading…
Cancel
Save