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

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1from fastapi import FastAPI 1abcd

2from fastapi.encoders import jsonable_encoder 1abcd

3from pydantic import BaseModel 1abcd

4 

5app = FastAPI() 1abcd

6 

7 

8class Item(BaseModel): 1abcd

9 name: str | None = None 1abcd

10 description: str | None = None 1abcd

11 price: float | None = None 1abcd

12 tax: float = 10.5 1abcd

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

14 

15 

16items = { 1abcd

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) 1abcd

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

25 return items[item_id] 1ijkl

26 

27 

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

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

30 update_item_encoded = jsonable_encoder(item) 1efgh

31 items[item_id] = update_item_encoded 1efgh

32 return update_item_encoded 1efgh