]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
📝 Use return type annotation instead of `response_model` when possible (#14753)
authorMotov Yurii <109919500+YuriiMotov@users.noreply.github.com>
Wed, 4 Feb 2026 12:07:26 +0000 (15:07 +0300)
committerGitHub <noreply@github.com>
Wed, 4 Feb 2026 12:07:26 +0000 (13:07 +0100)
27 files changed:
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

index 59be5ab37673c935e34353d537bfe9abcee8f595..1bb7ee5544b2eeb1614ec424458012872a406b55 100644 (file)
@@ -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
 
index c5952be0b3e1c7502469192aa9a0f351d1f5b104..120289f56dde329ad44978d7bfc815a4e739f238 100644 (file)
@@ -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:
index 142e23a26ae89d1fbd653410b69ba28b0dcda042..801d5f21ead8f7b8c22a3a41644f9794c7c02d3e 100644 (file)
@@ -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:
index eccedcc7ce2ecb208a75bd0a8b5c1f02040622ba..6c5c341308644d22b4f6a9deee39b3ba8b4fb16a 100644 (file)
@@ -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:
index 45a103378388a042aef5c23281263285ec5e9fd0..89053c432c051ed7dfb2d85d092e6c2a70c62abf 100644 (file)
@@ -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:
index e5db711108001ec58b6ef362767e62eb3ce33ec4..d30e41027c024a56ecbb6acec2580f6aae9bea5b 100644 (file)
@@ -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)
index eddd7af716397fdec6f2c4dcd67f6162168db887..3714b5a55d8a32f01117929671d63c15d126d50f 100644 (file)
@@ -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)
index a815a564b7db58124faf2b31d044dab96b6df3cc..f222b11dc6e09a735f6969e3164acb7771a16220 100644 (file)
@@ -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:
 
index d5fe6705ca351714bd54894d974b20c9d216e44a..8fabe7cb800cd259d08ea4c9073278cda02bea70 100644 (file)
@@ -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:
 
index da078fdf582fe1ad15ca4c9f3fabf7947530d3de..2e7488ea4f903a78b797b2749f8ef185e45fea97 100644 (file)
@@ -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
index a9dcbf3898b9de1e0b880f593881149411942da9..09b31828214b9c28651e4e7155ebb93d56af1c93 100644 (file)
@@ -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
index 9a8af543276fe708328d955c54233e951f5136ee..59908ed7c8c0332646f16dba0b26080979d2374d 100644 (file)
@@ -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
 
 
index e7ced7de7e6a1eabfba7aac2148e1f588957a7d5..fca3b0de9e679f6613ab25f8de22bbecf531ac99 100644 (file)
@@ -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
 
 
index 3d94afe2c054f2fba2720d8bfdc141f59d237a6c..56bd7e36aacacd0367659b5c154b42b8066cdc55 100644 (file)
@@ -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
index 607c5707e6e30ed6000b1c8b0daa34fc9c99a13b..a77fb34d8904ddf9d40813ffe890e5a54b7470f3 100644 (file)
@@ -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
index 4cb8bdd438ab16d74b0aa56093620f46c6337b4e..44404aa0836e7aa05deb28fa76a8cf2cffbc9cd5 100644 (file)
@@ -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:
 
index fc25680c5a3d62d929d2b0cf442d9d1eb1c7ca07..31dfbff1d23a6710236cc0ead85d6aef3db32087 100644 (file)
@@ -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:
 
index b176631d846bbe78d2e87d7145950041779857be..a4129d600a31f9cbbffbaa17404b98dc5c98f489 100644 (file)
@@ -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:
 
index ddf29b733d07cbdbbb9308669f9a7e8e40456548..0a53a8f2ddc6587cf291e10f2d255f7db9969f9a 100644 (file)
@@ -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:
 
index 18ea96bc5cdb42ac387cf8723aef1eb818b9ecee..368c743bf921a265aed385aeb7d1936cb152793a 100644 (file)
@@ -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
 
 
index d3fd29e5a5c847ffcbcfc982d1b8fb1a7bbb8aa2..73b3d456d1487d1ec1cd9c5b98032e2b179cadeb 100644 (file)
@@ -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
 
 
index cd1dcff4604032233597610cab42e804884ace4c..8d0785b404cd13a100e90beaa45f68848f1e7eb4 100644 (file)
@@ -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
 
 
index 130dc699a086dde4ff05ba8c2b6f046fa4c9e3ce..e67403d5d7dabdc6b2e4e0fb7d598f97ddb0cdbd 100644 (file)
@@ -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
 
 
index df55951c074c938f5cbaaec1df35851af6dc6bef..fef0ab71ca2fee0f5ef2b18600c9a7506a25011b 100644 (file)
@@ -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
 
 
index 983c1c22cf93072a607a0072de74e68d3618abfd..1aeba688a657f033b764eb5dc87ed37487d200a3 100644 (file)
@@ -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
 
 
index d08e2c59f33b0ea2e727ff5d69bae45c7195517f..412fbf798484db67a1acacc6e92c6559b4f62f76 100644 (file)
@@ -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
 
 
index 5bde47ef478c04fca709302b37004f591d6c9be3..32280aa48b16d8ab4d2fdecf7730eae62b726379 100644 (file)
@@ -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