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

24 statements  

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

1from fastapi import FastAPI 1adbc

2from pydantic import BaseModel, EmailStr 1adbc

3 

4app = FastAPI() 1adbc

5 

6 

7class UserBase(BaseModel): 1adbc

8 username: str 1abc

9 email: EmailStr 1abc

10 full_name: str | None = None 1adbc

11 

12 

13class UserIn(UserBase): 1adbc

14 password: str 1abc

15 

16 

17class UserOut(UserBase): 1adbc

18 pass 1adbc

19 

20 

21class UserInDB(UserBase): 1adbc

22 hashed_password: str 1abc

23 

24 

25def fake_password_hasher(raw_password: str): 1adbc

26 return "supersecret" + raw_password 1efgh

27 

28 

29def fake_save_user(user_in: UserIn): 1adbc

30 hashed_password = fake_password_hasher(user_in.password) 1efgh

31 user_in_db = UserInDB(**user_in.model_dump(), hashed_password=hashed_password) 1efgh

32 print("User saved! ..not really") 1efgh

33 return user_in_db 1efgh

34 

35 

36@app.post("/user/", response_model=UserOut) 1adbc

37async def create_user(user_in: UserIn): 1adbc

38 user_saved = fake_save_user(user_in) 1efgh

39 return user_saved 1efgh