You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

fusebox

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fusebox

A lightweight and Pythonic dependency injection container

0.0.1
pipPyPI
Maintainers
1

Fusebox

Fusebox is a lightweight and Pythonic dependency injection (DI) container built for simplicity and minimalism. It allows you to easily register and resolve classes and inject dependencies into functions with automatic dependency resolution.

⚡️ No magic. No runtime patching. Just clean, type-safe DI.

🚀 Features

  • Minimal API surface
  • Automatic class registration and constructor-based dependency injection for classes using the @component decorator
  • Function parameter injection with @inject decorator
  • Interface-to-implementation binding
  • Caches singletons automatically
  • Pure Python, zero dependencies

📦 Installation

poetry add fusebox

📐 Quick Example

from fusebox import Container, component

@component
class ServiceA:
  def greet(self):
    return "Hello from A"

@component
class ServiceB:
  def __init__(self, service_a: ServiceA):
    self.service_a = service_a

  def greet_with_service_a(self) -> str:
    return f"ServiceB says: {self.service_a.greet()}"

service_b = Container.get(ServiceB)
print(service_b.greet_with_service_a())  # ServiceB says: Hello from A

🔁 Interface Binding

Bind an abstract base class (ABC) to a concrete implementation:

from abc import ABC, abstractmethod
from fusebox import component, Container

class Greeter(ABC):
    @abstractmethod
    def greet(self): pass

@component
class HelloGreeter(Greeter):
    def greet(self):
        return "Hello!"

greeter = Container.get(Greeter)
print(greeter.greet())  # Hello!

🪄 Function Injection with @inject

You can also inject dependencies directly into functions using the @inject decorator:

from fusebox import component, inject, Container

@component
class ServiceA:
    def greet(self):
        return "Hello from A"

@inject
def greet_with_a(a: ServiceA):
    return a.greet()

print(greet_with_a())  # Hello from A

🔀 Mixed Injection

You can combine injected dependencies with explicit parameters:

from fusebox import component, inject, Container

@component
class ServiceA:
    def greet(self):
        return "Hello from A"

@inject
def greet_with_message(a: ServiceA, message: str):
    return f"{a.greet()} - {message}"

# 'a' is injected, 'message' is passed explicitly
print(greet_with_message(message="Welcome!"))  # Hello from A - Welcome!

# You can also override injected dependencies
custom_a = ServiceA()
print(greet_with_message(a=custom_a, message="Override!"))  # Hello from A - Override!

🧾 License

MIT License. See LICENSE file.

📦 PyPI

💻 GitHub

🙌 Contributing

Pull requests are welcome! Please submit issues and suggestions to help improve the project.

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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.