Coverage for docs_src / dataclasses_ / tutorial003_py310.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from dataclasses import field # (1) 1abc

2 

3from fastapi import FastAPI 1abc

4from pydantic.dataclasses import dataclass # (2) 1abc

5 

6 

7@dataclass 1abc

8class Item: 1abc

9 name: str 1abc

10 description: str | None = None 1abc

11 

12 

13@dataclass 1abc

14class Author: 1abc

15 name: str 1abc

16 items: list[Item] = field(default_factory=list) # (3) 1abc

17 

18 

19app = FastAPI() 1abc

20 

21 

22@app.post("/authors/{author_id}/items/", response_model=Author) # (4) 1abc

23async def create_author_items(author_id: str, items: list[Item]): # (5) 1abc

24 return {"name": author_id, "items": items} # (6) 1def

25 

26 

27@app.get("/authors/", response_model=list[Author]) # (7) 1abc

28def get_authors(): # (8) 1abc

29 return [ # (9) 1ghi

30 { 

31 "name": "Breaters", 

32 "items": [ 

33 { 

34 "name": "Island In The Moon", 

35 "description": "A place to be playin' and havin' fun", 

36 }, 

37 {"name": "Holy Buddies"}, 

38 ], 

39 }, 

40 { 

41 "name": "System of an Up", 

42 "items": [ 

43 { 

44 "name": "Salt", 

45 "description": "The kombucha mushroom people's favorite", 

46 }, 

47 {"name": "Pad Thai"}, 

48 { 

49 "name": "Lonely Night", 

50 "description": "The mostests lonliest nightiest of allest", 

51 }, 

52 ], 

53 }, 

54 ]