Browse Source
📝 Use return type annotation instead of `response_model` when possible (#14753)
pull/14815/head
Motov Yurii
4 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
27 changed files with
50 additions and
54 deletions
-
docs/en/docs/tutorial/path-operation-configuration.md
-
docs_src/app_testing/app_b_an_py310/main.py
-
docs_src/app_testing/app_b_an_py39/main.py
-
docs_src/app_testing/app_b_py310/main.py
-
docs_src/app_testing/app_b_py39/main.py
-
docs_src/body_updates/tutorial002_py310.py
-
docs_src/body_updates/tutorial002_py39.py
-
docs_src/path_operation_advanced_configuration/tutorial004_py310.py
-
docs_src/path_operation_advanced_configuration/tutorial004_py39.py
-
docs_src/path_operation_configuration/tutorial001_py310.py
-
docs_src/path_operation_configuration/tutorial001_py39.py
-
docs_src/path_operation_configuration/tutorial002_py310.py
-
docs_src/path_operation_configuration/tutorial002_py39.py
-
docs_src/path_operation_configuration/tutorial003_py310.py
-
docs_src/path_operation_configuration/tutorial003_py39.py
-
docs_src/path_operation_configuration/tutorial004_py310.py
-
docs_src/path_operation_configuration/tutorial004_py39.py
-
docs_src/path_operation_configuration/tutorial005_py310.py
-
docs_src/path_operation_configuration/tutorial005_py39.py
-
docs_src/security/tutorial004_an_py310.py
-
docs_src/security/tutorial004_an_py39.py
-
docs_src/security/tutorial004_py310.py
-
docs_src/security/tutorial004_py39.py
-
docs_src/security/tutorial005_an_py310.py
-
docs_src/security/tutorial005_an_py39.py
-
docs_src/security/tutorial005_py310.py
-
docs_src/security/tutorial005_py39.py
|
|
|
@ -52,7 +52,7 @@ In these cases, it could make sense to store the tags in an `Enum`. |
|
|
|
|
|
|
|
You can add a `summary` and `description`: |
|
|
|
|
|
|
|
{* ../../docs_src/path_operation_configuration/tutorial003_py310.py hl[18:19] *} |
|
|
|
{* ../../docs_src/path_operation_configuration/tutorial003_py310.py hl[17:18] *} |
|
|
|
|
|
|
|
## Description from docstring { #description-from-docstring } |
|
|
|
|
|
|
|
@ -70,7 +70,7 @@ It will be used in the interactive docs: |
|
|
|
|
|
|
|
You can specify the response description with the parameter `response_description`: |
|
|
|
|
|
|
|
{* ../../docs_src/path_operation_configuration/tutorial005_py310.py hl[19] *} |
|
|
|
{* ../../docs_src/path_operation_configuration/tutorial005_py310.py hl[18] *} |
|
|
|
|
|
|
|
/// info |
|
|
|
|
|
|
|
|
|
|
|
@ -28,8 +28,8 @@ async def read_main(item_id: str, x_token: Annotated[str, Header()]): |
|
|
|
return fake_db[item_id] |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item) |
|
|
|
async def create_item(item: Item, x_token: Annotated[str, Header()]): |
|
|
|
@app.post("/items/") |
|
|
|
async def create_item(item: Item, x_token: Annotated[str, Header()]) -> Item: |
|
|
|
if x_token != fake_secret_token: |
|
|
|
raise HTTPException(status_code=400, detail="Invalid X-Token header") |
|
|
|
if item.id in fake_db: |
|
|
|
|
|
|
|
@ -28,8 +28,8 @@ async def read_main(item_id: str, x_token: Annotated[str, Header()]): |
|
|
|
return fake_db[item_id] |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item) |
|
|
|
async def create_item(item: Item, x_token: Annotated[str, Header()]): |
|
|
|
@app.post("/items/") |
|
|
|
async def create_item(item: Item, x_token: Annotated[str, Header()]) -> Item: |
|
|
|
if x_token != fake_secret_token: |
|
|
|
raise HTTPException(status_code=400, detail="Invalid X-Token header") |
|
|
|
if item.id in fake_db: |
|
|
|
|
|
|
|
@ -26,8 +26,8 @@ async def read_main(item_id: str, x_token: str = Header()): |
|
|
|
return fake_db[item_id] |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item) |
|
|
|
async def create_item(item: Item, x_token: str = Header()): |
|
|
|
@app.post("/items/") |
|
|
|
async def create_item(item: Item, x_token: str = Header()) -> Item: |
|
|
|
if x_token != fake_secret_token: |
|
|
|
raise HTTPException(status_code=400, detail="Invalid X-Token header") |
|
|
|
if item.id in fake_db: |
|
|
|
|
|
|
|
@ -28,8 +28,8 @@ async def read_main(item_id: str, x_token: str = Header()): |
|
|
|
return fake_db[item_id] |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item) |
|
|
|
async def create_item(item: Item, x_token: str = Header()): |
|
|
|
@app.post("/items/") |
|
|
|
async def create_item(item: Item, x_token: str = Header()) -> Item: |
|
|
|
if x_token != fake_secret_token: |
|
|
|
raise HTTPException(status_code=400, detail="Invalid X-Token header") |
|
|
|
if item.id in fake_db: |
|
|
|
|
|
|
|
@ -25,8 +25,8 @@ async def read_item(item_id: str): |
|
|
|
return items[item_id] |
|
|
|
|
|
|
|
|
|
|
|
@app.patch("/items/{item_id}", response_model=Item) |
|
|
|
async def update_item(item_id: str, item: Item): |
|
|
|
@app.patch("/items/{item_id}") |
|
|
|
async def update_item(item_id: str, item: Item) -> Item: |
|
|
|
stored_item_data = items[item_id] |
|
|
|
stored_item_model = Item(**stored_item_data) |
|
|
|
update_data = item.model_dump(exclude_unset=True) |
|
|
|
|
|
|
|
@ -27,8 +27,8 @@ async def read_item(item_id: str): |
|
|
|
return items[item_id] |
|
|
|
|
|
|
|
|
|
|
|
@app.patch("/items/{item_id}", response_model=Item) |
|
|
|
async def update_item(item_id: str, item: Item): |
|
|
|
@app.patch("/items/{item_id}") |
|
|
|
async def update_item(item_id: str, item: Item) -> Item: |
|
|
|
stored_item_data = items[item_id] |
|
|
|
stored_item_model = Item(**stored_item_data) |
|
|
|
update_data = item.model_dump(exclude_unset=True) |
|
|
|
|
|
|
|
@ -12,8 +12,8 @@ class Item(BaseModel): |
|
|
|
tags: set[str] = set() |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item, summary="Create an item") |
|
|
|
async def create_item(item: Item): |
|
|
|
@app.post("/items/", summary="Create an item") |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
""" |
|
|
|
Create an item with all the information: |
|
|
|
|
|
|
|
|
|
|
|
@ -14,8 +14,8 @@ class Item(BaseModel): |
|
|
|
tags: set[str] = set() |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item, summary="Create an item") |
|
|
|
async def create_item(item: Item): |
|
|
|
@app.post("/items/", summary="Create an item") |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
""" |
|
|
|
Create an item with all the information: |
|
|
|
|
|
|
|
|
|
|
|
@ -12,6 +12,6 @@ class Item(BaseModel): |
|
|
|
tags: set[str] = set() |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item, status_code=status.HTTP_201_CREATED) |
|
|
|
async def create_item(item: Item): |
|
|
|
@app.post("/items/", status_code=status.HTTP_201_CREATED) |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
return item |
|
|
|
|
|
|
|
@ -14,6 +14,6 @@ class Item(BaseModel): |
|
|
|
tags: set[str] = set() |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item, status_code=status.HTTP_201_CREATED) |
|
|
|
async def create_item(item: Item): |
|
|
|
@app.post("/items/", status_code=status.HTTP_201_CREATED) |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
return item |
|
|
|
|
|
|
|
@ -12,8 +12,8 @@ class Item(BaseModel): |
|
|
|
tags: set[str] = set() |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item, tags=["items"]) |
|
|
|
async def create_item(item: Item): |
|
|
|
@app.post("/items/", tags=["items"]) |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
return item |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -14,8 +14,8 @@ class Item(BaseModel): |
|
|
|
tags: set[str] = set() |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item, tags=["items"]) |
|
|
|
async def create_item(item: Item): |
|
|
|
@app.post("/items/", tags=["items"]) |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
return item |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -14,9 +14,8 @@ class Item(BaseModel): |
|
|
|
|
|
|
|
@app.post( |
|
|
|
"/items/", |
|
|
|
response_model=Item, |
|
|
|
summary="Create an item", |
|
|
|
description="Create an item with all the information, name, description, price, tax and a set of unique tags", |
|
|
|
) |
|
|
|
async def create_item(item: Item): |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
return item |
|
|
|
|
|
|
|
@ -16,9 +16,8 @@ class Item(BaseModel): |
|
|
|
|
|
|
|
@app.post( |
|
|
|
"/items/", |
|
|
|
response_model=Item, |
|
|
|
summary="Create an item", |
|
|
|
description="Create an item with all the information, name, description, price, tax and a set of unique tags", |
|
|
|
) |
|
|
|
async def create_item(item: Item): |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
return item |
|
|
|
|
|
|
|
@ -12,8 +12,8 @@ class Item(BaseModel): |
|
|
|
tags: set[str] = set() |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item, summary="Create an item") |
|
|
|
async def create_item(item: Item): |
|
|
|
@app.post("/items/", summary="Create an item") |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
""" |
|
|
|
Create an item with all the information: |
|
|
|
|
|
|
|
|
|
|
|
@ -14,8 +14,8 @@ class Item(BaseModel): |
|
|
|
tags: set[str] = set() |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/items/", response_model=Item, summary="Create an item") |
|
|
|
async def create_item(item: Item): |
|
|
|
@app.post("/items/", summary="Create an item") |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
""" |
|
|
|
Create an item with all the information: |
|
|
|
|
|
|
|
|
|
|
|
@ -14,11 +14,10 @@ class Item(BaseModel): |
|
|
|
|
|
|
|
@app.post( |
|
|
|
"/items/", |
|
|
|
response_model=Item, |
|
|
|
summary="Create an item", |
|
|
|
response_description="The created item", |
|
|
|
) |
|
|
|
async def create_item(item: Item): |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
""" |
|
|
|
Create an item with all the information: |
|
|
|
|
|
|
|
|
|
|
|
@ -16,11 +16,10 @@ class Item(BaseModel): |
|
|
|
|
|
|
|
@app.post( |
|
|
|
"/items/", |
|
|
|
response_model=Item, |
|
|
|
summary="Create an item", |
|
|
|
response_description="The created item", |
|
|
|
) |
|
|
|
async def create_item(item: Item): |
|
|
|
async def create_item(item: Item) -> Item: |
|
|
|
""" |
|
|
|
Create an item with all the information: |
|
|
|
|
|
|
|
|
|
|
|
@ -133,10 +133,10 @@ async def login_for_access_token( |
|
|
|
return Token(access_token=access_token, token_type="bearer") |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/users/me/", response_model=User) |
|
|
|
@app.get("/users/me/") |
|
|
|
async def read_users_me( |
|
|
|
current_user: Annotated[User, Depends(get_current_active_user)], |
|
|
|
): |
|
|
|
) -> User: |
|
|
|
return current_user |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -133,10 +133,10 @@ async def login_for_access_token( |
|
|
|
return Token(access_token=access_token, token_type="bearer") |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/users/me/", response_model=User) |
|
|
|
@app.get("/users/me/") |
|
|
|
async def read_users_me( |
|
|
|
current_user: Annotated[User, Depends(get_current_active_user)], |
|
|
|
): |
|
|
|
) -> User: |
|
|
|
return current_user |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -130,8 +130,8 @@ async def login_for_access_token( |
|
|
|
return Token(access_token=access_token, token_type="bearer") |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/users/me/", response_model=User) |
|
|
|
async def read_users_me(current_user: User = Depends(get_current_active_user)): |
|
|
|
@app.get("/users/me/") |
|
|
|
async def read_users_me(current_user: User = Depends(get_current_active_user)) -> User: |
|
|
|
return current_user |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -131,8 +131,8 @@ async def login_for_access_token( |
|
|
|
return Token(access_token=access_token, token_type="bearer") |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/users/me/", response_model=User) |
|
|
|
async def read_users_me(current_user: User = Depends(get_current_active_user)): |
|
|
|
@app.get("/users/me/") |
|
|
|
async def read_users_me(current_user: User = Depends(get_current_active_user)) -> User: |
|
|
|
return current_user |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -160,10 +160,10 @@ async def login_for_access_token( |
|
|
|
return Token(access_token=access_token, token_type="bearer") |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/users/me/", response_model=User) |
|
|
|
@app.get("/users/me/") |
|
|
|
async def read_users_me( |
|
|
|
current_user: Annotated[User, Depends(get_current_active_user)], |
|
|
|
): |
|
|
|
) -> User: |
|
|
|
return current_user |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -160,10 +160,10 @@ async def login_for_access_token( |
|
|
|
return Token(access_token=access_token, token_type="bearer") |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/users/me/", response_model=User) |
|
|
|
@app.get("/users/me/") |
|
|
|
async def read_users_me( |
|
|
|
current_user: Annotated[User, Depends(get_current_active_user)], |
|
|
|
): |
|
|
|
) -> User: |
|
|
|
return current_user |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -159,8 +159,8 @@ async def login_for_access_token( |
|
|
|
return Token(access_token=access_token, token_type="bearer") |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/users/me/", response_model=User) |
|
|
|
async def read_users_me(current_user: User = Depends(get_current_active_user)): |
|
|
|
@app.get("/users/me/") |
|
|
|
async def read_users_me(current_user: User = Depends(get_current_active_user)) -> User: |
|
|
|
return current_user |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -160,8 +160,8 @@ async def login_for_access_token( |
|
|
|
return Token(access_token=access_token, token_type="bearer") |
|
|
|
|
|
|
|
|
|
|
|
@app.get("/users/me/", response_model=User) |
|
|
|
async def read_users_me(current_user: User = Depends(get_current_active_user)): |
|
|
|
@app.get("/users/me/") |
|
|
|
async def read_users_me(current_user: User = Depends(get_current_active_user)) -> User: |
|
|
|
return current_user |
|
|
|
|
|
|
|
|
|
|
|
|