Skip to main content

Lightweight type-based dependency injection for Python modular monoliths

Project description

spryx-di

CI PyPI Python License

Lightweight dependency injection for Python modular monoliths.

  • NestJS-inspired modules with providers, exports, and imports
  • Type-based auto-wiring via __init__ type hints
  • Module boundary enforcement at boot time
  • Zero runtime dependencies
  • Zero intrusion in domain code

Install

pip install spryx-di

Quick Example

from spryx_di import ClassProvider, Module, ApplicationContext

identity_module = Module(
    name="identity",
    providers=[
        ClassProvider(provide=UserRepository, use_class=PgUserRepository),
        ClassProvider(provide=UserReader, use_class=PgUserReader),
    ],
    exports=[UserReader],
)

orders_module = Module(
    name="orders",
    providers=[
        ClassProvider(provide=OrderRepository, use_class=PgOrderRepository),
    ],
    imports=[identity_module],
)

ctx = ApplicationContext(modules=[identity_module, orders_module])
handler = ctx.resolve(CreateOrderHandler)  # auto-wired from type hints

Handlers declare dependencies via __init__ — no decorators, no DI imports:

class CreateOrderHandler:
    def __init__(self, repo: OrderRepository, reader: UserReader) -> None:
        self._repo = repo
        self._reader = reader

Features

Feature Description
ClassProvider, FactoryProvider, ValueProvider, ExistingProvider Typed providers — use_class, use_factory, use_value, use_existing with Scope.SINGLETON (default) or TRANSIENT
Module Declarative providers, exports, imports, on_destroy
ApplicationContext Composes modules with boundary enforcement and boot-time validation
forward_ref() Circular module dependencies without Python import errors
Lifecycle on_destroy per module, auto-close managed instances, await ctx.shutdown()
FastAPI Inject(), ScopedInject(), configure(), request scope middleware
Testing override() context manager, Container with fakes

Documentation

https://spryx-ai.github.io/spryx-di

Development

make install   # deps + pre-commit hooks
make check     # lint + typecheck + tests
make docs      # serve docs locally

License

MIT

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

spryx_di-3.0.2.tar.gz (156.8 kB view details)

Uploaded Source

Built Distribution

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

spryx_di-3.0.2-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file spryx_di-3.0.2.tar.gz.

File metadata

  • Download URL: spryx_di-3.0.2.tar.gz
  • Upload date:
  • Size: 156.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spryx_di-3.0.2.tar.gz
Algorithm Hash digest
SHA256 8e49ec34f84d10daab816f147c9c99f2bb173fb6fe659f4324ca3e2190fc66e7
MD5 f6a5cba060eac6d10ff10fac206b5b06
BLAKE2b-256 6344861490d9b46762aac8808fac7fa6d23e507f2bb7b5c5e4cbd15a421a8d14

See more details on using hashes here.

Provenance

The following attestation bundles were made for spryx_di-3.0.2.tar.gz:

Publisher: release.yml on Spryx-AI/spryx-di

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spryx_di-3.0.2-py3-none-any.whl.

File metadata

  • Download URL: spryx_di-3.0.2-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spryx_di-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 77f52d53a4bd056c5fc2b7fa68fb73026f092ce17ec3595fdfb27e7dfd454c86
MD5 f5a8afa4c81e5b7d9d2bb145ead099ba
BLAKE2b-256 956bfc9297b6c43ef04d2f59e4195784b23663ed1fb1369646d1595121b513ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for spryx_di-3.0.2-py3-none-any.whl:

Publisher: release.yml on Spryx-AI/spryx-di

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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