Dependency injection framework using pure Python
Project description
Prion
A dependency injection framework using pure Python. No external dependencies.
Installation
pip install prion
Usage
Define a container by subclassing Syringe and declaring dependencies with single[T]() or factory[T]().
>>> from prion import Syringe, single, factory
>>> class AppSyringe(Syringe):
... config = single[dict]()
... session = factory[dict]()
Create an instance and register providers using decorators.
>>> syringe = AppSyringe()
>>> @syringe.config
... def create_config() -> dict:
... return {"debug": True}
>>> @syringe.session
... def create_session() -> dict:
... return {}
Access dependencies as attributes.
>>> syringe.config is syringe.config
True
>>> syringe.session is not syringe.session
True
Dependency Wiring
Providers can receive other dependencies as parameters. Parameter names are matched to dependency names automatically.
>>> class WiredSyringe(Syringe):
... config = single[dict]()
... greeting = single[str]()
>>> syringe = WiredSyringe()
>>> @syringe.config
... def create_config() -> dict:
... return {"name": "world"}
>>> @syringe.greeting
... def create_greeting(config: dict) -> str:
... return f"hello {config['name']}"
>>> syringe.greeting
'hello world'
Circular dependencies are detected at resolve time with a RuntimeError.
Eager Initialization
Use warm() to resolve all dependencies upfront. This validates that every provider is registered and detects circular dependencies at boot time.
>>> syringe = AppSyringe()
>>> @syringe.config
... def create_config() -> dict:
... return {"debug": True}
>>> @syringe.session
... def create_session() -> dict:
... return {}
>>> syringe.warm()
Lifecycle
Use reset() to clear all cached singletons, or use the container as a context manager.
>>> syringe = AppSyringe()
>>> @syringe.config
... def create_config() -> dict:
... return {"debug": True}
>>> with syringe:
... _ = syringe.config
...
>>> # singleton cache is cleared after exiting the context
Testing
Use override() to temporarily replace a provider in tests.
>>> syringe = AppSyringe()
>>> @syringe.config
... def create_config() -> dict:
... return {"debug": True}
>>> syringe.config
{'debug': True}
>>> with syringe.override("config", lambda: {"debug": False}):
... syringe.config
{'debug': False}
>>> syringe.config
{'debug': True}
Strategies
| Strategy | Behavior |
|---|---|
single[T]() |
Creates once, returns cached |
factory[T]() |
Creates a new instance each time |
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prion-0.3.0.tar.gz.
File metadata
- Download URL: prion-0.3.0.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fee46a44c4c625c9a9c1cbd6dd88725a75bc9879c42bd353a409bc8dc739ba8
|
|
| MD5 |
7ac539173db288d92c56a394e918ae10
|
|
| BLAKE2b-256 |
2063d5b7065741d1e4e92ab8d81b35fe2b4e22e0224f55d74f55dfedd5f8ebcf
|
File details
Details for the file prion-0.3.0-py3-none-any.whl.
File metadata
- Download URL: prion-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5541851eec2e81f357315a38db2e730afe54a197f80fea48d1784fdfc0c94d23
|
|
| MD5 |
69ed466be2a9ccc144a243db7a528090
|
|
| BLAKE2b-256 |
519c104ffa3dc3abd13575ff942c01ce834ba67c5ee45d3afedd3538b9411416
|