1717[ read the docs ] : https://fastapi-injectable.readthedocs.io/
1818[ tests ] : https://github.com/JasperSui/fastapi-injectable/actions?workflow=Tests
1919[ pre-commit ] : https://github.com/pre-commit/pre-commit
20- [ black ] : https://github.com/psf/black
2120
2221A lightweight package that lets you use FastAPI's dependency injection anywhere - not just in route handlers. Perfect for CLI tools, background tasks, and more.
2322
@@ -27,28 +26,62 @@ A lightweight package that lets you use FastAPI's dependency injection anywhere
2726
2827## Quick Start
2928
29+ Basic example:
30+
31+ ``` python
32+ from typing import Annotated
33+
34+ from fastapi import Depends
35+ from fastapi_injectable import injectable
36+
37+ class Database :
38+ def query (self ) -> str :
39+ return " data"
40+
41+ def get_db () -> Database:
42+ return Database()
43+
44+ @injectable
45+ def process_data (db : Annotated[Database, Depends(get_db)]) -> str :
46+ return db.query()
47+
48+ # Use it anywhere!
49+ result = process_data()
50+ print (result) # Output: 'data'
51+ ```
52+
3053Key features:
3154
32- - ** Flexible Injection APIs** : Choose between decorator or function-based approaches
55+ - ** Flexible Injection APIs** : Use decorator, function-based approach or the utility function!
3356 ``` python
34- from fastapi_injectable.decorator import injectable
57+ from typing import Annotated
58+
59+ from fastapi import Depends
60+ from fastapi_injectable import injectable
3561 from fastapi_injectable.util import get_injected_obj
3662
37- # 1. Decorator approach
63+ class Database :
64+ def query (self ) -> str :
65+ return " data"
66+
67+ def get_db () -> Database:
68+ return Database()
69+
70+ # Option 1: Decorator approach
3871 @injectable
39- def process_data (db : Annotated[Database, Depends(get_db)]):
72+ def process_data (db : Annotated[Database, Depends(get_db)]) -> str :
4073 return db.query()
4174 result1 = process_data()
4275
43- # 2. Function-wrapper approach
44- def process_data (db : Annotated[Database, Depends(get_db)]):
76+ # Option 2: Function-wrapper approach
77+ def process_data (db : Annotated[Database, Depends(get_db)]) -> str :
4578 return db.query()
4679
4780 injectable_process_data = injectable(process_data)
4881 result2 = injectable_process_data()
4982
50- # 3. Use the utility
51- def process_data (db : Annotated[Database, Depends(get_db)]):
83+ # Option 3: Use the utility
84+ def process_data (db : Annotated[Database, Depends(get_db)]) -> str :
5285 return db.query()
5386
5487 result3 = get_injected_obj(process_data)
@@ -59,21 +92,40 @@ Key features:
5992
6093- ** Support for Both Sync and Async** : Works seamlessly with both synchronous and asynchronous code
6194 ``` python
62- from fastapi_injectable.decorator import injectable
95+ from typing import Annotated
6396
64- def get_service ():
97+ from fastapi import Depends
98+ from fastapi_injectable import injectable
99+
100+ class Service :
101+ async def process (self ) -> None :
102+ print (" Processing" )
103+ return
104+
105+ def get_service () -> Service:
65106 return Service()
66107
67108 @injectable
68- async def async_task (service : Annotated[Service, Depends(get_service)]):
109+ async def async_task (service : Annotated[Service, Depends(get_service)]) -> None :
69110 await service.process()
111+
112+ await async_task() # Output: Processing
70113 ```
71114
72115- ** Controlled Resource Management** : Explicit cleanup of dependencies through utility functions
73116 ``` python
74- from fastapi_injectable.decorator import injectable
117+ from collections.abc import Generator
118+
119+ from fastapi_injectable import injectable
75120 from fastapi_injectable.util import cleanup_all_exit_stacks, cleanup_exit_stack_of_func
76121
122+ class Database :
123+ def query (self ) -> str :
124+ return " data"
125+
126+ def cleanup (self ) -> None :
127+ print (" cleaning" )
128+
77129 # Define a dependency with cleanup
78130 def get_db () -> Generator[Database, None , None ]:
79131 db = Database()
@@ -82,9 +134,11 @@ Key features:
82134
83135 # Use the dependency
84136 @injectable
85- def process_data (db : Annotated[Database, Depends(get_db)]):
137+ def process_data (db : Annotated[Database, Depends(get_db)]) -> str :
86138 return db.query()
87139
140+ result = process_data()
141+
88142 # Cleanup options
89143 await cleanup_exit_stack_of_func(process_data) # Option #1: Cleanup specific function's resources
90144 await cleanup_all_exit_stacks() # Option #2: Cleanup all resources
@@ -101,12 +155,10 @@ Key features:
101155 class Mayor :
102156 pass
103157
104-
105158 class Capital :
106159 def __init__ (self , mayor : Mayor) -> None :
107160 self .mayor = mayor
108161
109-
110162 class Country :
111163 def __init__ (self , capital : Capital) -> None :
112164 self .capital = capital
@@ -177,6 +229,7 @@ This package is particularly useful for:
177229 * [ License] ( #license )
178230 * [ Issues] ( #issues )
179231 * [ Credits] ( #credits )
232+ * [ Related Issue & Discussion] ( #related-issue-discussion )
180233 * [ Bonus] ( #bonus )
181234
182235## Requirements
@@ -364,17 +417,14 @@ from fastapi_injectable.decorator import injectable
364417class Mayor :
365418 pass
366419
367-
368420class Capital :
369421 def __init__ (self , mayor : Mayor) -> None :
370422 self .mayor = mayor
371423
372-
373424class Country :
374425 def __init__ (self , capital : Capital) -> None :
375426 self .capital = capital
376427
377-
378428def get_mayor () -> Mayor:
379429 return Mayor()
380430
@@ -498,6 +548,11 @@ please [file an issue] along with a detailed description.
498548[ pip ] : https://pip.pypa.io/
499549[ his work ] : https://github.com/fastapi/fastapi/discussions/7720#discussioncomment-8661497
500550
551+ ## Related Issue & Discussion
552+
553+ - [[ Issue] Using Depends() in other functions, outside the endpoint path operation!] ( https://github.com/fastapi/fastapi/issues/1105 )
554+ - [[ Discussion] Using Depends() in other functions, outside the endpoint path operation!] ( https://github.com/fastapi/fastapi/discussions/7720 )
555+
501556## Bonus
502557
503558My blog posts about the prototype of this project:
0 commit comments