How to use injectable - 10 common examples

To help you get started, we’ve selected a few injectable examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github allrod5 / injectable / tests / test_autowired.py View on Github external
def test_positional_arg_as_injectable_raises_type_error(self):
        with pytest.raises(TypeError):
            autowired(['b'])(foo)
github allrod5 / injectable / tests / test_autowired.py View on Github external
def test_autowiring(self):
        caller_args = (False, None, True)
        caller_kwargs = {
            'w': {},
            'x': "string",
            'f': lambda x: print(x),
            'kwargs': {'extra': True},
        }

        _, kwargs = autowired()(foo)(*caller_args, **caller_kwargs)

        assert isinstance(kwargs['y'], bool)
        assert isinstance(kwargs['z'], DummyClass)
        assert isinstance(kwargs['l'], DummyClass)
        assert isinstance(kwargs['s'], DummyClass)
github allrod5 / injectable / tests / test_autowired.py View on Github external
def test_injectable_with_non_instantiable_class_raises_type_error(self):
        # eligible injectable argument is annotated with non
        # instantiable class
        with pytest.raises(TypeError):
            autowired()(baz)
        with pytest.raises(TypeError):
            autowired()(qux)

        # specified argument for injection is annotated with non
        # instantiable class
        with pytest.raises(TypeError):
            autowired(['nope'])(baz)
        with pytest.raises(TypeError):
            autowired(['nope'])(qux)
github allrod5 / injectable / tests / test_examples.py View on Github external
    @autowired(lazy=True)
    def __init__(self, *, dep1: CircularDependant1, dep2: CircularDependant2):
        self.dep1 = dep1
        self.dep2 = dep2
github allrod5 / injectable / tests / test_autowired.py View on Github external
def test_missing_kwonly_args_raises_type_error(self):
        with pytest.raises(TypeError):
            autowired()(foo)(a=None, b=10, c='')
github allrod5 / injectable / tests / test_autowired.py View on Github external
def test_injectable_lazy_initialization(self):
        caller_args = (False, None, True)
        caller_kwargs = {
            'w': {},
            'x': "string",
            'f': lambda x: print(x),
            'kwargs': {'extra': True},
        }

        _, kwargs = autowired()(foo)(*caller_args, **caller_kwargs)

        assert isinstance(kwargs['l'], Proxy)
        assert isinstance(kwargs['l'], DummyClass)
        assert kwargs['l'].dummy_method() == 42
github allrod5 / injectable / examples / injection_container_resetting_for_tests / injection_container_resetting_example.py View on Github external
def run_example():
    load_injection_container()
    example = InjectionContainerResetting()
    example.run()
github allrod5 / injectable / injectable / testing / clear_injectables_util.py View on Github external
:param namespace: (optional) namespace in which the injectable will be registered.
            Defaults to :const:`injectable.constants.DEFAULT_NAMESPACE`.

    Usage::

      >>> from injectable.testing import clear_injectables
      >>> clear_injectables("foo")

    .. versionadded:: 3.3.0
    """
    namespace = InjectionContainer.NAMESPACES[namespace or DEFAULT_NAMESPACE]
    if isinstance(dependency, str):
        injectables = namespace.qualifier_registry[dependency]
        namespace.qualifier_registry[dependency] = set()
    else:
        dependency_name = get_dependency_name(dependency)
        injectables = namespace.class_registry[dependency_name]
        namespace.class_registry[dependency_name] = set()
    return injectables
github allrod5 / injectable / examples / injection_container_resetting_for_tests / injection_container_resetting_example.py View on Github external
def run(self):
        self.bar()
        # doing something

        reset_injection_container()

        try:
            self.bar()
            # WARNING:root:Injection Container is empty. Make sure \
            # 'load_injection_container' is being called before any injections are made.
        except InjectionError as e:
            print(e)
            # No injectable matches class 'Foo'
github allrod5 / injectable / injectable / testing / clear_injectables_util.py View on Github external
"""
    Utility function to clear all injectables registered for the dependency in a given
    namespace. Returns a set containing all cleared injectables.

    :param dependency: class or qualifier of the dependency.
    :param namespace: (optional) namespace in which the injectable will be registered.
            Defaults to :const:`injectable.constants.DEFAULT_NAMESPACE`.

    Usage::

      >>> from injectable.testing import clear_injectables
      >>> clear_injectables("foo")

    .. versionadded:: 3.3.0
    """
    namespace = InjectionContainer.NAMESPACES[namespace or DEFAULT_NAMESPACE]
    if isinstance(dependency, str):
        injectables = namespace.qualifier_registry[dependency]
        namespace.qualifier_registry[dependency] = set()
    else:
        dependency_name = get_dependency_name(dependency)
        injectables = namespace.class_registry[dependency_name]
        namespace.class_registry[dependency_name] = set()
    return injectables