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

28 statements  

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

1from fastapi import FastAPI 1abc

2from pydantic import BaseModel, EmailStr 1abc

3 

4app = FastAPI() 1abc

5 

6 

7class UserIn(BaseModel): 1abc

8 username: str 1abc

9 password: str 1abc

10 email: EmailStr 1abc

11 full_name: str | None = None 1abc

12 

13 

14class UserOut(BaseModel): 1abc

15 username: str 1abc

16 email: EmailStr 1abc

17 full_name: str | None = None 1abc

18 

19 

20class UserInDB(BaseModel): 1abc

21 username: str 1abc

22 hashed_password: str 1abc

23 email: EmailStr 1abc

24 full_name: str | None = None 1abc

25 

26 

27def fake_password_hasher(raw_password: str): 1abc

28 return "supersecret" + raw_password 1def

29 

30 

31def fake_save_user(user_in: UserIn): 1abc

32 hashed_password = fake_password_hasher(user_in.password) 1def

33 user_in_db = UserInDB(**user_in.model_dump(), hashed_password=hashed_password) 1def

34 print("User saved! ..not really") 1def

35 return user_in_db 1def

36 

37 

38@app.post("/user/", response_model=UserOut) 1abc

39async def create_user(user_in: UserIn): 1abc

40 user_saved = fake_save_user(user_in) 1def

41 return user_saved 1def