🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
DemoInstallSign in
Socket

python-injection

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

python-injection

Fast and easy dependency injection framework.

0.19.2
PyPI
Maintainers
1

python-injection

CI PyPI - Version PyPI - Downloads Ruff

Installation

⚠️ Requires Python 3.12 or higher

pip install python-injection

Features

  • Automatic dependency resolution based on type hints.
  • Support for multiple dependency lifetimes: transient, singleton, constant, and scoped.
  • Works seamlessly in both async and sync environments.
  • 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.

Motivations

  • Easy to use
  • No impact on class and function definitions
  • No tedious configuration

Quick start

Simply apply the decorators and the package takes care of the rest.

from injection import injectable, inject, singleton

@singleton
class Printer:
    def __init__(self):
        self.history = []

    def print(self, message: str):
        self.history.append(message)
        print(message)

@injectable
class Service:
    def __init__(self, printer: Printer):
        self.printer = printer

    def hello(self):
        self.printer.print("Hello world!")

@inject
def main(service: Service):
    service.hello()

if __name__ == "__main__":
    main()

Resources

⚠️ The package isn't threadsafe, for better performance in single-threaded applications and those using asyncio. So remember to use threading.Lock if you're writing a multithreaded program.

Keywords

dependencies

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts