Skip to main content

component-injector

A modern component / dependency injector for python 3.6+.

This library provides a framework-agnostic component (or dependency) injector that injects registered components into your function calls. The components to insert are identified by looking at the called function argument annotations.

When registering a component, all its base classes are registered as well unless you explicitly disable that. You can also choose to only register base classes that are not already registered with the injector.

It provides local scopes where you can register additional components or override existing components. After exiting the scope, the registry will return to the state it was in before entering the scope.

Compatibility

component-injector is compatible with python 3.6+ using the backported contextvars and dataclasses packages.

The scopes are thread-safe and when using python 3.7 also safe for for use with asyncio tasks.

Installation

component-injector is available from pypi:

pip install component-injector

Usage example

Here's a small demo on how to use the injector:

from component_injector import Injector

injector = Injector()

class O:
    pass

@injector.inject
def consumer_of_o(o: O) -> None:
    print(o)

injector.register(O())
consumer_of_o()  # 'o' wil be the registered instance.

consumer_of_o(O())  # 'o' will be this new instance.

For more examples and usage, please refer to demo.py and async_demo.py.

Development setup

For development purposes, you can clone the repository and use poetry to install and maintain the dependencies. There is no test suite. The project comes with a set of pre-commit hooks that can format (isort, black) and check your code (mypy, flake8) automatically.

git clone git@github.com:iksteen/component-injector.git
cd component-injector
poetry run pre-commit install

Release History

  • 1.1.1

    • Fix incorrect type registration when not registering bases.
    • Fix typo in documentation (thanks @trbs!).
  • 1.1.0

    • Add documentation.
    • Add support for component factories. Asynchronous factories are supported as well when injecting into asynchronous functions.
  • 1.0.2

    • Fix bug where already bound arguments were overwritten.
  • 1.0.1

    • Fix links to examples.
  • 1.0.0

    • Initial Release.

Meta

Ingmar Steen – @iksteen

Distributed under the MIT license. See LICENSE for more information.

https://github.com/iksteen/

Contributing

  1. Fork it (https://github.com/iksteen/component-injector/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Metadata

Release files for component-injector 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for component-injector 1.1.1
File Size Uploaded Provenance
component-injector-1.1.1.tar.gz 5.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for component-injector 1.1.1
File Interpreter ABI Platform
component_injector-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.9 kB

Release files / component-injector-1.1.1.tar.gz

Download URL component-injector-1.1.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1ad14f3f06c7c434af44bd4c659051a97135423d139305c37076329f530c78b1
BLAKE2b-256 checksum
How to use checksums
401fc2f60b68e6aabfaab669356c2615a2417502bcaf1d7a22999cdb74a131b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0a2 CPython/3.7.2 Linux/4.20.8-arch1-1-ARCH

Release files / component_injector-1.1.1-py3-none-any.whl

Download URL component_injector-1.1.1-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d90dd9aba90dbb2d86d1a5096e922566708fda598cfb9837a2504a3c4e130b5d
BLAKE2b-256 checksum
How to use checksums
30b77a4a191dd2a88c8095bd8ea9a2b3d8d9d766081b4d65dba4c3f9f218571d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0a2 CPython/3.7.2 Linux/4.20.8-arch1-1-ARCH

Security for component-injector 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page