Skip to main content

Fast and easy dependency injection framework.

Project description

Injection

CI PyPI Black

Fast and easy dependency injection framework.

Quick start

⚠️ Requires Python 3.10 or higher

pip install python-injection

How to use

Create an injectable

If you wish to inject a singleton, use unique decorator.

from injection import unique

@unique
class MyClass:
    """ class implementation """

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

from injection import new

@new
class MyClass:
    """ 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: MyClass):
    """ function implementation """

Inheritance

In the case of inheritance, you can use the decorator parameters reference or references 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.

reference parameter example:

from injection import unique

class A:
    ...

@unique(reference=A)
class B(A):
    ...

references parameter example:

from injection import unique

class A:
    ...

class B(A):
    ...

@unique(references=(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 reference class(es) when defining the recipe.

from injection import unique

@unique(reference=MyClass)
def my_recipe() -> MyClass:
    """ recipe implementation """

Utils

load_package

Useful for put in memory injectables hidden deep within a package. Example:

package
├── sub_package
│   ├── __init__.py
│   └── module2.py
│       └── class Injectable1
├── __init__.py
└── module1.py
    └── class Injectable2

To load Injectable1 and Injectable2 into memory you can do the following:

from injection.utils import load_package

import package

load_package(package)

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

Uploaded Source

Built Distribution

python_injection-0.2.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_injection-0.2.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure

File hashes

Hashes for python_injection-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fb8a2cfcf599e8dcfccb0c04ac69c0516c43d1f89d379e1ae5620d1901e824a0
MD5 18f827a43da000b2545b5fbd7ba9b944
BLAKE2b-256 4c56206ce1b5d9d2f2e83969a9aa5e5e7ed470e430ace31c9b2c74bb98425421

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_injection-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure

File hashes

Hashes for python_injection-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ca9c4e41d878cb6405cb9f903edd5fee4ed1bc907e3c92fa6bc5a068b1466f1
MD5 9e6cf133ecd1daf22e473ae05f4cfb50
BLAKE2b-256 393775f011d877852844ba7138059efac65be64888eae19b3f474fdfe9c3be2f

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