Skip to main content

Fast and easy dependency injection framework.

Project description

Basic usage

Register 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 ServiceA:
    """ class implementation """

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

from injection import injectable

@injectable
class ServiceB:
    """ 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

class ServiceC:
    """ class implementation """

service_c = ServiceC()
set_constant(service_c)

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 some_function(service_a: ServiceA):
    """ 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 SomeDataClass:
    service_a: ServiceA = ...

Get an instance

Example with get_instance function:

from injection import get_instance

service_a = get_instance(ServiceA)

Example with get_lazy_instance function:

from injection import get_lazy_instance

lazy_service_a = get_lazy_instance(ServiceA)
# ...
service_a = ~lazy_service_a

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:

class AbstractService(ABC):
    ...

@injectable(on=AbstractService)
class ConcreteService(AbstractService):
    ...

Example with several classes:

class AbstractService(ABC):
    ...

class ConcreteService(AbstractService):
    ...

@injectable(on=(AbstractService, ConcreteService))
class ConcreteServiceOverload(ConcreteService):
    ...

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

@injectable
class InaccessibleService:
    ...

# ...

@injectable(on=InaccessibleService, mode="override")
class ServiceOverload(InaccessibleService):
    ...

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 injectable

@injectable
def service_d_recipe() -> ServiceD:
    """ 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.9.5.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

python_injection-0.9.5-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_injection-0.9.5.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1023-azure

File hashes

Hashes for python_injection-0.9.5.tar.gz
Algorithm Hash digest
SHA256 a0c3806d37e4a1ce8b803e5d8c84c8584889ca3620828ab57dfaabd4f86d694b
MD5 b8e18858f3056d9b53e0feef0fdf328f
BLAKE2b-256 b54fccbcaf966dda8761ad8aaebf6a6ac6ed62b84b13ece50d9a8b843f3bd8ee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_injection-0.9.5-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1023-azure

File hashes

Hashes for python_injection-0.9.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a96841ecc1ac61d273defe687866a1e329d53513be47d6ebf682cee209367757
MD5 ce2c922d8dcf02ee736651e41e689185
BLAKE2b-256 9a38326f7b9c865bff9ebf16545499640d8702c0adb7b2fd8fa5f44f9a85fa59

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