Coverage for docs_src/body_updates/tutorial001_py39.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from typing import Union 1abcd

2 

3from fastapi import FastAPI 1abcd

4from fastapi.encoders import jsonable_encoder 1abcd

5from pydantic import BaseModel 1abcd

6 

7app = FastAPI() 1abcd

8 

9 

10class Item(BaseModel): 1abcd

11 name: Union[str, None] = None 1abcd

12 description: Union[str, None] = None 1abcd

13 price: Union[float, None] = None 1abcd

14 tax: float = 10.5 1abcd

15 tags: list[str] = [] 1abcd

16 

17 

18items = { 1abcd

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} 

23 

24 

25@app.get("/items/{item_id}", response_model=Item) 1abcd

26async def read_item(item_id: str): 1abcd

27 return items[item_id] 1abcd

28 

29 

30@app.put("/items/{item_id}", response_model=Item) 1abcd

31async def update_item(item_id: str, item: Item): 1abcd

32 update_item_encoded = jsonable_encoder(item) 1abcd

33 items[item_id] = update_item_encoded 1abcd

34 return update_item_encoded 1abcd