Skip to main content

Fast and easy dependency injection framework.

Project description

Basic usage

Create an injectable

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

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

If you have a constant (such as a global variable) and wish to register it as an injectable, use set_constant function.

from injection import set_constant

app = set_constant(Application())

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:

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

from dataclasses import dataclass

from injection import inject

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

Get an instance

Example with get_instance function:

from injection import get_instance

instance = get_instance(Injectable)

Example with get_lazy_instance function:

from injection import get_lazy_instance

lazy_instance = get_lazy_instance(Injectable)
# ...
instance = ~lazy_instance

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):
    ...

If a class is registered in a package and you want to override it, there is the override parameter:

@singleton
class A:
    ...

# ...

@singleton(on=A, override=True)
class B(A):
    ...

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

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

Uploaded Source

Built Distribution

python_injection-0.7.5-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_injection-0.7.5.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.14 Linux/6.5.0-1016-azure

File hashes

Hashes for python_injection-0.7.5.tar.gz
Algorithm Hash digest
SHA256 e822df8e00f0f805d4fd2cc4a7ddddb1afdc433f55574704e50dc7aae92e57ed
MD5 6d34f4ffbe360fe8f66bef437bb2f4ca
BLAKE2b-256 2e8be8202b11eeb06f5c2be39b18dc0b59a6d6feadc4670fbbfc9afbd766f465

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_injection-0.7.5-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.14 Linux/6.5.0-1016-azure

File hashes

Hashes for python_injection-0.7.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cf4b1f62686137c4dedab60d5f6d06cda8d056f9625248c961ec001ef002d20b
MD5 5fde29c5027cc598cc2eb5691059ef55
BLAKE2b-256 fc8e3f71a257aa7f06bc81df817f0aded6801f1474ea5f1bc627384bfaa61944

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