Skip to main content

Fast and easy dependency injection framework.

Project description

Injection

CI Black

Fast and easy dependency injection framework.

Quick start

⚠️ Requires Python 3.10 or higher

pip install python-injection

How to use

Create an injectable

If you wish to inject a singleton, use unique decorator.

from injection import unique

@unique
class MyClass:
    """ class implementation """

If you wish to inject a new instance each time, use new decorator.

from injection import new

@new
class MyClass:
    """ class implementation """

Inject an instance

To inject one or several instances, use inject decorator. Don't forget to annotate type of parameter to inject.

from injection import inject

@inject
def my_function(instance: MyClass):
    """ function implementation """

Inheritance

In the case of inheritance, you can use the decorator parameters reference or references to link the injection to one or several other classes.

Warning: if the child class is in another file, make sure that file is imported before injection.

reference parameter example:

from injection import unique

class A:
    ...

@unique(reference=A)
class B(A):
    ...

references parameter example:

from injection import unique

class A:
    ...

class B(A):
    ...

@unique(references=(A, B))
class C(B):
    ...

Recipes

A recipe is a function that tells the injector how to construct the instance to be injected. It is important to specify the reference class(es) when defining the recipe.

from injection import unique

@unique(reference=MyClass)
def my_recipe() -> MyClass:
    """ recipe implementation """

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

python_injection-0.1.0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

python_injection-0.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file python_injection-0.1.0.tar.gz.

File metadata

  • Download URL: python_injection-0.1.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure

File hashes

Hashes for python_injection-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d3a5fceca262176d2542d2f12d47be71b5a0aff4495edddb91b2fae6a58a7917
MD5 f121ce3e1c4533d0bf48983dc71fed37
BLAKE2b-256 a2e01b144e3977a472be883e50fc591927de94e09025b86f4d2bb6ea98856e4b

See more details on using hashes here.

File details

Details for the file python_injection-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_injection-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure

File hashes

Hashes for python_injection-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4941210cd5f7fc69890333398e4cc39de8196b8e8b2ad997084901ccec93bcdd
MD5 92e77900d9fe3a4edf655a38a24c8c27
BLAKE2b-256 80841558b00df5ea4b29a1f39a87892cc385a2b041b760cf8dfef51fa815f226

See more details on using hashes here.

Supported by

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