mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-18 13:27:45 -04:00
📝 Update source examples and docs from Python 3.9 to 3.10 (#14900)
This commit is contained in:
committed by
GitHub
parent
d06ab3f5c7
commit
c9e2277d8b
@@ -1,43 +0,0 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel, EmailStr
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class UserIn(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
email: EmailStr
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
class UserOut(BaseModel):
|
||||
username: str
|
||||
email: EmailStr
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
class UserInDB(BaseModel):
|
||||
username: str
|
||||
hashed_password: str
|
||||
email: EmailStr
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
def fake_password_hasher(raw_password: str):
|
||||
return "supersecret" + raw_password
|
||||
|
||||
|
||||
def fake_save_user(user_in: UserIn):
|
||||
hashed_password = fake_password_hasher(user_in.password)
|
||||
user_in_db = UserInDB(**user_in.model_dump(), hashed_password=hashed_password)
|
||||
print("User saved! ..not really")
|
||||
return user_in_db
|
||||
|
||||
|
||||
@app.post("/user/", response_model=UserOut)
|
||||
async def create_user(user_in: UserIn):
|
||||
user_saved = fake_save_user(user_in)
|
||||
return user_saved
|
||||
@@ -1,41 +0,0 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel, EmailStr
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
username: str
|
||||
email: EmailStr
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
class UserIn(UserBase):
|
||||
password: str
|
||||
|
||||
|
||||
class UserOut(UserBase):
|
||||
pass
|
||||
|
||||
|
||||
class UserInDB(UserBase):
|
||||
hashed_password: str
|
||||
|
||||
|
||||
def fake_password_hasher(raw_password: str):
|
||||
return "supersecret" + raw_password
|
||||
|
||||
|
||||
def fake_save_user(user_in: UserIn):
|
||||
hashed_password = fake_password_hasher(user_in.password)
|
||||
user_in_db = UserInDB(**user_in.model_dump(), hashed_password=hashed_password)
|
||||
print("User saved! ..not really")
|
||||
return user_in_db
|
||||
|
||||
|
||||
@app.post("/user/", response_model=UserOut)
|
||||
async def create_user(user_in: UserIn):
|
||||
user_saved = fake_save_user(user_in)
|
||||
return user_saved
|
||||
@@ -1,35 +0,0 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class BaseItem(BaseModel):
|
||||
description: str
|
||||
type: str
|
||||
|
||||
|
||||
class CarItem(BaseItem):
|
||||
type: str = "car"
|
||||
|
||||
|
||||
class PlaneItem(BaseItem):
|
||||
type: str = "plane"
|
||||
size: int
|
||||
|
||||
|
||||
items = {
|
||||
"item1": {"description": "All my friends drive a low rider", "type": "car"},
|
||||
"item2": {
|
||||
"description": "Music is my aeroplane, it's my aeroplane",
|
||||
"type": "plane",
|
||||
"size": 5,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@app.get("/items/{item_id}", response_model=Union[PlaneItem, CarItem])
|
||||
async def read_item(item_id: str):
|
||||
return items[item_id]
|
||||
20
docs_src/extra_models/tutorial004_py310.py
Normal file
20
docs_src/extra_models/tutorial004_py310.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: str
|
||||
|
||||
|
||||
items = [
|
||||
{"name": "Foo", "description": "There comes my hero"},
|
||||
{"name": "Red", "description": "It's my aeroplane"},
|
||||
]
|
||||
|
||||
|
||||
@app.get("/items/", response_model=list[Item])
|
||||
async def read_items():
|
||||
return items
|
||||
8
docs_src/extra_models/tutorial005_py310.py
Normal file
8
docs_src/extra_models/tutorial005_py310.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/keyword-weights/", response_model=dict[str, float])
|
||||
async def read_keyword_weights():
|
||||
return {"foo": 2.3, "bar": 3.4}
|
||||
Reference in New Issue
Block a user