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

17 statements  

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

1from typing import Annotated 1abc

2 

3from fastapi import Depends, FastAPI, HTTPException 1abc

4 

5app = FastAPI() 1abc

6 

7 

8class InternalError(Exception): 1abc

9 pass 1abc

10 

11 

12def get_username(): 1abc

13 try: 1dmefgnhijokl

14 yield "Rick" 1dmefgnhijokl

15 except InternalError: 1defghijkl

16 print("Oops, we didn't raise again, Britney 😱") 1dfgijl

17 

18 

19@app.get("/items/{item_id}") 1abc

20def get_item(item_id: str, username: Annotated[str, Depends(get_username)]): 1abc

21 if item_id == "portal-gun": 1dmefgnhijokl

22 raise InternalError( 1dfgijl

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

24 ) 

25 if item_id != "plumbus": 1menhok

26 raise HTTPException( 1ehk

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

28 ) 

29 return item_id 1mno