Skip to main content

Fast and easy dependency injection framework.

Project description

How to use?

Create an injectable

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

from injection import singleton

@singleton
class Singleton:
    """ class implementation """

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

from injection import injectable


@injectable
class Injectable:
    """ 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: Injectable):
    """ function implementation """

If inject decorates a class, it will be applied to the __init__ method. Especially useful for dataclasses:

from dataclasses import dataclass

from injection import inject

@inject
@dataclass
class DataClass:
    instance: Injectable = ...

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. See load_package function.

reference parameter example:

from injection import singleton

class A:
    ...

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

references parameter example:

from injection import singleton

class A:
    ...

class B(A):
    ...

@singleton(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 singleton

@singleton(reference=Singleton)
def my_recipe() -> Singleton:
    """ recipe implementation """

Auto inject

By default, injectable and singleton decorators will automatically apply @inject to the decorated class or function. To disable it, set the auto_inject parameter to False.

from injection import singleton

@singleton(auto_inject=False)
class Singleton:
    """ class 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.3.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

python_injection-0.3.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_injection-0.3.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.13 Linux/6.2.0-1015-azure

File hashes

Hashes for python_injection-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f02ee96f161bd339804774ae420deb55afa13fb96c7f840a849b8aa0de07923f
MD5 d9befb93d1c747093a9083f5096063aa
BLAKE2b-256 18ff1f5b5847d8a8276f7eca08c4b1d0cc7e353808c907e3859293c3bb56ab20

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_injection-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.13 Linux/6.2.0-1015-azure

File hashes

Hashes for python_injection-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dfbd35b7b684c88fe4db3008ca9711359b0f45d1a9fee28741f8c9fbe499dff2
MD5 072715d2f7958a5c9612a7f6b7f21c44
BLAKE2b-256 9538b6bc7a432e1d5f883ef3fd0840696c2623e9e4d302e5848ae978e0205986

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