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

19 statements  

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

1from fastapi import FastAPI 1abc

2from fastapi.encoders import jsonable_encoder 1abc

3from pydantic import BaseModel 1abc

4 

5app = FastAPI() 1abc

6 

7 

8class Item(BaseModel): 1abc

9 name: str | None = None 1abc

10 description: str | None = None 1abc

11 price: float | None = None 1abc

12 tax: float = 10.5 1abc

13 tags: list[str] = [] 1abc

14 

15 

16items = { 1abc

17 "foo": {"name": "Foo", "price": 50.2}, 

18 "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2}, 

19 "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []}, 

20} 

21 

22 

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

24async def read_item(item_id: str): 1abc

25 return items[item_id] 1abc

26 

27 

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

29async def update_item(item_id: str, item: Item): 1abc

30 update_item_encoded = jsonable_encoder(item) 1abc

31 items[item_id] = update_item_encoded 1abc

32 return update_item_encoded 1abc