Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Readme
.. _injectable: .. role:: python(code) :language: python
Usage Examples 🚩 <https://injectable.readthedocs.io/en/latest/usage/index.html>
_ | Developer Reference 👩💻 <https://injectable.readthedocs.io/en/latest/reference/index.html>
_ | Authors 👫 <https://injectable.readthedocs.io/en/latest/authors.html>
_
Injectable is an elegant and simple Dependency Injection framework built with Heart and designed for Humans.
.. list-table:: :header-rows: 0
* - .. code:: python
from injectable import Autowired, autowired
from typing import List
from models import Database
from messaging import Broker
class Service:
@autowired
def __init__(
self,
database: Autowired(Database),
message_brokers: Autowired(List[Broker]),
):
pending = database.get_pending_messages()
for broker in message_brokers:
broker.send_pending(pending)
.. code:: python
from abc import ABC
class Broker(ABC):
def send_pending(messages):
...
- .. code:: python
from injectable import injectable
@injectable
class Database:
...
.. code:: python
from messaging import Broker
from injectable import injectable
@injectable
class KafkaProducer(Broker):
...
.. code:: python
from messaging import Broker
from injectable import injectable
@injectable
class SQSProducer(Broker):
...
Autowiring: injection is transparent to the function. Just decorate the function
with :python:@autowired
and annotate parameters with :python:Autowired
, that's it.
Automatic dependency discovery: just call :python:load_injection_container()
at
the root of your project or pass the root path as an argument. All classes decorated
with :python:@injectable
will be automatically discovered and ready for injection.
Qualifier overloading: declare as many injectables as you like for a single
qualifier or extending the same base class. You can inject all of them just by
specifying a :python:typing.List
to :python:Autowired
: :python:deps: Autowired(List["qualifier"])
.
Transparent lazy initialization: passing the argument :python:lazy=True
for
:python:Autowired
will make your dependency to be initialized only when actually used, all
in a transparent fashion.
Singletons: decorate your class with :python:@injectable(singleton=True)
and only a
single instance will be initialized and shared for injection.
Namespaces: specify different namespaces for injectables as in
:python:@injectable(namespace="foo")
and then just use them when annotating your
parameters as in :python:dep: Autowired(..., namespace="foo")
.
Linters friendly: :python:Autowired
is carefully designed to comply with static linter
analysis such as PyCharm's to preserve the parameter original type hint.
These are just a few cool and carefully built features for you. Check out our docs <https://injectable.readthedocs.io/en/latest/>
_!
testing.register_injectables
not creating the namespace when it doesn't exist
yetinject
return type hintInjectionContainer::load
in favor of load_injection_container
."_GLOBAL"
to "DEFAULT_NAMESPACE"
.reset_injection_container
utility to injectable.testing
.injectable.testing
utilities to ease mocking injectables.InjectionContainer::load
is more resilient against duplicated injectables
registeringAutowired(Optional[...])
Autowired(List[...])
not working with qualifiersinspect
imports.InjectionContainer.load()
is called multiple times with different relative search paths.@injectable_factory
decorator for declaring injectable factory methods@autowired
decorator for working with the Autowired
type annotation@injectable
decorator for registering injectables to the InjectionContainerAutowired
type annotation for marking parameters for autowiringinject
and inject_multiple
as service locatorsTypeError
when injectable fails on the trial dependency
instantiation which can happen when the dependency does provide a default
constructor with no arguments but the running environment (possibly a test suite
environment) will make the instantiation fail@autowired
decorator without parenthesislazy_object_proxy
not being installed when installing
injectable through pipFAQs
Dependency Injection for Humans™
We found that injectable 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.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.