Coverage for docs_src/dataclasses/tutorial003.py: 100%

19 statements  

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

1from dataclasses import field # (1) 1abcde

2from typing import List, Union 1abcde

3 

4from fastapi import FastAPI 1abcde

5from pydantic.dataclasses import dataclass # (2) 1abcde

6 

7 

8@dataclass 1abcde

9class Item: 1abcde

10 name: str 1abcde

11 description: Union[str, None] = None 1abcde

12 

13 

14@dataclass 1abcde

15class Author: 1abcde

16 name: str 1abcde

17 items: List[Item] = field(default_factory=list) # (3) 1abcde

18 

19 

20app = FastAPI() 1abcde

21 

22 

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

24async def create_author_items(author_id: str, items: List[Item]): # (5) 1abcde

25 return {"name": author_id, "items": items} # (6) 1abcde

26 

27 

28@app.get("/authors/", response_model=List[Author]) # (7) 1abcde

29def get_authors(): # (8) 1abcde

30 return [ # (9) 1abcde

31 { 

32 "name": "Breaters", 

33 "items": [ 

34 { 

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

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

37 }, 

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

39 ], 

40 }, 

41 { 

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

43 "items": [ 

44 { 

45 "name": "Salt", 

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

47 }, 

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

49 { 

50 "name": "Lonely Night", 

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

52 }, 

53 ], 

54 }, 

55 ]