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

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 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 a reference class:

from injection import singleton

class A:
    ...

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

Example with several reference 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 """

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

This version

0.5.3

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

Uploaded Source

Built Distribution

python_injection-0.5.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for python_injection-0.5.3.tar.gz
Algorithm Hash digest
SHA256 c8248938b8d384f23cbb1d5c53e2f5b5b62afb67f2f9dba075c4922712921fd3
MD5 b3834334a455edfb28c2ce68b597f098
BLAKE2b-256 20d78d511bf84d848283d6667301fa1d95ab7f325674a6b3e91a7e8303a6abc7

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for python_injection-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0d2787f7efc2b8465c87f05bd5398c21d9b1ced47b540d18bd5134cc5382fc9d
MD5 6000390c6ddd7912489f5975e4298caf
BLAKE2b-256 99553ff88760f1df72802cd8d2f980b8957df0dc2ace23fb76bbd9260ba0aee2

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