Coverage for docs_src / body_updates / tutorial002_py310.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1from fastapi import FastAPI 1abc
2from fastapi.encoders import jsonable_encoder 1abc
3from pydantic import BaseModel 1abc
5app = FastAPI() 1abc
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
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}
23@app.get("/items/{item_id}", response_model=Item) 1abc
24async def read_item(item_id: str): 1abc
25 return items[item_id] 1jkl
28@app.patch("/items/{item_id}") 1abc
29async def update_item(item_id: str, item: Item) -> Item: 1abc
30 stored_item_data = items[item_id] 1defghi
31 stored_item_model = Item(**stored_item_data) 1defghi
32 update_data = item.model_dump(exclude_unset=True) 1defghi
33 updated_item = stored_item_model.model_copy(update=update_data) 1defghi
34 items[item_id] = jsonable_encoder(updated_item) 1defghi
35 return updated_item 1defghi