Coverage for docs_src/dependencies/tutorial008d_an_py39.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1from typing import Annotated 1abcdef

2 

3from fastapi import Depends, FastAPI, HTTPException 1abcdef

4 

5app = FastAPI() 1abcdef

6 

7 

8class InternalError(Exception): 1abcdef

9 pass 1abcdef

10 

11 

12def get_username(): 1abcdef

13 try: 1ysghztijAuklBvmnCwopDxqr

14 yield "Rick" 1ysghztijAuklBvmnCwopDxqr

15 except InternalError: 1sghtijuklvmnwopxqr

16 print("We don't swallow the internal error here, we raise again 😎") 1ghijklmnopqr

17 raise 1ghijklmnopqr

18 

19 

20@app.get("/items/{item_id}") 1abcdef

21def get_item(item_id: str, username: Annotated[str, Depends(get_username)]): 1abcdef

22 if item_id == "portal-gun": 1ysghztijAuklBvmnCwopDxqr

23 raise InternalError( 1ghijklmnopqr

24 f"The portal gun is too dangerous to be owned by {username}" 

25 ) 

26 if item_id != "plumbus": 1ysztAuBvCwDx

27 raise HTTPException( 1stuvwx

28 status_code=404, detail="Item not found, there's only a plumbus here" 

29 ) 

30 return item_id 1yzABCD