Coverage for docs_src / dependencies / tutorial008c_an_py310.py: 100%
17 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from typing import Annotated 1abcd
3from fastapi import Depends, FastAPI, HTTPException 1abcd
5app = FastAPI() 1abcd
8class InternalError(Exception): 1abcd
9 pass 1abcd
12def get_username(): 1abcd
13 try: 1eofghipjklqmn
14 yield "Rick" 1eofghipjklqmn
15 except InternalError: 1efghijklmn
16 print("Oops, we didn't raise again, Britney 😱") 1egrikln
19@app.get("/items/{item_id}") 1abcd
20def get_item(item_id: str, username: Annotated[str, Depends(get_username)]): 1abcd
21 if item_id == "portal-gun": 1eofghipjklqmn
22 raise InternalError( 1egrikln
23 f"The portal gun is too dangerous to be owned by {username}"
24 )
25 if item_id != "plumbus": 1ofhpjqm
26 raise HTTPException( 1fhjm
27 status_code=404, detail="Item not found, there's only a plumbus here"
28 )
29 return item_id 1ospq