Skip to main content

Simple Injection

Build Status codecov Documentation Status PyPI version PyPI license

Introduction

Simple Injection is a simple, objected-oriented approach to dependency injection in python. The goal of Simple Injection is to allow simple and effective dependency injection in python applications without the use of anything other than what is natively in your application. This means no decorators or anything else anywhere in your code is required for Simple Injection. All that is required is python typings.

Features

  • Only dependent on native python code and typings. Services are injected through typing annotations, rather than variable names or decorators in your code.
  • Easily define service lifetimes.
  • Enforces typings in your application.
  • Bind abstract services to their implementations.
  • Add services in any order, resolution occurs when a service is request.

Installation

Simply install Simple Injection through pip.

pip install simple-injection

Usage

Due to Simple Injection relying only on typings, it is easy to add to your application.

from simple_injection import ServiceCollection


class Dependency:
    def hello(self):
        print("Hello from Dependency!")

class Service:
    def __init__(self, dependency: Dependency):
        self._dependency = dependency

    def hello(self):
        self._dependency.hello()

collection = ServiceCollection()
collection.add_transient(Dependency)
collection.add_transient(Service)

collection.resolve(Service).hello()
# Outputs: Hello from Dependency!

This approach to dependency injection makes it easy to use mocks when developing and unit testing.

class MockDependency:
    def hello(self):
        print("Hello from MockDependency!")

collection = ServiceCollection()
collection.add_transient(Dependency, MockDependency)
collection.add_transient(Service)

collection.resolve(Service).hello()
# Outputs: Hello from MockDependency!

This can also be achieved through the use of an interface (or base class) that both the dependency and the mock inherit from, but as the above example shows, it is not required.

Simple Injection will also allow you to simply inject strings and other constants to your dependencies, easily injecting the needed dependency to your class with the constant.

from simple_injection import ServiceCollection, ServiceResolverFlags


class Dependency:
    def __init__(self, my_str: str):
        self.my_str = my_str

class Service:
    def __init__(self, dependency: Dependency, my_int: int):
        self.my_int
        self._dependency = dependency

    def get_str(self):
        return self._dependency.my_str

collection = ServiceCollection()
collection.add_transient(Dependency, args=["Example string!"])
collection.add_transient(Service, args=[ServiceResolverFlags.REQUIRED_SERVICE ,23])

service = collection.resolve(Service)
service.my_int # 23
service.get_str() # Example string!

See examples for more examples.

Documentation

Documentation for Simple Injection can be found on readthedocs.

Contributing

Contributions are more than welcome. Feel welcome to add issues or make pull requests!

License

MIT

Metadata

Release files for simple-injection 0.2.5

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

Source distribution (sdist)

Source distribution for simple-injection 0.2.5
File Size Uploaded
simple-injection-0.2.5.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for simple-injection 0.2.5
File Interpreter ABI Platform
simple_injection-0.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 17.6 kB

Release files / simple-injection-0.2.5.tar.gz

Download URL simple-injection-0.2.5.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9c2e7943cb99cefafe451517098afeccb1de635bf81e686414dc3710f50929f4
BLAKE2b-256 checksum
How to use checksums
956e906c5af858f6741bc7046c6348104f3a261915fd46a2a186171a3e3e301e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / simple_injection-0.2.5-py3-none-any.whl

Download URL simple_injection-0.2.5-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7facbe055910e2535db0959c0dd218b5c82f33d235f92311d172cfebc9a12921
BLAKE2b-256 checksum
How to use checksums
b0153359c8226411cf9bca3fca817aedbb5d66331675dd5605d35e2b126a34cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release history Release notifications | RSS feed

This release

0.2.5 This release

2 release files

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

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