Skip to main content

Dependency Injection library for Python using decorators.

Project description

rr-inject

Dependency Injection library for Python using decorators.

Description

This library attempts to simply injection of dependencies into functions, constructors and methods with minimal overhead during coding. This is done using the decorator @inject. When the function, constructor or method with this decorator is invoked the arguments are inspected to find any named argument that the callable allows, but is not in the argument list. The missing values are then replaced with values registered as an injectable.

Installation

pip install rr-inject

Basic Usage

from rr_inject import inject, injectable


# Mark the class type as injectable using the decorator.
@injectable(name="my_service")
class MyService:
    pass


# Register an instance of the decorated class. No need to keep a reference 
# since it will be held in the cache.
MyService()


# To receive the registered object as an argument simply use the name
# used to register the object as the parameter name and default it to `None`
@inject
def demo(my_service: MyService = None):
    print(my_service)


demo()

Output

<__main__.MyService object at 0x000001780FBA0E20>

Alternatives to using @injectable

Using @injectable requires that only one instance of the object in the application. Attempting to create a second instance will result in an error. You may also have types that you do not want to define and link to the decorator to not create a dependency on the rr-inject library in lower level packages. You can therefore use the register_injectable function instead.

register_injectable("my_service", MyService)

or

register_injectable("my_service", lambda: MyService())

or

inst = MyService()
register_injectable("my_service", lambda: inst)

Kwargs only with @inject

The only downside is that callables marked with @inject cannot be invoked with positional arguments, if you do you will get an error. All arguments must be named.

@inject
def add_util(a: int, b: int, calc: Calculator = None) -> int:
    return calc.add(a, b)

add_util(a=1, b=5)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rr_inject-0.2.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rr_inject-0.2.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file rr_inject-0.2.0.tar.gz.

File metadata

  • Download URL: rr_inject-0.2.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.10

File hashes

Hashes for rr_inject-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ff066a961b159f0287356168999f6e6c7718d8ab510ecd47cff41679e5ed2ef4
MD5 7d20d3485299bceba1bac81ad9150196
BLAKE2b-256 d58decd3c68780b00fd1b08175cd503926da805965cb5e130c86008ae27ec645

See more details on using hashes here.

File details

Details for the file rr_inject-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: rr_inject-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.10

File hashes

Hashes for rr_inject-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a116405d0d64c67ef0013fe4c1e2dab43e85ed5b6713a33211bd6a6c27c86310
MD5 5de168d55b2937d48e1dd76310722bd4
BLAKE2b-256 7bce43221fbe95367e0c3677ded6591becce8b520c3bcb7bddffe587029bc06b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page