Skip to main content

Fast and easy dependency injection framework.

Project description

Basic usage

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 """

Note: If the class needs dependencies, these will be resolved when the instance is retrieved.

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 = ...

Note: Doesn't work with Pydantic BaseModel because the signature of the __init__ method doesn't contain the dependencies.

Inheritance

In the case of inheritance, you can use the decorator parameter on 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.

Example with one class:

from injection import singleton

class A:
    ...

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

Example with several classes:

from injection import singleton

class A:
    ...

class B(A):
    ...

@singleton(on=(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 return type annotation when defining the recipe.

from injection import singleton

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

Project details


Release history Release notifications | RSS feed

This version

0.6.5

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.6.5.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

python_injection-0.6.5-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_injection-0.6.5.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.13 Linux/6.2.0-1018-azure

File hashes

Hashes for python_injection-0.6.5.tar.gz
Algorithm Hash digest
SHA256 20fa194cebc6a8593ea643cce4e3147b65b68bd3b92c7b96b6113031a17c4137
MD5 329bd2a50cc02f4c5592b801efd3ddc8
BLAKE2b-256 5945013fdd3eb34bcd2990f61fbb0a406ae7a29bc66866e224bb09d7d8e0bc2c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for python_injection-0.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 74f04df80e574ff0ba8b6cdabcf35d880b5f6c785806694cc1fd56e0b18e27bd
MD5 780b78e060a2ef73bc06bbc7f01a1252
BLAKE2b-256 c42fcfabf9b8a0d68d8bd9b60f5ff8d2397f8e3d29e244a49f7250b58c5f66c9

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