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

18 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from dataclasses import field # (1) 1abfgcde

2 

3from fastapi import FastAPI 1abfgcde

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

5 

6 

7@dataclass 1abfgcde

8class Item: 1abfgcde

9 name: str 1abcde

10 description: str | None = None 1abfgcde

11 

12 

13@dataclass 1abfgcde

14class Author: 1abfgcde

15 name: str 1abcde

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

17 

18 

19app = FastAPI() 1abfgcde

20 

21 

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

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

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

25 

26 

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

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

29 return [ # (9) 1lmno

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 ]