Skip to main content

A lightweight and Pythonic dependency injection container

Project description

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.

🌍 Links

📦 PyPI

💻 GitHub

🙌 Contributing

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fusebox-0.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fusebox-0.0.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file fusebox-0.0.1.tar.gz.

File metadata

  • Download URL: fusebox-0.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0

File hashes

Hashes for fusebox-0.0.1.tar.gz
Algorithm Hash digest
SHA256 05bbac043a61c87fc42493127278d97be101bcdc7ce0e96fcb5af6ce6c69614e
MD5 fcd29607b8d23a4bc0d9075efeb28372
BLAKE2b-256 c21e3675f921078c2fd359c000db039764a2708a41a61c4fe72df1fac7c52c53

See more details on using hashes here.

File details

Details for the file fusebox-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: fusebox-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0

File hashes

Hashes for fusebox-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 90c649a3fd3167f9e248587d80daba7f285f02301c68c7d9e3146d10a81e535e
MD5 b75ebb98a40edb4c91610498a84c3866
BLAKE2b-256 4eaa8d0c915f8d51c4855932c0b601386b4fd272f1a16bfc17ba05d547cf99db

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page