mirror of
https://github.com/fastapi/fastapi.git
synced 2026-04-05 07:34:12 -04:00
✨ Add support for function return type annotations to declare the response_model (#1436)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from typing import List, Union
|
||||
from typing import Any, List, Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
@@ -15,5 +15,13 @@ class Item(BaseModel):
|
||||
|
||||
|
||||
@app.post("/items/", response_model=Item)
|
||||
async def create_item(item: Item):
|
||||
async def create_item(item: Item) -> Any:
|
||||
return item
|
||||
|
||||
|
||||
@app.get("/items/", response_model=List[Item])
|
||||
async def read_items() -> Any:
|
||||
return [
|
||||
{"name": "Portal Gun", "price": 42.0},
|
||||
{"name": "Plumbus", "price": 32.0},
|
||||
]
|
||||
|
||||
27
docs_src/response_model/tutorial001_01.py
Normal file
27
docs_src/response_model/tutorial001_01.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from typing import List, Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Union[float, None] = None
|
||||
tags: List[str] = []
|
||||
|
||||
|
||||
@app.post("/items/")
|
||||
async def create_item(item: Item) -> Item:
|
||||
return item
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items() -> List[Item]:
|
||||
return [
|
||||
Item(name="Portal Gun", price=42.0),
|
||||
Item(name="Plumbus", price=32.0),
|
||||
]
|
||||
25
docs_src/response_model/tutorial001_01_py310.py
Normal file
25
docs_src/response_model/tutorial001_01_py310.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: str | None = None
|
||||
price: float
|
||||
tax: float | None = None
|
||||
tags: list[str] = []
|
||||
|
||||
|
||||
@app.post("/items/")
|
||||
async def create_item(item: Item) -> Item:
|
||||
return item
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items() -> list[Item]:
|
||||
return [
|
||||
Item(name="Portal Gun", price=42.0),
|
||||
Item(name="Plumbus", price=32.0),
|
||||
]
|
||||
27
docs_src/response_model/tutorial001_01_py39.py
Normal file
27
docs_src/response_model/tutorial001_01_py39.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Union[float, None] = None
|
||||
tags: list[str] = []
|
||||
|
||||
|
||||
@app.post("/items/")
|
||||
async def create_item(item: Item) -> Item:
|
||||
return item
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items() -> list[Item]:
|
||||
return [
|
||||
Item(name="Portal Gun", price=42.0),
|
||||
Item(name="Plumbus", price=32.0),
|
||||
]
|
||||
@@ -1,3 +1,5 @@
|
||||
from typing import Any
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
@@ -13,5 +15,13 @@ class Item(BaseModel):
|
||||
|
||||
|
||||
@app.post("/items/", response_model=Item)
|
||||
async def create_item(item: Item):
|
||||
async def create_item(item: Item) -> Any:
|
||||
return item
|
||||
|
||||
|
||||
@app.get("/items/", response_model=list[Item])
|
||||
async def read_items() -> Any:
|
||||
return [
|
||||
{"name": "Portal Gun", "price": 42.0},
|
||||
{"name": "Plumbus", "price": 32.0},
|
||||
]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Union
|
||||
from typing import Any, Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
@@ -15,5 +15,13 @@ class Item(BaseModel):
|
||||
|
||||
|
||||
@app.post("/items/", response_model=Item)
|
||||
async def create_item(item: Item):
|
||||
async def create_item(item: Item) -> Any:
|
||||
return item
|
||||
|
||||
|
||||
@app.get("/items/", response_model=list[Item])
|
||||
async def read_items() -> Any:
|
||||
return [
|
||||
{"name": "Portal Gun", "price": 42.0},
|
||||
{"name": "Plumbus", "price": 32.0},
|
||||
]
|
||||
|
||||
@@ -14,6 +14,6 @@ class UserIn(BaseModel):
|
||||
|
||||
|
||||
# Don't do this in production!
|
||||
@app.post("/user/", response_model=UserIn)
|
||||
async def create_user(user: UserIn):
|
||||
@app.post("/user/")
|
||||
async def create_user(user: UserIn) -> UserIn:
|
||||
return user
|
||||
|
||||
@@ -12,6 +12,6 @@ class UserIn(BaseModel):
|
||||
|
||||
|
||||
# Don't do this in production!
|
||||
@app.post("/user/", response_model=UserIn)
|
||||
async def create_user(user: UserIn):
|
||||
@app.post("/user/")
|
||||
async def create_user(user: UserIn) -> UserIn:
|
||||
return user
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Union
|
||||
from typing import Any, Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel, EmailStr
|
||||
@@ -20,5 +20,5 @@ class UserOut(BaseModel):
|
||||
|
||||
|
||||
@app.post("/user/", response_model=UserOut)
|
||||
async def create_user(user: UserIn):
|
||||
async def create_user(user: UserIn) -> Any:
|
||||
return user
|
||||
|
||||
21
docs_src/response_model/tutorial003_01.py
Normal file
21
docs_src/response_model/tutorial003_01.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel, EmailStr
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class BaseUser(BaseModel):
|
||||
username: str
|
||||
email: EmailStr
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
class UserIn(BaseUser):
|
||||
password: str
|
||||
|
||||
|
||||
@app.post("/user/")
|
||||
async def create_user(user: UserIn) -> BaseUser:
|
||||
return user
|
||||
19
docs_src/response_model/tutorial003_01_py310.py
Normal file
19
docs_src/response_model/tutorial003_01_py310.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel, EmailStr
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class BaseUser(BaseModel):
|
||||
username: str
|
||||
email: EmailStr
|
||||
full_name: str | None = None
|
||||
|
||||
|
||||
class UserIn(BaseUser):
|
||||
password: str
|
||||
|
||||
|
||||
@app.post("/user/")
|
||||
async def create_user(user: UserIn) -> BaseUser:
|
||||
return user
|
||||
@@ -1,3 +1,5 @@
|
||||
from typing import Any
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel, EmailStr
|
||||
|
||||
@@ -18,5 +20,5 @@ class UserOut(BaseModel):
|
||||
|
||||
|
||||
@app.post("/user/", response_model=UserOut)
|
||||
async def create_user(user: UserIn):
|
||||
async def create_user(user: UserIn) -> Any:
|
||||
return user
|
||||
|
||||
Reference in New Issue
Block a user