Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Dead-simple dependency injection framework for Python.
License
100nm/python-injection
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
pip install python-injection
- Automatic dependency resolution based on type hints.
- Support for multiple dependency lifetimes:
transient,singleton,constant, andscoped. - Works seamlessly in both
asyncandsyncenvironments. - Separation of dependency sets using modules.
- Runtime switching between different sets of dependencies.
- Centralized setup logic using entrypoints.
- Built-in type annotation for easy integration with
FastAPI. - Lazy dependency resolution for optimized performance.
- Easy to use
- No impact on class and function definitions
- No tedious configuration
Simply apply the decorators and the package takes care of the rest.
frominjectionimportinjectable,inject,singleton@singletonclassPrinter:def__init__(self):self.history= []defprint(self,message:str):self.history.append(message)print(message)@injectableclassService:def__init__(self,printer:Printer):self.printer=printerdefhello(self):self.printer.print("Hello world!")@injectdefmain(service:Service):service.hello()if__name__=="__main__":main()
⚠️ The package isn't threadsafe by default, for better performance in single-threaded applications and those usingasyncio.Non-threadsafe functions are those that resolve dependencies or define scopes. They all come with an optionalparameter
threadsafe.You can set
PYTHON_INJECTION_THREADSAFE=1in environment variables to make the package fully threadsafe. Theenvironment variable is resolved at thePython module level, so be careful if the variable is defined dynamically.
About
Dead-simple dependency injection framework for Python.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.