Dependency injection thanks to type hints in Python 3.6+
Project description
auto-init is a dependency injection tool that works in Python 3.6+ thanks to type hints. If you write nice object oriented code and separate interfaces from implementations then you could benefit from this.
Introduction
Did you know that if you provide a type hint for an attribute in a class body and do not set a value then the attribute isn’t actually initialised (neither in class, nor in instances)?
class Point:
x: int
y: int
z: int = None
For p, an instance of Point, only p.z is set. This is great because we can build a dependency injection mechanism on top of this!
Simple Example
from auto_init import AutoInitContext
ctx = AutoInitContext()
p: Point = ctx.get_instance(Point)
assert p.x == 0
assert p.y == 0
assert p.z is None
Note that the Point class could also be a dataclass and it would work too.
Not So Simple Example
import logging
from auto_init import AutoInitContext
class Worker:
enterprise: "Enterprise"
log: logging.Logger
class Reporter:
enterprise: "Enterprise"
log: logging.Logger
class Enterprise:
worker: Worker
reporter: Reporter
ctx = AutoInitContext()
ctx.register_instance(logging.getLogger(__name__))
ctx.register_singleton(Enterprise)
enterprise: Enterprise = ctx.get_instance(Enterprise)
assert enterprise.worker.log is enterprise.reporter.log
assert enterprise.worker.enterprise is enterprise
assert enterprise.reporter.enterprise is enterprise
Installation
pip install auto-init
API
- AutoInitContext()
Create a new auto-initialisation context.
- register_singleton(instance_type: Type, factory: Callable=None)
Register a singleton type. This is different from register_instance in that here auto-init is responsible for the creation as well as initialisation of the singleton instance. This should be used when the singleton itself has dependencies that need to be injected. See the enterprise.py example under auto_init/examples/ . If factory is not supplied, the instance_type is used to create the instance.
- register_factory(instance_type: Type, factory: Callable)
Register a callable which is called to create a new instance of the specified type when on is requested.
- register_instance(instance, instance_type: Type=None)
Register an instance that should always be returned when an instance of the specified type is requested.
- get_instance(instance_type: Type) -> Any
Get an instance of the specified type.
- init_instance(instance)
Initialise any unitialised attributes of the instance.
Changelog
v0.0.5
Supports initialising attributes annotated with typing.List and typing.Dict as empty lists or dictionaries. Other typing.*-annotated attributes are initialised as None.
Do not attempt to get type hints from typing.* classes, it raises exceptions in Python 3.7 and does not return anything useful in Python 3.6 anyway.
Fixes dependencies for Python 3.6, removes unnecessary contextvars and dataclasses dependencies for Python 3.7.
Travis CI setup.
v0.0.4
Complete rewrite to deal with circular references. Intrusive approach is no good.
Forward references in type hints don’t really work in Python 3.7 even with futures. Let’s use Python 3.6.
v0.0.3
Added AutoInitContext.explicit_only – allows marking the context as only initialising the types with specified providers and leaving all others untouched.
If a type hint includes a default value (declares a class attribute) then the corresponding instance attribute will be a copy by reference of the class attribute unless there is an explicit provider specified in the context. This means that x: int = None will be initialised as None, not as 0.
v0.0.2
Non-intrusive auto-init: function auto_init and method AutoInitContext.auto_init that initialises instances without making any changes to user’s classes.
Added AutoInitContext.singleton_types – allows to specify singleton types non-intrusively.
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
Built Distribution
File details
Details for the file auto-init-0.0.5.tar.gz
.
File metadata
- Download URL: auto-init-0.0.5.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.0 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94db6303059dfac96054f758be00f090b5e7c47d8434487efc685c92cd0d104b |
|
MD5 | 343227cee45a56207292d047ea332d21 |
|
BLAKE2b-256 | 4766ad95cd3d3cff1c7d716737ce7c193937361bcd35868314e0cd8579626f53 |
File details
Details for the file auto_init-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: auto_init-0.0.5-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.0 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2468a32d1cd5aea84dc740893e5187ee7a8829d971ec7a08b6a0ca923f2a14e |
|
MD5 | fdb6f5f076545b78fb88e0a0b5bca055 |
|
BLAKE2b-256 | 99d77f369bc866bce568415d9dcf91a7af0ec9ff164ab9b4850117d563ff5406 |