Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
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)?
.. code-block:: python
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!
.. code-block:: python
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.
.. code-block:: python
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 ++++++++++++
.. code-block:: shell
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 +++++++++
typing.List
and typing.Dict
as empty lists or dictionaries.
Other typing.*
-annotated attributes are initialised as None
.typing.*
classes, it raises exceptions in Python 3.7 and does not return
anything useful in Python 3.6 anyway.contextvars
and dataclasses
dependencies for Python 3.7.AutoInitContext.explicit_only
-- allows marking the context as only initialising the types with specified
providers and leaving all others untouched.x: int = None
will be initialised as None
, not as 0
.auto_init
and method AutoInitContext.auto_init
that initialises instances
without making any changes to user's classes.AutoInitContext.singleton_types
-- allows to specify singleton types non-intrusively.FAQs
Dependency injection thanks to type hints in Python 3.6+
We found that auto-init demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.