Coverage for docs_src/body_updates/tutorial001.py: 100%
20 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
1from typing import List, Union 1abcdefg
3from fastapi import FastAPI 1abcdefg
4from fastapi.encoders import jsonable_encoder 1abcdefg
5from pydantic import BaseModel 1abcdefg
7app = FastAPI() 1abcdefg
10class Item(BaseModel): 1abcdefg
11 name: Union[str, None] = None 1abcdefg
12 description: Union[str, None] = None 1abcdefg
13 price: Union[float, None] = None 1abcdefg
14 tax: float = 10.5 1abcdefg
15 tags: List[str] = [] 1abcdefg
18items = { 1abcdefg
19 "foo": {"name": "Foo", "price": 50.2},
20 "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
21 "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
22}
25@app.get("/items/{item_id}", response_model=Item) 1abcdefg
26async def read_item(item_id: str): 1abcdefg
27 return items[item_id] 1opqrstu
30@app.put("/items/{item_id}", response_model=Item) 1abcdefg
31async def update_item(item_id: str, item: Item): 1abcdefg
32 update_item_encoded = jsonable_encoder(item) 1hijklmn
33 items[item_id] = update_item_encoded 1hijklmn
34 return update_item_encoded 1hijklmn