Skip to main content

Wint - dependency injection with type hints.

Build Status Coverage Status

Intro

Wint is a lightweight library that implements dependency injection via type hinting.

Installation

Just use pip:

$ pip install wint

How it works

There are a ton of approaches to DI, and this library implements only property injection.

When autowired decorator is applied then class is inspected for all annotated properties. If property has no value, then it is replaced with DependencyDescriptor.

Dependency is resolved when attribute is accessed on instantiated class instance.

This behaviour allows to resolve dependencies in lazy manner with the downside of a side effect — instance receives a descriptor property that was not existed earlier.

Example

from wint import autowired, ContainerProvider


class Printer:
    """Abstract class for printing messages."""

    def print(self, message):
        raise NotImplementedError


class RealPrinter(Printer):
    """Implementation of `Printer` which uses `print` to output messages."""

    def print(self, message):
        print(message)


@autowired()
class PrintService:
    # RealPrinter will be automatically injected on property access.
    printer: Printer

    def run(self, msg):
        self.printer.print(f"{msg}, i'm running!")


if __name__ == "__main__":
    container = ContainerProvider.get()
    # Register RealPrinter as singleton implementation of Printer.
    container.register(Printer, RealPrinter())

    PrintService().run('hey')
$ python example.py
hey, i'm running!

Notes

This library is built on punq (MIT License) and uses it's vendored version.

Metadata

Release files for wint 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 wint 0.2.0
File Size Uploaded
wint-0.2.0.tar.gz 6.4 kB Details

Built distribution (wheel)

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

Total release size: 30.0 kB

Release files / wint-0.2.0.tar.gz

Download URL wint-0.2.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
96b5f93ea7aa2d01e47ad3e35144d0aaa913278507b1b059b007826f77f7f4dd
BLAKE2b-256 checksum
How to use checksums
761a5612341055155268a97ca8f0be196e1d21d152c2ebc4c16b1788213fe929
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL wint-0.2.0-py3-none-any.whl
Size 23.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a9184eece1fa3438fa86600d5dc3ecfb0ea8c54f41627b5914e3191fd316ffb
BLAKE2b-256 checksum
How to use checksums
c8b981aab1d9fe2e405b51c25db5e5a5c9c8e5b701008f9c7055711126668706
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

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