Skip to main content

tagil

A simple dependency injection library for python

Simple usage

To mark class as injectable use @component annotation:

from tagil import component

@component
class InjectableClass:
    def __init__(self):
        pass

Now you can construct instance of this class with InjectionManager:

from tagil import InjectionManager

instance = InjectionManager().get_component(InjectableClass)

All dependent instances from __init__ will be resolved automatically if there is class annotation and @component decorator:

from tagil import component, InjectionManager

@component
class ClassWithDependency:
    def __init__(self, injectable: InjectableClass):
        self.injectable = injectable

instance = InjectionManager().get_component(ClassWithDependency)

Instance will be fully initialized.

Constructor decorator

You can assign function as a constructor function for component via @constructor decorator:

from tagil import constructor


class SomeDependency:
    pass


class SomeComponent:
    def __init__(self, dep):
       self.dep = dep

        
@constructor
def some_component(dep: SomeDependency) -> SomeComponent:
    return SomeComponent(dep)

Function some_component will be added as constructor. You can get its result by function name (or by decorator parameter name) and by class annotation if such annotation present. All dependencies from constructor arguments will be resolved the same way they are resolved in __init__ method.

Inject directive

In some rare cases you would like to manually set injectable components. For that case use inject parameter of @component or @constructor decorators:

from tagil import component, constructor


@component(inject={
    "dependency": "dependence_component_name",
})
class SomeComponent:
    def __init__(self, dependency):
        self.dependency = dependency


@constructor(inject={
    "dependency": DependencyClass,
})
def some_constructor(dependency):
    return SomeAnotherComponent(dependency)

In that case dependencies will be resolved by they names or classes provided in inject dictionary.

post_init and pre_destroy

When creating components tagil build initialization stack. You can manually call InjectionManager().post_init() and InjectionManager().pre_destory() or use application template via Application base class:

from tagil import component, Application


@component()
class SimpleApp(Application):
    def run(self) -> int:
        return 0


if __name__ == "__main__":
    SimpleApp.main()

All calls for post_init and pre_destroy methods of components will be performed by base class.

Component resolving algorythm

To define which component should be injected at instance creation tagil performs following set of rules:

  1. If injectable component name or class is set via inject argument, tagil will search component or constructor with that name or class.
  2. If type of argument is present:
    1. Tagil will search component or constructor with this type or with subclass type.
    2. In case of many of candidates tagil will try to use argument name as component or constructor name
  3. If no type information or decorator rules are set tagil will search component by argument name.

Metadata

Release files for tagil 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tagil 0.2.0
File Size Uploaded Provenance Actions
tagil-0.2.0.tar.gz 4.4 kB Without provenance Details for tagil-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tagil 0.2.0
File Interpreter ABI Platform Actions
tagil-0.2.0-py3-none-any.whl Python 3 none any Details for tagil-0.2.0-py3-none-any.whl

Total release size: 10.3 kB

Release files / tagil-0.2.0.tar.gz

Download URL tagil-0.2.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3f696aa4cc7d718055bb32f2cc06b2ee79dda1a17695f7792f0e59b02508083c
BLAKE2b-256 checksum
How to use checksums
c5f01039a44a1a72354ccd66cf827f41028dc2c4d20e893383c4573323cd4d7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / tagil-0.2.0-py3-none-any.whl

Download URL tagil-0.2.0-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31c8e13363c1ed79f88ac52489503ca20d57a61f1c45cb70655086f6c223a79d
BLAKE2b-256 checksum
How to use checksums
eaa40cc27baeeac81d15ee15e9d55a24345c1a0c572123c90a421b7a2d27cbed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for tagil 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page